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

How to Print Documents to a Specific Printer

Environment

Product Version Product Author
2022.3.913 RadPrintDocument for WinForms Desislava Yordanova

Description

This article demonstrates how to print a document to a specific printer by using RadPrintDocument.

Solution

RadPdfViewer and RadRichTextEditor are two controls from the UI for WinForms suite that supports loading .pdf, .docx, .rtf and other document formats. No matter what type of document you want to print, the RadPrintDocument.AssociatedObject property allows you to specify the control (IPrintable) you want to print.

The following example uses a RadPdfViewer but it can be applied to RadRichTextEditor as well. The PrinterSettings object offers different settings for adjusting the print document and specifying the PrinterName:


private void radButton1_Click(object sender, EventArgs e)
{
    RadPdfViewer radPdfViewer = new RadPdfViewer();
    radPdfViewer.DocumentLoaded += radPdfViewer_DocumentLoaded;
    radPdfViewer.LoadDocument(@"..\..\..\Lorem ipsum dolor sit amet.pdf");
    radPdfViewer.LoadElementTree();

    Application.DoEvents();
}

private void radPdfViewer_DocumentLoaded(object sender, EventArgs e)
{
    using (RadPrintDocument document = new RadPrintDocument { Landscape = false })
    {
        document.AssociatedObject = (sender as RadPdfViewerElement);
        string printerName = "Microsoft Print to PDF";
        PrinterSettings settings = new System.Drawing.Printing.PrinterSettings
        {
            PrinterName = printerName,
            Copies = 1,
            Duplex = Duplex.Simplex,
            PrintRange = PrintRange.AllPages
        };
        document.DefaultPageSettings = new System.Drawing.Printing.PageSettings
        {
            PrinterSettings = settings
        };
        document.PrinterSettings = settings;
        document.Print();
    }
}