Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
Printing PDF files directly from a C# application can be a valuable feature, especially when developing applications that require seamless integration with physical PDF documents. Whether you're working on a PDF document management system, a point-of-sale application, or any other software that deals with printing, C# provides a robust set of libraries to facilitate this PDF print method functionality.
For this purpose, Microsoft C# provides a print method to print PDF files to a default printer. In this article, we'll explore the steps to print a PDF file to a printer using C#.
Before getting started, make sure you have the following prerequisites in place:
Create a new C# project or open an existing one in your preferred development environment. Ensure that your project is configured correctly, and you have the necessary permissions for printer interactions. The following process allows you to complete this process:
If you don't have Visual Studio installed, download and install it from the official website: Visual Studio.
In your C# code file, import the necessary namespaces to access the classes and methods related to printing.
using System.Drawing.Printing;
using System.Drawing.Printing;
Imports System.Drawing.Printing
These namespaces provide essential classes such as PrintDocument, PrintPageEventArgs, and PrintController for handling print operations.
The PrintDocument class is a cornerstone for printing in C#. Handle its PrintPage event to define what content to print and how it should be formatted. Let's start with a simple example of printing the contents of a text file:
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
// Specify the file path
string filePath = "C:\\path\\to\\your\\file.txt";
// Read the content of the file
string line = System.IO.File.ReadAllText(filePath);
// Create a Font object (adjust as needed)
Font font = new Font("Arial", 12);
// Create a RectangleF to define the printing area
RectangleF area = new RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height);
// Draw the content to the printing area
e.Graphics.DrawString(line, font, Brushes.Black, area);
// Set HasMorePages to false to indicate that there are no more pages to print
e.HasMorePages = false;
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
// Specify the file path
string filePath = "C:\\path\\to\\your\\file.txt";
// Read the content of the file
string line = System.IO.File.ReadAllText(filePath);
// Create a Font object (adjust as needed)
Font font = new Font("Arial", 12);
// Create a RectangleF to define the printing area
RectangleF area = new RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height);
// Draw the content to the printing area
e.Graphics.DrawString(line, font, Brushes.Black, area);
// Set HasMorePages to false to indicate that there are no more pages to print
e.HasMorePages = false;
}
Private Sub printDocument1_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
' Specify the file path
Dim filePath As String = "C:\path\to\your\file.txt"
' Read the content of the file
Dim line As String = System.IO.File.ReadAllText(filePath)
' Create a Font object (adjust as needed)
Dim font As New Font("Arial", 12)
' Create a RectangleF to define the printing area
Dim area As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
' Draw the content to the printing area
e.Graphics.DrawString(line, font, Brushes.Black, area)
' Set HasMorePages to false to indicate that there are no more pages to print
e.HasMorePages = False
End Sub
This example reads the content of a text file and prints it using the specified font and formatting.
Initiate the print job by creating an instance of the PrintDocument class, attaching the PrintPage event handler, and then triggering the print process. Optionally, you can display a print dialog for user configuration:
private void btnPrint_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
// Optionally, display a print dialog for user configuration
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == DialogResult.OK)
{
pd.PrinterSettings = printDialog.PrinterSettings;
pd.Print();
}
}
private void btnPrint_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
// Optionally, display a print dialog for user configuration
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == DialogResult.OK)
{
pd.PrinterSettings = printDialog.PrinterSettings;
pd.Print();
}
}
Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim pd As New PrintDocument()
AddHandler pd.PrintPage, AddressOf printDocument1_PrintPage
' Optionally, display a print dialog for user configuration
Dim printDialog As New PrintDialog()
If printDialog.ShowDialog() = DialogResult.OK Then
pd.PrinterSettings = printDialog.PrinterSettings
pd.Print()
End If
End Sub
This code creates a PrintDocument instance, attaches the PrintPage event handler, and then prints the document. The optional print dialog allows users to configure printing settings before initiating the print job. This prints the text document to a locally connected printer. If it is not present then the file is printed to the default printer name (Microsoft Print to PDF) in the Print dialog as shown below:
When it comes to handling printing functionality in C# console applications efficiently and effectively, the IronPrint library provides a powerful solution.
IronPrint is a comprehensive print library developed by Iron Software, designed to seamlessly integrate with .NET applications. Whether you're working on a desktop, web, or mobile project, IronPrint offers versatile printing PDF documents capabilities, supporting various file formats and providing customizable print settings.
Before diving into file printing, you need to install the IronPrint library. You can easily do this using the NuGet Package Manager Console:
Install-Package IronPrint
This command line will download and install the IronPrint library into your C# project.
Once IronPrint is installed, you need to initialize it in your C# code. Import the IronPrint namespace, and set the license key to ensure proper functionality:
using IronPrint;
class Program
{
public static void Main()
{
License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY";
}
}
using IronPrint;
class Program
{
public static void Main()
{
License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY";
}
}
Imports IronPrint
Friend Class Program
Public Shared Sub Main()
License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY"
End Sub
End Class
Printing a file using IronPrint is straightforward. You can use the Printer class, specifying the file path to the Print method to print PDF silently:
using IronPrint;
class Program
{
// static void main
public static void Main()
{
License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY";
// Specify the file path
var document = "C:\\path\\to\\your\\file.pdf";
// Print PDFs
Printer.Print(document);
}
}
using IronPrint;
class Program
{
// static void main
public static void Main()
{
License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY";
// Specify the file path
var document = "C:\\path\\to\\your\\file.pdf";
// Print PDFs
Printer.Print(document);
}
}
Imports IronPrint
Friend Class Program
' static void main
Public Shared Sub Main()
License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY"
' Specify the file path
Dim document = "C:\path\to\your\file.pdf"
' Print PDFs
Printer.Print(document)
End Sub
End Class
This example demonstrates printing a PDF file, but IronPrint supports a variety of file formats, including PDF, PNG, HTML, TIFF, GIF, JPEG, IMAGE, and BITMAP.
IronPrint allows you to customize print settings according to your application's requirements. You can configure settings like DPI, number of copies, paper orientation, and more using the PrintSettings class. The following code example helps you set the page settings and print PDF documents:
using IronPrint;
class Program
{
public static void Main()
{
IronPrint.License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY";
Console.WriteLine("Printing Started...");
// Specify the file path
string filePath = "C:\\path\\to\\your\\file.pdf";
// Configure print settings
PrintSettings printSettings = new PrintSettings();
printSettings.Dpi = 300;
printSettings.NumberOfCopies = 2;
printSettings.PaperOrientation = PaperOrientation.Landscape;
// Print the document with custom settings
Printer.Print(filePath, printSettings);
// Print using the Print dialog
Printer.ShowPrintDialog(filePath, printSettings);
}
}
using IronPrint;
class Program
{
public static void Main()
{
IronPrint.License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY";
Console.WriteLine("Printing Started...");
// Specify the file path
string filePath = "C:\\path\\to\\your\\file.pdf";
// Configure print settings
PrintSettings printSettings = new PrintSettings();
printSettings.Dpi = 300;
printSettings.NumberOfCopies = 2;
printSettings.PaperOrientation = PaperOrientation.Landscape;
// Print the document with custom settings
Printer.Print(filePath, printSettings);
// Print using the Print dialog
Printer.ShowPrintDialog(filePath, printSettings);
}
}
Imports IronPrint
Friend Class Program
Public Shared Sub Main()
IronPrint.License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY"
Console.WriteLine("Printing Started...")
' Specify the file path
Dim filePath As String = "C:\path\to\your\file.pdf"
' Configure print settings
Dim printSettings As New PrintSettings()
printSettings.Dpi = 300
printSettings.NumberOfCopies = 2
printSettings.PaperOrientation = PaperOrientation.Landscape
' Print the document with custom settings
Printer.Print(filePath, printSettings)
' Print using the Print dialog
Printer.ShowPrintDialog(filePath, printSettings)
End Sub
End Class
Here is what the output looks like:
If the physical printer is not installed then the default printer is used to print PDF documents. To get all the available printers, you can also use GetPrinterNames method.
// Retrieve printers' name
List<string> printersName = Printer.GetPrinterNames();
foreach (string printer in printersName)
{
Console.WriteLine(printer);
}
// Retrieve printers' name
List<string> printersName = Printer.GetPrinterNames();
foreach (string printer in printersName)
{
Console.WriteLine(printer);
}
' Retrieve printers' name
Dim printersName As List(Of String) = Printer.GetPrinterNames()
For Each printer As String In printersName
Console.WriteLine(printer)
Next printer
For more detailed information, please visit the documentation page.
Printing files in C# is a manageable task with the capabilities provided by the System.Drawing.Printing namespace. By handling the PrintPage event and utilizing the PrintDocument class, you can tailor the printing process to your specific requirements. This comprehensive guide has covered the fundamental steps involved in printing files from a C# application, offering you a solid foundation to integrate this functionality into your projects. As we explored further, we looked at IronPrint for additional customization options, such as handling different file types, incorporating images, and enhancing formatting based on the application's needs.
Integrating IronPrint into your C# application simplifies the process of printing files to a printer. With support for various file formats and customizable print settings, IronPrint provides a robust solution for developers looking to enhance their printing capabilities. Whether you're working on a desktop, web, or mobile project, IronPrint streamlines the printing process, making it a valuable addition to your .NET toolkit.
IronPrint offers a free trial page for more information. Download the library from here and give it a try.
9 .NET API products for your office documents