7 Ways to Print PDF in a .NET Application

Alexander Stock
4 min readMar 29, 2022

Adding the ability to print PDF documents to your .NET application is a cool idea, as PDF documents are a very common electronic document format and often need to be printed. In this article, I am going to introduce how to print PDF documents in C# using Free Spire.PDF for .NET from the following seven aspects.

  • Print PDF with the Default Printer
  • Print Selected Pages with a Specified Printer
  • Print PDF to XPS using Microsoft XPS Document Writer
  • Print PDF Silently
  • Print PDF in Duplex Mode
  • Print PDF in Grayscale
  • Print Different Page Ranges to Different Trays

Free Spire.PDF for .NET is a community edition of Spire.PDF for .NET. And it is limited to 10 pages when loading or creating a PDF document. If your document exceeds 10 pages, you could try Spire.PDF for .NET instead.

Add References

Method #1: Download Free Spire.PDF and unzip the package somewhere on your disk to find the “BIN” folder. Free Spire.PDF has the DLLs compiled for multiple versions of .NET Framework as well as for .NET Core and other platforms. Choose the DLLs from the folder that you exactly need and add them all as dependencies in your project.

Method #2: Create a .NET application in you Visual Studio, and install Free Spire.PDF directly through NuGet. NuGet package manager will automatically install the correct version that fits in with your application.

Prerequisite Knowledge

There are only two core classes involved when printing PDF documents using Spire.PDF, namely PdfDocument class and PrintSettings class. The PdfDocument class is used to load an existing PDF file from disk or from stream. The PrintSetting class is used to set the print settings, such as printer name, print range, and duplex printing. The following table lists the important classes, methods, and properties that you should learn about.

Print PDF with the Default Printer

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

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

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

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

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

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

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();
}
}
}

--

--

Alexander Stock

I'm Alexander Stock, a software development consultant and blogger with 10+ years' experience. Specializing in office document tools and knowledge introduction.