| layout | post |
|---|---|
| title | Printing PDF Files in WPF Pdf Viewer control | Syncfusion |
| description | Learn about Printing PDF Files support in Syncfusion<sup>®</sup>; Essential Studio® WPF Pdf Viewer control, its elements and more. |
| platform | document-processing |
| control | PDF Viewer |
| documentation | ug |
PDF Viewer allows printing loaded PDFs using the Print button in the toolbar. The following Print dialog opens upon triggering the Print button.
N> PdfDocumentView is used to view the PDF documents without the toolbar. So, make use of PdfViewerControl to print the document using Print button in the toolbar.
The Print method of PdfViewerControl and PdfDocumentView allows you to print PDF files silently to the system's default printer, without any user interaction. You can enable the preferred settings for silent printing using the PrinterSettings property. The following code example shows how to perform silent printing in WPF PDF Viewer.
{% tabs %} {%highlight c#%}
pdfviewer1.Print();
{%endhighlight%}
{%highlight vb%}
pdfviewer1.Print()
{%endhighlight%} {% endtabs %}
The Print method of PdfViewerControl and PdfDocumentView allows you to print PDF files silently to the specified printer, without any user interaction. You can enable the preferred settings for silent printing using the PrinterSettings property. The following code example shows how to perform silent printing with a specified printer name in WPF PDF Viewer.
{% tabs %} {%highlight c#%}
string printerName = "enter the printer name"; pdfviewer1.Print(printerName);
{%endhighlight%}
{%highlight vb%}
Dim printerName As String = "enter the printer name" pdfviewer1.Print(printerName)
{%endhighlight%} {% endtabs %}
PDF viewer printer settings allows scaling PDF pages to shrink or enlarge while printing.
Actual size is the default value of print size option in printer settings. This prints the loaded PDF document without any scaling factors. The pages that do not fit on the paper will be cropped. The following code example illustrates how to print the document in Actual Size.
{% tabs %} {% highlight c# %}
// Prints the document in actual size.
pdfviewer1.PrinterSettings.PageSize = PdfViewerPrintSize.ActualSize;
{% endhighlight %}
{% highlight vbnet %}
' Prints the document in actual size.
pdfviewer1.PrinterSettings.PageSize = PdfViewerPrintSize.ActualSize
{% endhighlight %} {% endtabs %}
Fit option enlarges or reduces each page to fit the printable area of the selected paper size. The following code example illustrates the same.
{% tabs %} {% highlight c# %}
// Prints the document in fit size.
pdfviewer1.PrinterSettings.PageSize = PdfViewerPrintSize.Fit;
{% endhighlight %}
{% highlight vbnet %}
' Prints the document in fit size.
pdfviewer1.PrinterSettings.PageSize = PdfViewerPrintSize.Fit
{% endhighlight %} {% endtabs %}
Custom Scale option resizes the page with the specified scale percentage. The following code example illustrates the same.
{% tabs %} {% highlight c# %}
// Prints the document with custom scaling.
pdfviewer1.PrinterSettings.PageSize = PdfViewerPrintSize.CustomScale;
// Scale percentage with the page to be resized and it is applicable only for Custom Scale. The default value is 100.
pdfviewer1.PrinterSettings.ScalePercentage = 120;
{% endhighlight %}
{% highlight vbnet %}
' Prints the document with custom scaling.
pdfviewer1.PrinterSettings.PageSize = PdfViewerPrintSize.CustomScale
' Scale percentage with the page to be resized and it is applicable only for Custom Scale. The default value is 100.
pdfviewer1.PrinterSettings.ScalePercentage = 120
{% endhighlight %} {% endtabs %}
PDF Viewer printer settings allows the user to print the document with a custom orientation.
Auto Portrait/Landscape is the default option and it automatically selects the best orientation (Portrait or Landscape) based on the content and selected paper. The following code example illustrates how to print the document in Auto orientation.
{% tabs %} {% highlight c# %}
// Prints the document in auto orientation.
pdfviewer1.PrinterSettings.PageOrientation = PdfViewerPrintOrientation.Auto;
{% endhighlight %}
{% highlight vbnet %}
' Prints the document in auto orientation.
pdfviewer1.PrinterSettings.PageOrientation = PdfViewerPrintOrientation.Auto
{% endhighlight %} {% endtabs %}
Portrait option prints the PDF document in portrait orientation and it overrides the orientation settings provided in the print dialog. The following code example illustrates the same.
{% tabs %} {% highlight c# %}
// Prints the document in portrait orientation.
pdfviewer1.PrinterSettings.PageOrientation = PdfViewerPrintOrientation.Portrait;
{% endhighlight %}
{% highlight vbnet %}
' Prints the document in portrait orientation.
pdfviewer1.PrinterSettings.PageOrientation = PdfViewerPrintOrientation.Portrait
{% endhighlight %} {% endtabs %}
Landscape option prints the PDF document in landscape orientation and it overrides the orientation settings provided in print dialog. The following code example illustrates the same.
{% tabs %} {% highlight c# %}
// Prints the document in landscape orientation.
pdfviewer1.PrinterSettings.PageOrientation = PdfViewerPrintOrientation.Landscape;
{% endhighlight %}
{% highlight vbnet %}
' Prints the document in landscape orientation.
pdfviewer1.PrinterSettings.PageOrientation = PdfViewerPrintOrientation.Landscape
{% endhighlight %} {% endtabs %}
PDF viewer allows you to set the custom document name to be displayed when printing the PDF document using the DocumentName property that is available in the PrinterSettings. You can either set the original file name or a custom text to the property for display. Setting the DocumentName property will affect the Document name details in the print status dialog.
The DocumentName property will also affect the Document name details in the print queue information window.
The following code shows how to set the original file name to the DocumentName property.
{% tabs %} {% highlight c# %}
using System.IO; using System.Windows;
namespace PdfViewer { ///
//Print the file.
pdfViewer.Print();
}
#endregion
}
}
{% endhighlight %} {% endtabs %}
PDF Viewer allows you to hide the following print status dialog when printing the PDF files by setting the value false to the ShowPrintStatusDialog property that is available in the PrinterSettings.
It will be helpful if no UI interactions are required when printing. The following code shows how to hide the print status dialog using the ShowPrintStatusDialog property.
{% tabs %} {% highlight c# %}
using System.IO; using System.Windows;
namespace PdfViewer { ///
//Print the file.
pdfViewer.Print();
}
#endregion
}
}
{% endhighlight %} {% endtabs %}
You can customize PaperSource in silent printing by changing the PaperSource property in PageSettings and passing it as a parameter with printer name to the Print API.
N> The System.Drawing assembly is required.
The following code example illustrates how to set PaperSource for Your printer. In the PageSettings, pass the required PaperSource value.
{% tabs %} {% highlight c# %}
PrinterSettings printerSettings = new PrinterSettings(); PageSettings pageSettings = new PageSettings();
private void Button_Click(object sender, RoutedEventArgs e) { //Enter Your Printer’s Name printerSettings.PrinterName = "Mention Your printer's name here"; //Changes the papersource in pagesettings pageSettings.PaperSource = printerSettings.PaperSources[3];
//Prints the PDF with required papersource
pdfviewer.Print(printerSettings.PrinterName, pageSettings);
}
{% endhighlight %} {% endtabs %}
The PdfViewerControl notifies you at the start, progress, and end of the printing through events such as BeginPrint, PrintProgress, and EndPrint, respectively.
The BeginPrint event occurs when the print is called and before the first page of the document prints. The following code shows how to wire the event in PdfViewerControl.
{% tabs %} {% highlight c# %}
using System.Windows; using Syncfusion.Windows.PdfViewer;
namespace WPF_PDFViewer { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); //Wire the BeginPrint event pdfViewer.BeginPrint += pdfViewer_BeginPrint; //Load the PDF file pdfViewer.Load("F Sharp Succinctly.pdf"); //Print the PDF silently to the default printer. pdfViewer.Print(true); } //Handle the BeginPrint Event private void pdfViewer_BeginPrint(object sender, BeginPrintEventArgs args) { //Insert Your code Here. } } }
{% endhighlight %} {% endtabs %}
The PrintProgress event provides information on how much printing content you submitted to the printing subsystem. It provides the page information through the PrintProgressEventArgs. The following code shows how to wire the event in PdfViewerControl.
{% tabs %} {% highlight c# %}
using System.Windows; using Syncfusion.Windows.PdfViewer;
namespace WPF_PDFViewer { ///
{% endhighlight %} {% endtabs %}
The EndPrint event occurs when you have printed the last page of the document. It also happens if you cancel the printing process or an exception occurs during the printing process. The following code shows how to wire the event in PdfViewerControl.
{% tabs %} {% highlight c# %}
using System.Windows; using Syncfusion.Windows.PdfViewer;
namespace WPF_PDFViewer { ///
{% endhighlight %} {% endtabs %}
N> You can refer to our WPF PDF Viewer feature tour page for its groundbreaking feature representations. You can also explore our WPF PDF Viewer example to know how to render and configure the pdfviewer.


