Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 19, 2022 08:26 am GMT

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 PDFanchor-create-a-pdf-and-print**/using IronPdf;// Create a new PDF and print itIronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();PdfDocument Pdf = Renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf");// Send the PDF to the default printer to printPdf.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 itPrivate 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 printPdf.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 Nameanchor-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 UsingEnd UsingEnd 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 Resolutionanchor-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 UsingEnd UsingEnd 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 youd like to see a preview, for example:
C#:

/**PrinttoFileanchor-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 printers name, resolution as well as how to get a count of pages that was printed.

C#:

/**Tracing Printing Processesanchor-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) printedPagesprintedPages += 1** **printDocument.Print()**End UsingEnd UsingEnd Using

Original Link: https://dev.to/ironsoftware/c-print-pdf-documents-241m

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To