Introduction
Are you looking to print a PDF files programmatically using C# .NET platform? You have come to the right place in this article we will discuss how you can print PDF documents using C#.
First let us get to know how PDF printing works. Digital images and text are converted into tangible copies by printers. They achieve this by converting the file into a form that the printer can comprehend using a driver or software. A succession of tiny dots are then used to replicate the picture or text on the page.
IronPDF is .NET library that helps you print programmatically PDF document directly. Also IronPDF Provide a lot of advanced printing features like, IronPDF is capable of handling complex printing capabilities such as locating and setting the printer name, as well as changing the printer resolution.
IronPDF C# Library
In. NET and .NET core development, IronPDF is the ideal tool for printing PDF files. It not only prints PDF files, but it also has a lot of other useful functions. IronPDF allows developers to create, alter, and retrieve PDF documents from within .NET Core and framework projects. The IronPDF library allows developers to rapidly produce or alter PDFs printing.
With the use of Visual Basic or C# code, you may quickly print a PDF in .NET apps. This article will show you how to print programmatically using C# .NET print PDF capabilities. IronPDF provide many programmatic printing functionalities for printing pdf file like setting printer resolution, select specific printer.
Creating a New Visual Studio project
Go to the File menu in the Visual Studio program. Choose "new project," then "console application." In this topic, we'll produce PDF documents using a console program.
In the relevant text box, type the project name and choose the path. Then press the create button. As seen in the screenshot below, select the required .NET framework:
Now the new project has been created go ahead and integrate IronPDF library. In the next example you will find different ways to integrate IronPDF in Visual Studio.
Install IronPDF Library
IronPDF provides several options to integrate IronPDF into your Visual Studio project, however we will only cover two of them in this post.
1. Using the Visual Studio NuGet Package Manager
The NuGet Package Manager option in Visual Studio allows you to install the package directly into the solution. The screenshot below demonstrates how to access the Package Manager in solution explorer.
It has a search box that displays a list of NuGet package libraries. We need to look for the keyword "IronPDF" in the package manager, as seen below:
You will get a list of relevant packages search list from the image above. You must choose the IronPDF option and install the package.
2. Using the Visual Studio Command-Line
In Visual Studio menu, Go to Tools-> NuGet Package manager -> Package manager console
Enter the following line in the package manager console tab:
Install-Package IronPDF
Now the package will download/install to the current project and be ready to use
Print PDF Files
When it comes to print pdf files IronPDF provide two options, first send a PDF document directly to a printer, Secondly you can create an object sender that sent to GUI print dialogs. Below code snippet shows is the very quick demonstration how to print pdf files by using both options
Direct print pdf files (silent printing)
using IronPdf;
IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
using PdfDocument Pdf = Renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf");
Pdf.Print();
Using Object (print dialog)
using IronPdf;
IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
using PdfDocument Pdf = Renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf");
PdfDocument.GetPrintDocument
//Remember to add an assembly reference to System.Drawing.dll
System.Drawing.Printing.PrintDocument PrintDocYouCanWorkWith = Pdf.GetPrintDocument();
Advanced PDF document printing IronPDF
IronPDF can handle complex printing capabilities such as identifying or establishing the printer name, changing the printer resolution, printing PDF to file, and tracing printing processes.
Specify Printer Name
if you have multiple printer available to perform printing process IronPDF allow you to select specific printer for printing process. if you do not specify printer name then use the default printer to print
/**
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();
}
}
}
Printer Resolution
The number of pixels printed or shown, depending on the result, is referred to as resolution. Using the DefaultPageSettings.PrinterResolution option in IronPDF, you may change the resolution of your printing. below code snippet shows how to set printer resolution with just a few lines of code.
/**
Set Printer Resolution
anchor-set-printer-resolution
/
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
**Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
printDocument.Print();
}
}
}
printing PDF to File
The PDFDocument.PrintToFile function allows you to print a PDF to a file; simply provide the output filepath and indicate whether or not you want to get a preview.
/**
PrinttoFile
anchor-printtofile-method
**/
printDocument.PrintToFile(“PathToFile”, false);
Tracing Printing Processes
The benefit of using C# with IronPDF is that keeping track of printed pages, or anything printing-related, is actually extremely straightforward. In the following example, I will show how to trace the number of pages produced.
/**
Tracing Printing Processes
anchor-tracing-printing-processes-using-c-num
/
using IronPdf;
IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
using PdfDocument Pdf = Renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf");
PdfDocument.GetPrintDocument
**var printedPages = 0;
printDocument.PrintPage += (sender, args) => printedPages++;
printDocument.Print();
}
}
}
Conclusion
To print PDF document IronPDF is the perfect solution, it not only print pdf files but also give a lot of control in the printing process like print multiple PDF files or PDF pages, Print specific pages, Set printer resolution, print PDF document to file, and set printer track to track printed page numbers and many more other features. With the help of NuGet Package console you can easily integrate and manage IronPDF and easily print you PDF files. IronPDF provide free version for developers also provide many features to manipulate PDF file as you see fit. for additional information of C# print a PDF visit the following link.
You can download the software product from this link.
Top comments (0)