C#/VB.NET: Print PDF Documents

Sending PDF to a physical printer is one of the most common tasks in our daily lives. For example, you may need to print contracts, invoices or resumes on paper so people are able to view them without the use of a device. This article shows you how to print PDF documents in C# and VB.NET using Spire.PDF for .NET with the following seven examples.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Print PDF with the Default Printer

The following are the steps to print PDF documents with the default printer in C# and VB.NET using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Call PdfDocument.Print() method to directly print the document with the default printer.
  • C#
  • VB.NET
using Spire.Pdf;

namespace PrintWithDefaultPrinter
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load a PDF file
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            //Print with default printer 
            doc.Print();
        }
    }
}

Print Selected Pages with a Specified Printer

The following are the steps to print PDF documents with a specified printer in C# and VB.NET using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Specify the printer name through the PrintSettings.PrinterName property.
  • Select discontinuous pages or continuous pages to print using PrintSettings.SelectSomePages() method or PrintSettings.SelectPageRange() method.
  • Call PdfDocument.Print() method to execute printing.
  • C#
  • VB.NET
using Spire.Pdf;

namespace PrintWithSpecifiedPrinter
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load a PDF file
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            //Specify printer name
            doc.PrintSettings.PrinterName = "HP LaserJet P1007";

            //Select a page range to print
            doc.PrintSettings.SelectPageRange(1, 5);

            //Select discontinuous pages to print
            //doc.PrintSettings.SelectSomePages(new int[] { 1, 3, 5, 7 });

            //Print document
            doc.Print();
        }
    }
}

Print PDF to XPS using Microsoft XPS Document Writer

The following are the steps to print PDF to XPS using Microsoft XPS Document Writer in C# and VB.NET.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Set the printer name to Microsoft XPS Document Writer through PrintSettings.PrinterName property.
  • Set the printed file’s path and name using PrintSettings.PrintToFile() method.
  • Execute printing using PdfDocument.Print() method.
  • C#
  • VB.NET
using Spire.Pdf;

namespace PrintPdfToXps
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load a PDF document
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            //Set printer name to Microsoft XPS Document Writer
            doc.PrintSettings.PrinterName = "Microsoft XPS Document Writer";

            //Set the printed file path and name
            doc.PrintSettings.PrintToFile("PrintToXps.xps");

            //Execute printing
            doc.Print();
        }
    }
}

Print PDF Silently

The following are the steps to silently print PDF documents in C# and VB.NET using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Specify the printer name through the PrintSettings.PrinterName property.
  • Set the value of PrintSettings.PrintController property to an instance of StandardPrintController class, which will prevent the printing process from displaying.
  • Execute printing using PdfDocument.Print() method.
  • C#
  • VB.NET
using Spire.Pdf;
using System.Drawing.Printing;

namespace PrintPdfSilently
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load a PDF file
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            //Specify printer name
            doc.PrintSettings.PrinterName = "HP LaserJet P1007";

            //Silent printing
            doc.PrintSettings.PrintController = new StandardPrintController();

            //Print document
            doc.Print();
        }
    }
}

Print PDF in Duplex Mode

The following are the steps to print PDF documents in duplex mode in C# and VB.NET using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Specify the printer name through the PrintSettings.PrinterName property.
  • Determine if the printer supports duplex printing through PrintSettings.CanDuplex property. If yes, set PrintSettings.Duplex property to Duplex.Defatult.
  • Execute printing using PdfDocument.Print() method.
  • C#
  • VB.NET
using Spire.Pdf;
using System.Drawing.Printing;

namespace PrintInDuplexMode
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load a PDF file
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            //Specify printer name
            doc.PrintSettings.PrinterName = "HP LaserJet P1007";

            //Determine if the printer supports duplex printing 
            if (doc.PrintSettings.CanDuplex)
            {
                //Set to duplex printing mode
                doc.PrintSettings.Duplex = Duplex.Default;

                //Print document
                doc.Print();
            }
        }
    }
}

Print PDF in Grayscale

The following are the steps to print PDF documents in grayscale in C# and VB.NET using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Specify the printer name through the PrintSettings.PrinterName property.
  • Set the PrintSettings.Color property to false, which will print a color PDF in black and white.
  • Execute printing using PdfDocument.Print() method.
  • C#
  • VB.NET
using Spire.Pdf;

namespace PrintInGrayscale
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load a PDF file
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            //Specify printer name
            doc.PrintSettings.PrinterName = "HP LaserJet P1007";

            //Print in black and white
            doc.PrintSettings.Color = false;

            //Print document
            doc.Print();
        }
    }
}

Print Different Page Ranges to Different Trays

The following are the steps to print different page ranges to different trays in C# and VB.NET using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Register the PrintSettings.PaperSettings event with a custom delegate which provides data for paper setting event.
  • Set the paper source of the tray 1 and tray 2.
  • Execute printing using PdfDocument.Print() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Print;

namespace PrintToDifferentTrays
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load a PDF file
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            //Register the PaperSettings event with a delegate that handles paper settings event
            doc.PrintSettings.PaperSettings += delegate (object sender, PdfPaperSettingsEventArgs e)
            {
                // Set the paper source of tray 1 to 1-10 pages
                if (1 <= e.CurrentPaper && e.CurrentPaper <= 10)
                {
                    e.CurrentPaperSource = e.PaperSources[0];
                }

                //Set the paper source of tray 2 to the rest pages
                else
                {
                    e.CurrentPaperSource = e.PaperSources[1];
                }
            };

            //Print document
            doc.Print();
        }
    }
}

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.