USING IRONPRINT

How to Print PDF files in a C# Windows application

Updated March 27, 2024
Share:

Introduction

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.

How to Print PDF files in a C# Windows application

  1. Create a new Windows Project.
  2. Install the IronPrint library from NuGet.
  3. Import the Library.
  4. Write the code to import the PDF files.
  5. Implement the Logic and handle the exception.
  6. Print the files.

IronPrint

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.

Features of IronPrint

  • IronPrint is compatible with Windows, macOS, Android, and iOS and operates without a hitch on any of them. IronPrint guarantees reliable printing results whether you're aiming for web apps, mobile apps, or desktop software.
  • Documents in the following formats can be printed using IronPrint: PDF, PNG, HTML, TIFF, GIF, JPEG, and BITMAP.
  • You can print documents straight from your application code with IronPrint. Printing functionality can be easily integrated for invoices, reports, and labels.
  • Print automatically without posing a dialog. Perfect for background jobs or batch processing where user participation is not necessary.
  • Adjust parameters like the number of copies, paper size, orientation, and DPI. IronPrint gives developers the ability to customize the printing procedure to meet certain needs.
  • IronPrint provides certain functions related to printing through classes and methods. For developers, a streamlined API is ensured by precise and comprehensive print settings.
  • IronPrint offers asynchronous printing, more platform compatibility, and improved printing functionality.

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.

Creating a New Project in Visual Studio

Open the Visual Studio application and click on the File menu. Then select "New Project", next select "Window Forms App (.NET Framework)" in C#.

How to Print PDF files in a C# Windows application: Figure 1 - Open Visual Studio and create a new C# Windows Form App project in .NET Framework.

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.

How to Print PDF files in a C# Windows application: Figure 2 - Select the Project name and location, next select the appropriate .NET Framework version and click on the Create button.

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.

Install IronPrint library

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.

How to Print PDF files in a C# Windows application: Figure 3 - To install IronPrint using NuGet Package Manager Console, use the following command: Install-Package IronPrint

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.

How to Print PDF files in a C# Windows application: Figure 4 - In Visual Studio, go to Tools - NuGet Package Manager - select Manage NuGet Packages for Solutions.

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.

How to Print PDF files in a C# Windows application: Figure 5 - Install IronPrint using the Manage NuGet Package for Solutions by searching "ironprint" in the search bar of NuGet Package Manager, then select the project and click on the Install button.

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.

Print PDF using IronPrint

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.

How to Print PDF files in a C# Windows application: Figure 6 - Windows Form design for selecting a PDF file and printing the selected PDF using the IronPrint library.

In this example, we are going to print PDF files with a few lines of code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using IronPrint;
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog(this);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Printer.Print(openFileDialog1.FileName.ToString());
            //or
            // Configure print setting and then Print the file
            PrintSettings printSettings = new PrintSettings();
            printSettings.Dpi = 150;
            printSettings.NumberOfCopies = 2;
            printSettings.PaperOrientation = PaperOrientation.Portrait;
            Printer.Print(openFileDialog1.FileName.ToString(), printSettings);
            // or
            Printer.ShowPrintDialog(openFileDialog1.FileName.ToString());
            }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using IronPrint;
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog(this);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Printer.Print(openFileDialog1.FileName.ToString());
            //or
            // Configure print setting and then Print the file
            PrintSettings printSettings = new PrintSettings();
            printSettings.Dpi = 150;
            printSettings.NumberOfCopies = 2;
            printSettings.PaperOrientation = PaperOrientation.Portrait;
            Printer.Print(openFileDialog1.FileName.ToString(), printSettings);
            // or
            Printer.ShowPrintDialog(openFileDialog1.FileName.ToString());
            }
    }
}
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows.Forms
Imports IronPrint
Namespace WindowsFormsApp1
	Partial Public Class Form1
		Inherits Form

		Public Sub New()
			InitializeComponent()
		End Sub
		Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
			openFileDialog1.ShowDialog(Me)
		End Sub
		Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
			Printer.Print(openFileDialog1.FileName.ToString())
			'or
			' Configure print setting and then Print the file
			Dim printSettings As New PrintSettings()
			printSettings.Dpi = 150
			printSettings.NumberOfCopies = 2
			printSettings.PaperOrientation = PaperOrientation.Portrait
			Printer.Print(openFileDialog1.FileName.ToString(), printSettings)
			' or
			Printer.ShowPrintDialog(openFileDialog1.FileName.ToString())
		End Sub
	End Class
End Namespace
VB   C#

In the above code example to use the IronPrint library, first we import them into the code "using IronPrint". Then we are allowed to help users in selecting 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, it will wait 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 will load the printing PDF files into the object and send the file to the default printer. Now the printer will be printing 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 will open the print dialog menu and allow us to select the printer options. To learn more about the IronPrint code, please refer to the code examples page.

Conclusion

In summary, the IronPrint is a monument to the strength of accessibility and knowledge sharing in the digital era. The 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, the 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.

< PREVIOUS
Dotnet PDF Printer (Developer Tutorial) | IronPrint
NEXT >
C# Print PDF Programatically (Code Example Tutorial)

Ready to get started? Version: 2024.5 just released

Start Free Trial Total downloads: 3,233
View Licenses >