USING IRONPRINT

C# Print PDF Programatically (Code Example Tutorial)

Published January 30, 2025
Share:

Printing PDFs in .NET must be fast and reliable. Handling print tasks manually can be tedious, especially with large document volumes. IronPrint, a .NET printing library, eliminates this hassle by ensuring that invoices, reports, and images are printed smoothly and without unnecessary complexity. It allows developers to automate their printing workflows without dealing with unpredictable printer behaviors or time-consuming configurations.

Businesses depend on automated printing for invoices, reports, and shipping labels. Some users also want to print specific pages due to business reasons. Manual printing introduces unnecessary steps and errors that slow down workflows. By integrating IronPrint, developers remove these inefficiencies, automate repetitive tasks, and improve accuracy. This results in a smoother experience for end users who need timely, well-formatted documents.

IronPrint simplifies printing by offering an efficient way to manage PDFs and images. Instead of handling complex printer settings manually, developers can send documents to the printer with minimal setup. This frees up time to focus on core application functionality while ensuring documents print correctly every time. The next step is setting up IronPrint in your .NET project. The process is simple and once integrated, it ensures consistent and reliable printing every time.

Getting Started with IronPrint

Csharp Print Pdf Programatically 1 related to Getting Started with IronPrint

Installation Process via NuGet Package Manager

Installing IronPrint is straightforward. The easiest way to add it to your .NET project is through NuGet Package Manager:

  • Open your project in Visual Studio.

  • Navigate to the NuGet Package Manager.

  • Search for "IronPrint".

  • Click Install and let Visual Studio handle the rest.

For those who prefer the Package Manager Console, use the following command:

Install-Package IronPrint
Install-Package IronPrint
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronPrint
VB   C#

Csharp Print Pdf Programatically 2 related to Installation Process via NuGet Package Manager

After installation, ensure all dependencies are properly restored.

Basic Setup and Configuration in a .NET Project

Once installed, IronPrint needs minimal configuration to start working. First, ensure your application has access to the required printer drivers. Then, initialize IronPrint by referencing the library and setting up basic parameters. A simple implementation might look like this:

using IronPrint;
Printer.Print("sample-document.pdf");
using IronPrint;
Printer.Print("sample-document.pdf");
Imports IronPrint
Printer.Print("sample-document.pdf")
VB   C#

This basic setup enables the application to send a PDF file to the default printer instantly. Developers can customize the behavior by specifying a different printer, adjusting print settings, or integrating additional configurations based on their needs. With installation and setup complete, the next step is exploring IronPrint’s core functionalities to manage different printing scenarios efficiently.

Core Printing Functionalities

Printing PDFs Programmatically

IronPrint makes printing PDFs and images straightforward. The Print method processes PDF files efficiently, sending them directly to the printer without unnecessary steps. This is especially useful when you need to print PDF files quickly and efficiently. A simple implementation looks like this:

using IronPrint;
Printer.Print("invoice.pdf");
using IronPrint;
Printer.Print("invoice.pdf");
Imports IronPrint
Printer.Print("invoice.pdf")
VB   C#

Printing Images

For image files, IronPrint supports common formats like PNG, JPEG, and BMP. The Print method automatically detects and processes images, ensuring they retain clarity and resolution when printed.

using IronPrint;
Printer.Print("logo.png");
using IronPrint;
Printer.Print("logo.png");
Imports IronPrint
Printer.Print("logo.png")
VB   C#

Multiple images in a single document are processed in sequence. IronPrint makes sure that formatting remains intact, preventing distortion or quality loss. This makes it ideal for printing complex reports or graphical documents alongside PDF files.

Silent Printing vs. Print Dialog

IronPrint gives developers control over how documents are printed. Silent printing sends PDF documents directly to the printer without requiring user input. This is useful for applications that automate batch printing, reducing interruptions and speeding up workflows. You can print multiple PDF files efficiently as well.

using IronPrint;
Printer.Print("report.pdf");
using IronPrint;
Printer.Print("report.pdf");
Imports IronPrint
Printer.Print("report.pdf")
VB   C#

Alternatively, if users need to adjust settings before printing, IronPrint can trigger a print dialog. This allows users to select a printer, set page orientation, and adjust print quality before finalizing the print job.

using IronPrint;
Printer.ShowPrintDialog("document.pdf");
using IronPrint;
Printer.ShowPrintDialog("document.pdf");
Imports IronPrint
Printer.ShowPrintDialog("document.pdf")
VB   C#

Silent printing is best for automated workflows where user intervention isn’t required, while print dialogs are ideal when customization is necessary. Both methods ensure flexibility and efficiency depending on the application’s needs. By leveraging these functionalities, developers can integrate automated and user-friendly printing capabilities within their .NET applications, ensuring smooth handling of print pdf documents.

Advanced Print Settings

Paper Configuration

With IronPrint, developers can customize paper sizes and orientations to fit their application's needs. This is especially useful for printing on non-standard paper formats or when a specific layout is required The current print document object allows developers to manage these configurations efficiently. To set the paper size and orientation, simply initialize the PrintSettings class and specify the desired properties:

using IronPrint;

