Printing PDF documents Programmatically

When working with many PDF documents, the best solution is to manage all the operations programmatically. That includes printing on a large scale since you can solve everything faster in just a few lines of code instead of doing everything manually. With the GemBox.Pdf API, you can print high-quality PDF documents in .NET, using C# and VB.NET.

In this article, you will learn how to print PDF files programmatically, send them to the default printer, or specify any other local or network printer connected to your machine. You will also learn how to do silent printing or provide a print dialog and print preview.

Scan of printed PDF file
Scan of a printed PDF file

To automate the printing of PDF documents, you can follow the simple steps below:

  1. Load the input PDF document using the PdfDocument class.
  2. Print the PDF file to a default printer with one of the PdfDocument.Print methods.

Verify the following code snippets to see how to print PDF files using C# and VB.NET:

using (var document = PdfDocument.Load("%#Print.pdf%"))
    // Print the document to default printer
    document.Print();
Using document = PdfDocument.Load("%#Print.pdf%")
    ' Print the document to default printer
    document.Print()
End Using

How to print multiple PDF files programmatically

Depending on your work demands for PDF, it can be overwhelming and even impossible to handle each document, especially when you need to print dozens of different files simultaneously. As you can imagine, by extending the code presented in the previous section, you can easily print multiple files programmatically. To do that, simply follow these steps:

  1. Compose a list of the source file names you need to print.
  2. Iterate through the list of file names.
  3. Load the input PDF file using PdfDocument.Load(String).
  4. Print the files on the list with PdfDocument.Print(String).

The following code snippet shows how to print multiple PDF files using C# and VB.NET:

// Define file names
var fileNames = new string[]
{
    "PrintFile01.pdf",
    "PrintFile02.pdf",
    "PrintFile03.pdf"
};

// Define the printer name or set to 'null' for the default one
string printerName = null;

// Iterate through the list of file names
foreach (var fileName in fileNames)
    // Load a PDF file
    using (var document = PdfDocument.Load(fileName))
        // Print the document to specified printer
        document.Print(printerName);
' Define file names
Dim fileNames = New String(2) _
{
    "PrintFile01.pdf",
    "PrintFile02.pdf",
    "PrintFile03.pdf"
}

' Define the printer name or set to 'Nothing' for the default one
Dim printerName As String = Nothing;

' Iterate through the list of file names
For Each fileName In fileNames
    ' Load a PDF file
    Using document = PdfDocument.Load(fileName)
        ' Print the document to specifed printer
        document.Print(printerName)
    End Using
Next

How to print specific pages of a PDF

With GemBox.Pdf API, you can also print page ranges, which can be handy depending on the kind of job you are performing. Follow the steps below to print specific pages:

  1. Load the file you want to print the pages from, using the PdfDocument class.
  2. Select the printer by specifying the printer name, or set it to null to use the default one.
  3. Set specific pages to print by defining a range with the FromPage and ToPage parameters.
  4. Print the pages using the PdfDocument.Print(printerName, printOptions) method.

The code snippet below shows how to print specific pages of the document using C# and VB.NET:

using (var document = PdfDocument.Load("%#LoremIpsum.pdf%"))
{
    // Define the printer name
    var printerName = "Microsoft Print to PDF";

    // Define the range of pages to print (second to third page in this case)
    // NOTE: page range is zero-based which means that page numbers start with 0
    var printOptions = new PrintOptions() { FromPage = 1, ToPage = 2 };
    
    // Print the pages
    document.Print(printerName, printOptions);
}
Using document = PdfDocument.Load("%#LoremIpsum.pdf%")
    ' Define the printer name
    Dim printerName = "Microsoft Print to PDF"

    ' Define the range of pages to print (second to third page in this case)
    ' NOTE: page range Is zero - based which means that page numbers start with 0
    Dim PrintOptions = New PrintOptions() With {.FromPage = 1, .ToPage = 2}

    ' Print the pages
    document.Print(printerName, PrintOptions)
End Using

Accessing advanced printing capabilities

In GemBox.Pdf, you can also access advanced printing capabilities, such as specifying the printer’s paper source (tray) or two-sided printing, for example. For these specific options, you can use the PrintTicket class in your code, such as in the snippet below:

using (var document = PdfDocument.Load("%#LoremIpsum.pdf%"))
{
    // Create a print ticket and define print job settings
    var printTicket = new PrintTicket
    {
        CopyCount = 2,
        Collation = Collation.Collated,
        Duplexing = Duplexing.TwoSidedLongEdge
    };

    // Convert the print ticket to GemBox.Pdf.PrintOptions
    var printOptions = new PrintOptions(printTicket.GetXmlStream());

    // Print the document to default printer
    document.Print(null, printOptions);
}
Using document = PdfDocument.Load("%#LoremIpsum.pdf%")
    ' Create a print ticket And define print job settings
    Dim printTicket = New PrintTicket With
    {
        .CopyCount = 2,
        .Collation = Collation.Collated,
        .Duplexing = Duplexing.TwoSidedLongEdge
    }

    ' Convert the print ticket to GemBox.Pdf.PrintOptions
    Dim PrintOptions = New PrintOptions(printTicket.GetXmlStream())

    ' Print the document to default printer
    document.Print(Nothing, PrintOptions)
End Using

Note that when working with PrintTicket instances, you don't have to fetch the DefaultPrintTicket from the desired printer's PrintQueue. GemBox.Pdf will do that internally and merge it with the PrintTicket you provided in your code. That’s why, as shown in the example above, it’s not necessary to specify all the options but only the ones you want to change.

Conclusion

Now you know how to optimize your work by printing PDF files programmatically. Besides the actions for printing described in this article, you can use the GemBox.Pdf API to read, write, merge, and split PDF files and execute other low-level object manipulations in a very straightforward and quick way.

For more information regarding the GemBox.Pdf API, check the documentation pages. You can also see the Print PDF files in C# and VB.NET example for more information on printing PDF documents in WPF applications.

See also


Next steps

GemBox.Pdf is a .NET component that enables developers to read, merge and split PDF files or execute low-level object manipulations from .NET applications in a simple and efficient way.

Download Buy