Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
The Portable Document Format (PDF), sometimes referred to as ISO 32000, was created by Adobe in 1992 and is a file format that enables the presentation of documents with text formatting and graphics without being dependent on operating systems, hardware, or application software. A PDF file is an explanation of a flat document with a defined layout that includes all the text, fonts, raster images, vector graphics, and other data needed for it to be shown. It is built on top of PostScript.
Automating the printing process by sending a PDF to a printer from .NET C# code saves human labor, ensures consistency in the creation of PDF files, and lets you incorporate printing capabilities into your apps. It offers fine control over the process of printing.
In this article, we are going to print a PDF file in the C# Windows application.
Developers of .NET C# applications can use IronPrint, a robust C# printing library, to help them incorporate printing features. IronPrint is a dependable solution for document printing, regardless of whether you're developing desktop, mobile, or web applications.
For applications that need smooth document output, IronPrint is a vital tool since it gives .NET developers exact control over printing. Investigate IronPrint to enhance your program with effective document printing. To know more about IronPrint refer to this documentation page.
Open the Visual Studio application and click on the File menu. Then select "New Project", next select "Window Forms App (.NET Framework)" in C#.
After selecting the project location, specify the project name into the assigned text field. Next, select the necessary .NET Framework, then click on the Create button, as demonstrated in the sample below.
Next, how the Visual Studio project is organized will depend on which application is chosen. Simply open Form1.cs file to begin adding code and building the Windows Forms application.
The code can then be tested and the library added.
Utilizing the Visual Studio Tool From the Tools Menu, choose NuGet Package Manager. To view the package management terminal console, navigate to the Package Manager interface.
Install-Package IronPrint
The package can now be used in the ongoing project after being downloaded and installed.
Another option is to use the NuGet Package Manager for Solutions approach. With Visual Studio, you may use the NuGet Package Manager to install the package directly into the solution. The image below illustrates how to open the NuGet Package Manager.
Use the search box on the NuGet website to find packages. Simply search for "IronPrint" in the package manager, as shown in the screenshot below.
The accompanying image shows a list of related search results. Please make these changes in order for the NuGet IronPrint library to be installed on your computer.
Printing a file is made easy with the help of the IronPrint library. The first step is to design the Windows form by adding two buttons in the default Windows form which is created while creating the project. The first button is to select the PDF document that we need to print. The second button is to trigger the print PDF documents.
In this example, we are going to print PDF files with a few lines of code.
using System;
using System.Windows.Forms;
using IronPrint;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
// Constructor to initialize the form
public Form1()
{
InitializeComponent();
}
// Event handler for the first button click to open file dialog
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog(this);
}
// Event handler for the second button click to print the selected PDF
private void button2_Click(object sender, EventArgs e)
{
// Print the file silently with default settings
Printer.Print(openFileDialog1.FileName);
// Alternative: Configure print settings before printing
PrintSettings printSettings = new PrintSettings
{
Dpi = 150,
NumberOfCopies = 2,
PaperOrientation = PaperOrientation.Portrait
};
// Print with custom settings
Printer.Print(openFileDialog1.FileName, printSettings);
// Alternative: Show print dialog
Printer.ShowPrintDialog(openFileDialog1.FileName);
}
}
}
using System;
using System.Windows.Forms;
using IronPrint;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
// Constructor to initialize the form
public Form1()
{
InitializeComponent();
}
// Event handler for the first button click to open file dialog
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog(this);
}
// Event handler for the second button click to print the selected PDF
private void button2_Click(object sender, EventArgs e)
{
// Print the file silently with default settings
Printer.Print(openFileDialog1.FileName);
// Alternative: Configure print settings before printing
PrintSettings printSettings = new PrintSettings
{
Dpi = 150,
NumberOfCopies = 2,
PaperOrientation = PaperOrientation.Portrait
};
// Print with custom settings
Printer.Print(openFileDialog1.FileName, printSettings);
// Alternative: Show print dialog
Printer.ShowPrintDialog(openFileDialog1.FileName);
}
}
}
Imports System
Imports System.Windows.Forms
Imports IronPrint
Namespace WindowsFormsApp1
Partial Public Class Form1
Inherits Form
' Constructor to initialize the form
Public Sub New()
InitializeComponent()
End Sub
' Event handler for the first button click to open file dialog
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
openFileDialog1.ShowDialog(Me)
End Sub
' Event handler for the second button click to print the selected PDF
Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
' Print the file silently with default settings
Printer.Print(openFileDialog1.FileName)
' Alternative: Configure print settings before printing
Dim printSettings As New PrintSettings With {
.Dpi = 150,
.NumberOfCopies = 2,
.PaperOrientation = PaperOrientation.Portrait
}
' Print with custom settings
Printer.Print(openFileDialog1.FileName, printSettings)
' Alternative: Show print dialog
Printer.ShowPrintDialog(openFileDialog1.FileName)
End Sub
End Class
End Namespace
In the above code example to use the IronPrint library, first we import it into the code using using IronPrint;
. We then help users select the PDF document which is available on the local drive via the OpenFileDialog
control by clicking the "Select a file" button. After selecting the PDF file, the application waits for the user to click the Print button. When the Print button is clicked, we pass the input PDF file into the Print method which is available in the IronPrint library's Printer class.
The Print Method allows us to print the PDF file silently without opening any print dialog. After passing the file name in the Print method, it loads the printing PDF files into the object and sends the file to the default printer. Now the printer will print the PDF documents. The print object allows us to pass two types of parameters: one is the filename or file byte array for printing PDF files using the default print settings, and the second is the PrintSetting parameter, in which we are able to specify the printer settings such as Page size, Paper Orientation as Portrait or Landscape orientation, Printer name, Paper Margin, print multiple copies using NumberOfCopies setting, etc.
If we don't want to print the document silently, we can print PDF files using another method called ShowPrintDialog which opens the print dialog menu and allows us to select the printer options. To learn more about the IronPrint code, please refer to the code examples page.
In summary, IronPrint is a testament to the power of accessibility and knowledge sharing in the digital era. IronPrint is an invaluable resource for scholars, hobbyists, and students alike, with its extensive collection of printed works covering a wide range of topics, genres, and languages. Through the adoption of technology and the digitization of its collections, IronPrint has made these invaluable resources accessible to a worldwide audience, dismantling informational boundaries and promoting learning and exploration on a scale never before achievable. A beacon of enlightenment, IronPrint preserves the past, enhances the present, and encourages future generations to discover the glories of human creativity and knowledge even as society changes.
The cost-effective development edition of IronPrint is available for free trial to find out more about the price. To know more about other Iron Software products, please check their website.
IronPrint is a robust C# printing library that helps developers incorporate printing features into their .NET applications. It supports multiple platforms including Windows, macOS, Android, and iOS.
You can install IronPrint in a C# project using the NuGet Package Manager in Visual Studio. Use the command 'Install-Package IronPrint' in the Package Manager Console or find it through the NuGet Package Manager interface.
IronPrint can handle various file formats for printing, including PDF, PNG, HTML, TIFF, GIF, JPEG, and BITMAP.
To print a PDF file using IronPrint, you need to create a Windows Forms application, add buttons for selecting and printing files, and use the Printer.Print method from the IronPrint library to send files to the printer.
Yes, IronPrint can print documents automatically without showing a print dialog, which is ideal for background jobs or batch processing.
Yes, IronPrint allows customization of print settings such as the number of copies, paper size, orientation, and DPI through the PrintSettings class.
IronPrint provides .NET developers with exact control over printing, offering a streamlined API, asynchronous printing, and compatibility across multiple platforms, making it essential for applications that need smooth document output.
Yes, a cost-effective development edition of IronPrint is available for free trial. More information can be found on the Iron Software website.
To create a new project in Visual Studio, open the application, navigate to the File menu, select 'New Project', choose 'Window Forms App (.NET Framework)' in C#, and configure your project settings.
You can find more code examples and documentation for IronPrint on the Iron Software website, which provides detailed guides and examples for various use cases.