Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
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.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
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.
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.
9 .NET API products for your office documents