C# Print PDF Documents

You can easily print a PDF in .NET applications with the help of Visual Basic or C# code. This tutorial will walk you through how to use C# print PDF capabilities to print programatically.

Create and Print PDF
using IronPdf;

// Create a new PDF and print it

IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();

PdfDocument Pdf = Renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf");

// Send the PDF to the default printer to print

Pdf.Print();

//For advanced silent real-world printing options, use PdfDocument.GetPrintDocument

//Remember to add an assembly reference to System.Drawing.dll

 System.Drawing.Printing.PrintDocument PrintDocYouCanWorkWith = Pdf.GetPrintDocument();
Imports IronPdf

' Create a new PDF and print it

Private Renderer As New IronPdf.HtmlToPdf()

Private Pdf As PdfDocument = Renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf")

' Send the PDF to the default printer to print

Pdf.Print()

'For advanced silent real-world printing options, use PdfDocument.GetPrintDocument

'Remember to add an assembly reference to System.Drawing.dll

 Dim PrintDocYouCanWorkWith As System.Drawing.Printing.PrintDocument = Pdf.GetPrintDocument()
Jump to Article
Specify Printer Name
using (var htmlToPdf = new HtmlToPdf())

{

using (var pdfDocument =

htmlToPdf.RenderHtmlAsPdf(TestSources.HtmlTemplateBasicText()))

{

using (var printDocument = pdfDocument.GetPrintDocument())

{

printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";

printDocument.Print();

}

}

}
Using htmlToPdf As New HtmlToPdf()


Using pdfDocument = htmlToPdf.RenderHtmlAsPdf(TestSources.HtmlTemplateBasicText())


Using printDocument = pdfDocument.GetPrintDocument()


printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"

printDocument.Print()

End Using

End Using

End Using
Jump to Article
Set Printer Resolution
using (var htmlToPdf = new HtmlToPdf())

{

using (var pdfDocument =

htmlToPdf.RenderHtmlAsPdf(TestSources.HtmlTemplateBasicText()))

{

using (var printDocument = pdfDocument.GetPrintDocument())

{

printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";

printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution

{

**Kind = PrinterResolutionKind.Custom,**

**X = 1200,**

**Y = 1200**

**};**

printDocument.Print();

}

}

}
Using htmlToPdf As New HtmlToPdf()


Using pdfDocument = htmlToPdf.RenderHtmlAsPdf(TestSources.HtmlTemplateBasicText())


Using printDocument = pdfDocument.GetPrintDocument()


printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"

printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
	**Kind = PrinterResolutionKind.Custom,
	** **X = 1200,
	** **Y = 1200** **
}
** printDocument.Print()

End Using

End Using

End Using
Jump to Article
PrinttoFile
printDocument.PrintToFile(“PathToFile”, false);
printDocument.PrintToFile("PathToFile", False)
Jump to Article
Tracing Printing Processes
using (var htmlToPdf = new HtmlToPdf())

{

using (var pdfDocument =

htmlToPdf.RenderHtmlAsPdf(TestSources.HtmlTemplateBasicText()))

{

using (var printDocument = pdfDocument.GetPrintDocument())

{

printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";

printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution

{

Kind = PrinterResolutionKind.Custom,

X = 1200,

Y = 1200

};

**var printedPages = 0;**

**printDocument.PrintPage += (sender, args) => printedPages++;**

**printDocument.Print();**

}

}

}
Using htmlToPdf As New HtmlToPdf()


Using pdfDocument = htmlToPdf.RenderHtmlAsPdf(TestSources.HtmlTemplateBasicText())


Using printDocument = pdfDocument.GetPrintDocument()


printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"

printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
	.Kind = PrinterResolutionKind.Custom,
	.X = 1200,
	.Y = 1200
}

**var printedPages = 0
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: ** **printDocument.PrintPage += (sender, args) => printedPages++;
AddHandler ** **printDocument.PrintPage, Sub(sender, args) printedPages
printedPages += 1
** **printDocument.Print()
**

End Using

End Using

End Using
Jump to Article
Try IronPDF free for development


Step 1

1. Install IronPDF for C# Print to PDF

Step 1 - Install the IronPDF C# PDF Library for all the programmatic printing functionalities you'll see in this tutorial. Download the DLL free for development, otherwise you can also install with NuGet and open the library in your Visual Studio project.


 PM > Install-Package IronPdf


How to Tutorial

2. Create a PDF and Print

You can send a PDF document directly to a printer silently or create a System.Drawing.Printing.PrintDocument object, which can be worked with and sent to GUI print dialogs.

The following code can be used for both options:

using IronPdf;

// Create a new PDF and print it

IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();

PdfDocument Pdf = Renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf");

// Send the PDF to the default printer to print

Pdf.Print();

