PDF: Print PDF documents in WinForms
In This Topic
The
ImagePrintDocument control is intended for printing images and it rasterizes PDF pages before printing. Rasterization of PDF page can take long time and a lot of memory especially if PDF page is rendered with large resolution.
The
PdfPrintDocument class, which is derived from
ImagePrintDocument class, allows to avoid the above situation because the class prints the PDF pages in a vector form. It is recommended to use
PdfPrintDocument class instead of
ImagePrintDocument class if the collection of printed images includes PDF pages.
PdfPrintDocument class prints raster images the same way as when using
ImagePrintDocument class.
PdfPrintDocument class provides all print settings available in the
ImagePrintDocument class.
Here is C#/VB.NET code that demonstrates how to print a PDF document on a default printer, each page of the document will be printed on separate sheet in BestFit mode:
/// <summary>
/// The printing PDF pages.
/// </summary>
Vintasoft.Imaging.ImageCollection _printingPdfPages;
/// <summary>
/// The index of printing PDF page.
/// </summary>
int _printingPdfPageIndex;
/// <summary>
/// Prints pages of the PDF document on default printer.
/// </summary>
/// <param name="pdfFilePath">Path to the PDF file.</param>
public void PrintPdfPagesOnDefaultPrinter(string pdfFilePath)
{
// create printing page collection
_printingPdfPages = new Vintasoft.Imaging.ImageCollection();
_printingPdfPages.Add(pdfFilePath);
_printingPdfPageIndex = 0;
// create print manager
Vintasoft.Imaging.Pdf.Print.PdfPrintDocument pdfPrintDocument =
new Vintasoft.Imaging.Pdf.Print.PdfPrintDocument();
// specify that each image must be resized to fit within the page margins,
// image proportions are not changed
pdfPrintDocument.PrintScaleMode = Vintasoft.Imaging.Print.PrintScaleMode.BestFit;
// subscribe to the PrintImage event
pdfPrintDocument.PrintImage +=
new System.EventHandler<Vintasoft.Imaging.Print.PrintImageEventArgs>(pdfPrintDocument_PrintImage);
// start print
pdfPrintDocument.Print();
// clear collection and dispose images
_printingPdfPages.ClearAndDisposeItems();
}
private void pdfPrintDocument_PrintImage(object sender, Vintasoft.Imaging.Print.PrintImageEventArgs e)
{
e.Image = _printingPdfPages[_printingPdfPageIndex];
_printingPdfPageIndex++;
if (_printingPdfPageIndex >= _printingPdfPages.Count)
{
Vintasoft.Imaging.Print.ImagePrintDocument imagePrintDocument =
(Vintasoft.Imaging.Print.ImagePrintDocument)sender;
// unsubscribe from the PrintImage event
imagePrintDocument.PrintImage -=
new System.EventHandler<Vintasoft.Imaging.Print.PrintImageEventArgs>(pdfPrintDocument_PrintImage);
// indicate that there is no more images to print
e.HasMoreImages = false;
}
else
{
// indicate that there are additional images to print
e.HasMoreImages = true;
}
}
''' <summary>
''' The printing PDF pages.
''' </summary>
Private _printingPdfPages As Vintasoft.Imaging.ImageCollection
''' <summary>
''' The index of printing PDF page.
''' </summary>
Private _printingPdfPageIndex As Integer
''' <summary>
''' Prints pages of the PDF document on default printer.
''' </summary>
''' <param name="pdfFilePath">Path to the PDF file.</param>
Public Sub PrintPdfPagesOnDefaultPrinter(pdfFilePath As String)
' create printing page collection
_printingPdfPages = New Vintasoft.Imaging.ImageCollection()
_printingPdfPages.Add(pdfFilePath)
_printingPdfPageIndex = 0
' create print manager
Dim pdfPrintDocument As New Vintasoft.Imaging.Pdf.Print.PdfPrintDocument()
' specify that each image must be resized to fit within the page margins,
' image proportions are not changed
pdfPrintDocument.PrintScaleMode = Vintasoft.Imaging.Print.PrintScaleMode.BestFit
' subscribe to the PrintImage event
AddHandler pdfPrintDocument.PrintImage, New System.EventHandler(Of Vintasoft.Imaging.Print.PrintImageEventArgs)(AddressOf pdfPrintDocument_PrintImage)
' start print
pdfPrintDocument.Print()
' clear collection and dispose images
_printingPdfPages.ClearAndDisposeItems()
End Sub
Private Sub pdfPrintDocument_PrintImage(sender As Object, e As Vintasoft.Imaging.Print.PrintImageEventArgs)
e.Image = _printingPdfPages(_printingPdfPageIndex)
_printingPdfPageIndex += 1
If _printingPdfPageIndex >= _printingPdfPages.Count Then
Dim imagePrintDocument As Vintasoft.Imaging.Print.ImagePrintDocument = DirectCast(sender, Vintasoft.Imaging.Print.ImagePrintDocument)
' unsubscribe from the PrintImage event
RemoveHandler imagePrintDocument.PrintImage, New System.EventHandler(Of Vintasoft.Imaging.Print.PrintImageEventArgs)(AddressOf pdfPrintDocument_PrintImage)
' indicate that there is no more images to print
e.HasMoreImages = False
Else
' indicate that there are additional images to print
e.HasMoreImages = True
End If
End Sub
Print PDF pages with Vintasoft annotations
If a collection of images intended for printing might include some PDF pages and images with annotation in VintaSoft format, created via
VintaSoftAnnotation.NET Plug-in, then it is recommended to use for printing the
AnnotatedPdfPrintDocument class instead of
AnnotatedImagePrintDocument class.