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).
- Reference/initialize winrt (e.g.:
winrt::init_apartment(winrt::apartment_type::single_threaded);
) - Decide what printer to use (
PrintDlgEx
or similar) and create a print ticket - Open the PDF with
PdfDocument::LoadFromFileAsync(file)
- Acquire system resources (D3D, DXGI, D2D, WIC)
- Create a print job (
IPrintDocumentPackageTargetFactory::CreateDocumentPackageTargetForPrintJob
andID2D1PrintControl
) - Create a PDF renderer (
PdfCreateRenderer
) - Loop over pages, render to a command list,
RenderPageToDeviceContext
, and add the command list as a page to theID2D1PrintControl
. - End the document with
ID2D1PrintControl::Close
- Keep alive for a bit, pumping messages (seemingly only needed for printing to virtual printers like "Acrobat PDF Converter")
- D2DPrintSample
C++ Sample based on https://github.com/microsoft/Windows-classic-samples/blob/main/Samples/D2DPrintingFromDesktopApps/cpp/D2DPrintingFromDesktopApps.cpp - PrintPdf
Minimal C++ sample reduced from D2DPrintSample - PrintPdfManaged
C#/net7 port of PrintPdf
- WPF viewer control
- NuGet wrapper