//For advanced silent real-world printing options, use PdfDocument.GetPrintDocument

//Remember to add an assembly reference to System.Drawing.dll

 System.Drawing.Printing.PrintDocument PrintDocYouCanWorkWith = Pdf.GetPrintDocument();
Imports IronPdf

' Create a new PDF and print it

Private Renderer As New IronPdf.HtmlToPdf()

Private Pdf As PdfDocument = Renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf")

' Send the PDF to the default printer to print

Pdf.Print()

'For advanced silent real-world printing options, use PdfDocument.GetPrintDocument

'Remember to add an assembly reference to System.Drawing.dll

 Dim PrintDocYouCanWorkWith As System.Drawing.Printing.PrintDocument = Pdf.GetPrintDocument()
VB   C#

This is the easiest method for using a C# PDF printer.


3. Advanced Printing

IronPDF is quite capable of dealing with advanced Printing features such as finding the printer name or setting it and setting the Printer resolution.

4. Specify Printer Name

To specify the printer name, all you need to do is to get the current print document object (with the use of the GetPrintDocument method of the PDF document), then use the PrinterSettings.PrinterName property, as follows:

using (var htmlToPdf = new HtmlToPdf())

{

using (var pdfDocument =

htmlToPdf.RenderHtmlAsPdf(TestSources.HtmlTemplateBasicText()))

{

using (var printDocument = pdfDocument.GetPrintDocument())

{

printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";

printDocument.Print();

}

}

}
Using htmlToPdf As New HtmlToPdf()


Using pdfDocument = htmlToPdf.RenderHtmlAsPdf(TestSources.HtmlTemplateBasicText())


Using printDocument = pdfDocument.GetPrintDocument()


printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"

printDocument.Print()

End Using

End Using

End Using
VB   C#


5. Set Printer Resolution

Resolution refers to the number of pixels being printed, or displayed, depending on your output. You can set the resolution of your printing through IronPDF by making use of the DefaultPageSettings.PrinterResolution property of the PDF document. Here is a very quick demonstration:

using (var htmlToPdf = new HtmlToPdf())

{

using (var pdfDocument =

htmlToPdf.RenderHtmlAsPdf(TestSources.HtmlTemplateBasicText()))

{

using (var printDocument = pdfDocument.GetPrintDocument())

{

printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";

printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution

{

**Kind = PrinterResolutionKind.Custom,**

**X = 1200,**

**Y = 1200**

**};**

printDocument.Print();

}

}

}
Using htmlToPdf As New HtmlToPdf()


Using pdfDocument = htmlToPdf.RenderHtmlAsPdf(TestSources.HtmlTemplateBasicText())


Using printDocument = pdfDocument.GetPrintDocument()


printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"

printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
	**Kind = PrinterResolutionKind.Custom,
	** **X = 1200,
	** **Y = 1200** **
}
** printDocument.Print()

End Using

End Using

End Using
VB   C#

As you can see, I have set the resolution to a custom level: 1200 vertical and 1200 horizontal.


6. PrintToFile Method

The PDFDocument.PrintToFile method allows you to print the PDF to a file, you simply supply the output filepath and specify whether or not you’d like to see a preview, for example:

printDocument.PrintToFile(“PathToFile”, false);
printDocument.PrintToFile("PathToFile", False)
VB   C#

The above code will print to the file you have specified, and not include a preview.


7. Tracing Printing Processes using C#

The beauty of C# in conjunction with IronPDF is that when it comes to keeping track of printed pages, or anything printing related, it is actually quite simple. In the next example I will demonstrate how to change the printer’s name, resolution as well as how to get a count of pages that was printed.

using (var htmlToPdf = new HtmlToPdf())

{

using (var pdfDocument =

htmlToPdf.RenderHtmlAsPdf(TestSources.HtmlTemplateBasicText()))

{

using (var printDocument = pdfDocument.GetPrintDocument())

{

printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";

printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution

{

Kind = PrinterResolutionKind.Custom,

X = 1200,

Y = 1200

};

**var printedPages = 0;**

**printDocument.PrintPage += (sender, args) => printedPages++;**

**printDocument.Print();**

}

}

}
Using htmlToPdf As New HtmlToPdf()


Using pdfDocument = htmlToPdf.RenderHtmlAsPdf(TestSources.HtmlTemplateBasicText())


Using printDocument = pdfDocument.GetPrintDocument()


printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"

printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
	.Kind = PrinterResolutionKind.Custom,
	.X = 1200,
	.Y = 1200
}

**var printedPages = 0
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: ** **printDocument.PrintPage += (sender, args) => printedPages++;
AddHandler ** **printDocument.PrintPage, Sub(sender, args) printedPages
printedPages += 1
** **printDocument.Print()
**

End Using

End Using

End Using
VB   C#