How to print PDF file in .NET Core

The following code snippet also work with Aspose.PDF.Drawing library.

The Aspose.PDF library allows us to convert PDF files to XPS. This function can be useful for organizing the printing of documents. Let’s take a look at an example for using the default printer.

In this example, we convert PDF document into XPS and add it as a job to the queue of the local printer:

class Program
{
    static void Main()
    {
        // Create the secondary thread and pass the printing method for
        // the constructor's ThreadStart delegate parameter.
        Thread printingThread = new Thread(() => PrintPDF(@"C:\tmp\doc-pdf.pdf"));

        // Set the thread that will use PrintQueue.AddJob to single threading.
        printingThread.SetApartmentState(ApartmentState.STA);

        // Start the printing thread. The method passed to the Thread
        // constructor will execute.
        printingThread.Start();
    }//end Main

    private static void PrintPDF(string pdfFileName)
    {
        // Create print server and print queue.
        PrintQueue defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue();

        Aspose.Pdf.Document document = new Document(pdfFileName);
        var xpsFileName = pdfFileName.Replace(".pdf", ".xps");
        document.Save(xpsFileName,SaveFormat.Xps);

        try
        {
            // Print the Xps file while providing XPS validation and progress notifications.
            PrintSystemJobInfo xpsPrintJob = defaultPrintQueue.AddJob(xpsFileName, xpsFileName, false);
            Console.WriteLine(xpsPrintJob.JobIdentifier);
        }
        catch (PrintJobException e)
        {
            Console.WriteLine("\n\t{0} could not be added to the print queue.", pdfFileName);
            if (e.InnerException != null && e.InnerException.Message == "File contains corrupted data.")
            {
                Console.WriteLine("\tIt is not a valid XPS file. Use the isXPS Conformance Tool to debug it.");
            }
            Console.WriteLine("\tContinuing with next XPS file.\n");
        }
    }
}//end Program class

Choosing paper source by PDF page size

Since the 24.4 release, choosing paper source by PDF page size in the print dialog is possible. The next code snippet enables picking a printer tray based on the PDF’s page size.

This preference can be switched on and off using the ‘Document.PickTrayByPdfSize’ property.

using (Document document = new Document())
{
    Page page = document.Pages.Add();
    page.Paragraphs.Add(new TextFragment("Hello world!"));

    // Set the flag to choose a paper tray using the PDF page size
    document.PickTrayByPdfSize = true;
    document.Save("result.pdf");
}

The next code snippet is intended to ensure that the PrintScaling property is correctly applied and saved in the PDF.

The PrintScaling property has been added to the Document class with values ​​Aspose.Pdf.PrintScaling.AppDefault or Aspose.Pdf.PrintScaling.None.

The page scaling option that shall be selected when a print dialog is displayed for this document. Valid values are None, which indicates no page scaling, and AppDefault, which indicates the conforming reader’s default print scaling. If this entry has an unrecognized value, AppDefault should be used. Default value: AppDefault.

public void PDFNET_111()
{
    Object[] printScalingValues = { null, PrintScaling.None, PrintScaling.Default };
    PrintScaling[] printScalingExpected = { PrintScaling.Default, PrintScaling.None, PrintScaling.Default };
    for (int i = 0; i < printScalingValues.Length; i++)
    {
        Document document = new Document();
        document.Pages.Add();
        Object printScalingValue = printScalingValues[i];
        if (printScalingValue != null)
            document.PrintScaling = (PrintScaling)printScalingValue;
        String outputPdf = GetOutputPdf("PDFNET-111_" + i);
        document.Save(outputPdf);
        Document documentOutput = new Document(outputPdf);
        Assert.AreEqual(printScalingExpected[i], documentOutput.PrintScaling);
    }
}