DEV Community

IronSoftware
IronSoftware

Posted on • Edited on • Originally published at ironpdf.com

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 programmatically.

How to Print PDF Files in C# by Following these Steps

  1. Download the Print to PDF C# Library
  2. Choose a PDF file from your computer
  3. Select specific printer to print and set resolution
  4. Check your PDF output from your printer
  5. Track your printing processes using C#

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:

C#:

/**
Create and Print PDF
anchor-create-a-pdf-and-print
**/
using IronPdf;
// Create a new PDF and print it
IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
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();
Enter fullscreen mode Exit fullscreen mode

VB:

'''
'''Create and Print PDF
'''anchor-create-a-pdf-and-print
'''*
Imports IronPdf
' Create a new PDF and print it
Private Renderer As New IronPdf.ChromePdfRenderer()
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()
Enter fullscreen mode Exit fullscreen mode

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:

C#:

/**
Specify Printer Name
anchor-specify-printer-name
**/
using (var ChromePdfRenderer = new ChromePdfRenderer())
{
using (var pdfDocument =
ChromePdfRenderer.RenderHtmlAsPdf(TestSources.HtmlTemplateBasicText()))
{
using (var printDocument = pdfDocument.GetPrintDocument())
{
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
printDocument.Print();
}
}
}
Enter fullscreen mode Exit fullscreen mode

VB:

'''
'''Specify Printer Name
'''anchor-specify-printer-name
'''*
Using ChromePdfRenderer As New ChromePdfRenderer()
Using pdfDocument = ChromePdfRenderer.RenderHtmlAsPdf(TestSources.HtmlTemplateBasicText())
Using printDocument = pdfDocument.GetPrintDocument()
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
printDocument.Print()
End Using
End Using
End Using
Enter fullscreen mode Exit fullscreen mode

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:

C#:

/**
Set Printer Resolution
anchor-set-printer-resolution
**/
using (var ChromePdfRenderer = new ChromePdfRenderer())
{
using (var pdfDocument =
ChromePdfRenderer.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();
}
}
}
Enter fullscreen mode Exit fullscreen mode

VB:

'''
'''Set Printer Resolution
'''anchor-set-printer-resolution
'''*
Using ChromePdfRenderer As New ChromePdfRenderer()
Using pdfDocument = ChromePdfRenderer.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
Enter fullscreen mode Exit fullscreen mode

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:
C#:

/**
PrinttoFile
anchor-printtofile-method
**/
printDocument.PrintToFile(“PathToFile”, false);
Enter fullscreen mode Exit fullscreen mode

VB:

'''
'''PrinttoFile
'''anchor-printtofile-method
'''*
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'printDocument.PrintToFile("PathToFile”, false);
Enter fullscreen mode Exit fullscreen mode

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.

C#:

/**
Tracing Printing Processes
anchor-tracing-printing-processes-using-c-num
**/
using (var ChromePdfRenderer = new ChromePdfRenderer())
{
using (var pdfDocument =
ChromePdfRenderer.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();**
}
}
}
Enter fullscreen mode Exit fullscreen mode

VB:

'''
'''Tracing Printing Processes
'''anchor-tracing-printing-processes-using-c-num
'''*
Using ChromePdfRenderer As New ChromePdfRenderer()
Using pdfDocument = ChromePdfRenderer.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
Enter fullscreen mode Exit fullscreen mode

Top comments (0)