Advanced Printing
The PrintJob class provides several advanced options available when printing PDF documents.
The following examples demonstrate these advanced printing topics. The following examples illustrate selectively printing PDF Content using page ranges, multiple pages per sheet, a specified area within a PDF document, and portfolio printing.
Print Page Ranges
The Pages property is an PrintJobPageList that contains an Add method that allows adding only the pages in a page range from a PDF.
public void Add(InputPdf pdf, int startPage, int pageCount)
In this example, you add a range of pages from different PDFs to a print job. When printing the PDF, the output for the first job is pages one through three, the second job pages three and four, and the third job pages two through five.
PrintJob printJob = new PrintJob(Printer.Default);
printJob.Pages.Add(pdfFilePath, 1, 3);
printJob.Pages.Add(pdfFilePath, 3, 2);
printJob.Pages.Add(pdfFilePath, 2, 4);
printJob.Print();
Dim printjob As PrintJob = New PrintJob(Printer.Default)
printJob.Pages.Add(pdfFilePath, 1, 3)
printjob.Pages.Add(pdfFilePath, 3, 2)
printjob.Pages.Add(pdfFilePath, 2, 4)
printjob.Print()
Print Multiple Pages per Sheet
The PrintJob class has a PrintOptions property with a Scaling property that allows the MultipagePageScaling class to print multiple pages to a single output page. In this example, you print multiple PDF pages on a single sheet of paper using four rows and two columns.
PrintJob printJob = new PrintJob(Printer.Default, pdfFilePath, 1, 4);
MultipagePageScaling multipagePageScaling = new MultipagePageScaling(4, 2);
multipagePageScaling.Border = new Border(System.Drawing.Color.Green, 2);
multipagePageScaling.Margin = new ceTe.DynamicPDF.Printing.Margin(5, 5, 5, 5);
multipagePageScaling.Spacing = new Spacing(2, 2);
multipagePageScaling.UniformScaling = true;
printJob.PrintOptions.Scaling = multipagePageScaling;
printJob.Print();
Dim printJob As New PrintJob(Printer.Default, pdfFilePath, 1, 4)
Dim multipagePageScaling As New MultipagePageScaling(4, 2)
multipagePageScaling.Border = New Border(System.Drawing.Color.Green, 2)
multipagePageScaling.Margin = New ceTe.DynamicPDF.Printing.Margin(5, 5, 5, 5)
multipagePageScaling.Spacing = New Spacing(2, 2)
multipagePageScaling.UniformScaling = True
printJob.PrintOptions.Scaling = multipagePageScaling
printJob.Print()
Print Specified Area of a PDF Page
The PrintJobPageList also contains an overloaded Add method that allows printing part of a PDF's page.
public void Add(InputPdf pdf, Rectangle rect, int startPage, int pageCount)
The following example demonstrates.
InputPdf pdf = new InputPdf(pdfFilePath);
PrintJob printJob = new PrintJob(Printer.Default);
//Specify print area for the second & third pages and add them to print job.
System.Drawing.Rectangle printArea = new System.Drawing.Rectangle(0, 0, 600, 400);
printJob.Pages.Add(pdf, printArea, 2, 2);
printJob.Print();
Dim pdf As InputPdf = New InputPdf(pdfFilePath)
Dim printJob As PrintJob = New PrintJob(Printer.Default)
'Specify print area for the second & third pages and add them to print job.
Dim printArea As System.Drawing.Rectangle = New System.Drawing.Rectangle(0, 0, 600, 400)
printJob.Pages.Add(pdf, printArea, 2, 2)
printJob.Print()
Print PDFs from PDF Portfolio
The InputPdf class has an Attachments property that returns an array of Attachment instances. Then, for each Attachment you can call the TryGetPDF method to get the PDF attachment as an InputPdf instance.
public InputPdf TryGetPdf()
Note: only PDF attachments are retrieved, if an attachment is not a PDF then the method returns null.
This final example demonstrates how to retrieve and print PDF attachments from a PDF portfolio.
InputPdf portfolioPDF = new InputPdf(pdfFilePath);
PrintJob printJob = new PrintJob(Printer.Default);
Attachment[] files = portfolioPDF.Attachments;
foreach (Attachment file in files)
{
InputPdf pdfAttachment = file.TryGetPdf();
if (pdfAttachment != null)
printJob.Pages.Add(pdfAttachment);
}
if (printJob.Pages.Count > 0)
printJob.Print();
Dim portfolioPDF As InputPdf = New InputPdf(pdfFilePath)
Dim printJob As PrintJob = New PrintJob(Printer.Default)
Dim files As Attachment() = portfolioPDF.Attachments
For Each file As Attachment In files
Dim pdfAttachment As InputPdf = file.TryGetPdf()
If pdfAttachment IsNot Nothing Then
printJob.Pages.Add(pdfAttachment)
End If
Next
If printJob.Pages.Count > 0 Then
printJob.Print()
End If
GitHub
Refer to the print-manager-dotnet-core project on GitHub for examples demonstrating how to use the DynamicPDF PrintManager for .NET. Examples include the following files.
- MultiplePagesPerSheet.cs
- MultiplePagesPerSheet.vb