PrintSettings printSettings = new PrintSettings
{
    PaperSize = PaperSize.A4,
    PaperOrientation = PaperOrientation.Landscape
};

Printer.Print("document.pdf", printSettings);
using IronPrint;

PrintSettings printSettings = new PrintSettings
{
    PaperSize = PaperSize.A4,
    PaperOrientation = PaperOrientation.Landscape
};

Printer.Print("document.pdf", printSettings);
Imports IronPrint

Private printSettings As New PrintSettings With {
	.PaperSize = PaperSize.A4,
	.PaperOrientation = PaperOrientation.Landscape
}

Printer.Print("document.pdf", printSettings)
VB   C#

Print Quality and Copies

For better control over print quality, developers can adjust the DPI (dots per inch) setting to ensure documents are printed with the right level of detail. Higher DPI settings improve clarity but may increase print time. Additionally, you can specify the number of copies to be printed:

using IronPrint;

PrintSettings printSettings = new PrintSettings
{
    Dpi = 300,
    NumberOfCopies = 2
};

Printer.Print("report.pdf", printSettings);
using IronPrint;

PrintSettings printSettings = new PrintSettings
{
    Dpi = 300,
    NumberOfCopies = 2
};

Printer.Print("report.pdf", printSettings);
Imports IronPrint

Private printSettings As New PrintSettings With {
	.Dpi = 300,
	.NumberOfCopies = 2
}

Printer.Print("report.pdf", printSettings)
VB   C#

Printer Selection

Developers can specify which printer to use instead of relying on the system's default printer. This is particularly helpful in office environments where multiple printers are available:

using IronPrint;

PrintSettings printSettings = new PrintSettings
{
    // Specified Printer name
    PrinterName = "Your Printer Name"
};

// Input PDF file for PDF printing process
Printer.Print("invoice.pdf", printSettings);
using IronPrint;

PrintSettings printSettings = new PrintSettings
{
    // Specified Printer name
    PrinterName = "Your Printer Name"
};

// Input PDF file for PDF printing process
Printer.Print("invoice.pdf", printSettings);
Imports IronPrint

Private printSettings As New PrintSettings With {.PrinterName = "Your Printer Name"}

' Input PDF file for PDF printing process
Printer.Print("invoice.pdf", printSettings)
VB   C#

Margins and Grayscale Printing

Custom margins help ensure proper document alignment, while grayscale printing can save ink when color isn’t necessary. Margins are defined in millimeters for precise adjustments:

using IronPrint;

PrintSettings printSettings = new PrintSettings
{
    PaperMargins = new Margins
    {
        Top = 10,
        Right = 10,
        Bottom = 10,
        Left = 10
    },
    Grayscale = true
};

Printer.Print("draft.pdf", printSettings);
using IronPrint;

PrintSettings printSettings = new PrintSettings
{
    PaperMargins = new Margins
    {
        Top = 10,
        Right = 10,
        Bottom = 10,
        Left = 10
    },
    Grayscale = true
};

Printer.Print("draft.pdf", printSettings);
Imports IronPrint

Private printSettings As New PrintSettings With {
	.PaperMargins = New Margins With {
		.Top = 10,
		.Right = 10,
		.Bottom = 10,
		.Left = 10
	},
	.Grayscale = True
}

Printer.Print("draft.pdf", printSettings)
VB   C#

By taking advantage of these advanced print settings, developers can fine-tune the printing process to meet specific requirements for consistent and professional-quality output.

Retrieving Printer Information

IronPrint allows developers to retrieve a list of all available printers on the system. This is useful in applications where users need to select a specific printer before initiating a print job. The GetPrinterNames method returns an array of installed printer names.

using IronPrint;

var printers = Printer.GetPrinterNames();
foreach (var printer in printers)
{
    Console.WriteLine(printer); // Outputs: OneNote (Desktop), Microsoft Print to PDF
}
using IronPrint;

var printers = Printer.GetPrinterNames();
foreach (var printer in printers)
{
    Console.WriteLine(printer); // Outputs: OneNote (Desktop), Microsoft Print to PDF
}
Imports IronPrint

Private printers = Printer.GetPrinterNames()
For Each printer In printers
	Console.WriteLine(printer) ' Outputs: OneNote (Desktop), Microsoft Print to PDF
Next printer
VB   C#

This implementation fetches the available printer names and prints them to the console. Developers can use this data to populate dropdown menus or dynamically assign printer preferences within their applications.

Conclusion

Csharp Print Pdf Programatically 3 related to Conclusion

IronPrint revolutionizes programmatic printing in .NET applications by providing a powerful yet flexible solution for handling PDFs and images. IronPrint’s advanced print settings empower users with precise control over paper size, print quality, margins, and grayscale options. Additionally, its ability to dynamically retrieve printer information enhances usability, enabling applications to adjust to different environments without requiring manual intervention.

With IronPrint managing print-related tasks efficiently and accurately, developers can dedicate their focus to refining core application features and delivering seamless functionality. IronPrint offers a free trial for developers to explore its full capabilities before committing. Its licensing starts at $749, providing a cost-effective and scalable solution for businesses of all sizes.

< PREVIOUS
How to Print PDF files in a C# Windows application
NEXT >
C# Send PDF to Printer (Step-By-Step Tutorial)