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 Step…


This content originally appeared on DEV Community and was authored by IronSoftware

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

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

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

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

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

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

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

VB:

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

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

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


This content originally appeared on DEV Community and was authored by IronSoftware


Print Share Comment Cite Upload Translate Updates
APA

IronSoftware | Sciencx (2022-01-19T08:26:56+00:00) C# Print PDF Documents. Retrieved from https://www.scien.cx/2022/01/19/c-print-pdf-documents/

MLA
" » C# Print PDF Documents." IronSoftware | Sciencx - Wednesday January 19, 2022, https://www.scien.cx/2022/01/19/c-print-pdf-documents/
HARVARD
IronSoftware | Sciencx Wednesday January 19, 2022 » C# Print PDF Documents., viewed ,<https://www.scien.cx/2022/01/19/c-print-pdf-documents/>
VANCOUVER
IronSoftware | Sciencx - » C# Print PDF Documents. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/19/c-print-pdf-documents/
CHICAGO
" » C# Print PDF Documents." IronSoftware | Sciencx - Accessed . https://www.scien.cx/2022/01/19/c-print-pdf-documents/
IEEE
" » C# Print PDF Documents." IronSoftware | Sciencx [Online]. Available: https://www.scien.cx/2022/01/19/c-print-pdf-documents/. [Accessed: ]
rf:citation
» C# Print PDF Documents | IronSoftware | Sciencx | https://www.scien.cx/2022/01/19/c-print-pdf-documents/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.