Skip to content

Example of using Windows.Data.Pdf to print a PDF via Direct2D

License

Notifications You must be signed in to change notification settings

mgaffigan/D2DPdfPrintSample

Repository files navigation

D2D PDF Printer

Example of using Windows.Data.Pdf from C++/winrt or C# to print a PDF file.

No third party apps required - no additional licenses. Only Windows.

PDF's are printed as vectors. Text is preserved. Minimal testing done - demo only, not production ready.

For a production C# version, consider Rmg.PdfPrinting (Nuget).

Key steps

  1. Reference/initialize winrt (e.g.: winrt::init_apartment(winrt::apartment_type::single_threaded);)
  2. Decide what printer to use (PrintDlgEx or similar) and create a print ticket
  3. Open the PDF with PdfDocument::LoadFromFileAsync(file)
  4. Acquire system resources (D3D, DXGI, D2D, WIC)
  5. Create a print job (IPrintDocumentPackageTargetFactory::CreateDocumentPackageTargetForPrintJob and ID2D1PrintControl)
  6. Create a PDF renderer (PdfCreateRenderer)
  7. Loop over pages, render to a command list, RenderPageToDeviceContext, and add the command list as a page to the ID2D1PrintControl.
  8. End the document with ID2D1PrintControl::Close
  9. Keep alive for a bit, pumping messages (seemingly only needed for printing to virtual printers like "Acrobat PDF Converter")

Examples

Future possibilites

  • WPF viewer control
  • NuGet wrapper

About

Example of using Windows.Data.Pdf to print a PDF via Direct2D

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published