Print PDF files without showing print dialog in WPF

When you need to print many PDF documents, surely you don't want to see the print dialog every time. This article will show you clearly how to print PDF documents in WPF without invoking Print Dialog by using Spire.PDFViewer for WPF.

Here comes to the steps of how to print PDF files in WPF.

Step 1: First you need to create a new project by choosing "WPF Application".

Step 2: Set the Target Framework to be .NET Framework 4 in Properties.

Step 3: Right-click on the blank part of the Toolbox → "Add Tab" → "Choose Items" → "WPF Components" → "Browse" to the "Bin" folder → find the file "Spire.PdfViewer.Wpf.dll" → "OK".

Step4: Spire.PDFViewer offers PdfViewer and PdfDocumentViewer to print the PDF files in C#. Please check the code snippet as below:

Print via PdfViewer without invoking PrintDialog with the following method.

PrintDialog dialog = new PrintDialog();
this.pdfViewer1.PrintDialog = dialog;
dialog.PrintDocument(pdfViewer1.PrintDocument.DocumentPaginator, "Print Document");

Print via PdfDocumentViewer without invoking PrintDialog with the following method.

PrintDialog dialog = new PrintDialog();
this.pdfDocumentViewer1.PrintDialog = dialog;
dialog.PrintDocument(pdfDocumentViewer1.PrintDocument.DocumentPaginator, "Print Document");

Then your PDF document will be printed directly without showing the print dialog.

Full codes of how to print PDF file in WPF.

namespace PrintPDFinWPF
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.pdfViewer1.LoadFromFile("sample.pdf");
            ////Print the PDF file directly with PrintDialog
            //this.pdfViewer1.Print();

            //Print without Print Dialog
            PrintDialog dialog = new PrintDialog();
            this.pdfViewer1.PrintDialog = dialog;
            dialog.PrintDocument(pdfViewer1.PrintDocument.DocumentPaginator, "Print Document");
        }
    }
}