New to Telerik UI for WinForms? Download free 30-day trial

PDF Silent Print

Environment

Product Version 2018.1 220
Product RadPdfViewer for WinForms

Problem

You need to print a PDF document without showing it to the user.

Solution

You need to use the DocumentLoaded event because the document is loaded in a separate thread. Once the document is loaded you can call the Print method. The following snippet shows how you can print the document.

PDF Silent Print

private void radButton1_Click(object sender, EventArgs e)
{
    RadPdfViewer rViewer = new RadPdfViewer();
    rViewer.DocumentLoaded += RViewer_DocumentLoaded;
    rViewer.LoadDocument(@"C:\Book1.pdf");
    rViewer.LoadElementTree();
    Application.DoEvents();
}

private static void RViewer_DocumentLoaded(object sender, EventArgs e)
{

    var radPdfViewerElement = (RadPdfViewerElement)sender;
    radPdfViewerElement.PrintScalePageToPaperSize = true;

    RadPrintDocument document = new RadPrintDocument();
    document.Landscape = false;
    document.Margins = new Margins(0, 0, 0, 0);
    document.DefaultPageSettings.PrinterSettings.Copies = 1;
    document.AssociatedObject = radPdfViewerElement;

    document.Print();
}

Private Sub radButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim rViewer As New RadPdfViewer()
    AddHandler rViewer.DocumentLoaded, AddressOf RViewer_DocumentLoaded
    rViewer.LoadDocument("C:\Book1.pdf")
    rViewer.LoadElementTree()
    Application.DoEvents()
End Sub

Private Shared Sub RViewer_DocumentLoaded(ByVal sender As Object, ByVal e As EventArgs)
    Dim radPdfViewerElement = CType(sender, RadPdfViewerElement)
    radPdfViewerElement.PrintScalePageToPaperSize = True

    Dim document As New RadPrintDocument()
    document.Landscape = False
    document.Margins = New Margins(0, 0, 0, 0)
    document.DefaultPageSettings.PrinterSettings.Copies = 1
    document.AssociatedObject = radPdfViewerElement

    document.Print()
End Sub

If you don't assign the RadPrintDocument.PrintController property, then you get a default print controller. An instance of PrintControllerWithStatusDialog which displays the progress dialog, counting pages. If you don't want to show the small printing dialog counting the pages, you can initialize the RadPrintDocument.PrintController property to a new System.Drawing.Printing.StandardPrintController instance.

Assembly References

If you add the RadPdfViewer at run time you need to add references to the following assemblies:

  • Telerik.WinControls.PdfViewer
  • Telerik.WinControls
  • Telerik.WinControls.UI
  • TelerikCommon
  • Telerik.Windows.Documents.Fixed
  • Telerik.Windows.Documents.Core
  • Telerik.Windows.Zip.dll

See Also

In this article