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. To automate the printing of PDF documents, you can follow the simple steps below: Verify the following code snippets to see how to print PDF files using C# and VB.NET: 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: The following code snippet shows how to print multiple PDF files using C# and VB.NET: 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: The code snippet below shows how to print specific pages of the document using C# and VB.NET: 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: 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. 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.How to print PDF documents in C# and VB.NET
PdfDocument
class.PdfDocument.Print
methods.How to print multiple PDF files programmatically
PdfDocument.Load(String)
.PdfDocument.Print(String)
.// 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
PdfDocument
class.null
to use the default one.FromPage
and ToPage
parameters.PdfDocument.Print(printerName, printOptions)
method.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
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
Conclusion