Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
In the ever-evolving landscape of software development, the ability to generate and print Word document programmatically is a fundamental requirement. C# developers often encounter scenarios where generating and printing Word documents becomes essential for tasks such as report generation, document processing, or creating professional-looking outputs. To address this need, Iron Software presents IronWord, IronPDF and IronPrint, powerful libraries designed to streamline the creation, manipulation, and printing of Word and PDF documents within C# applications.
This article will explore the features and advantages of IronPrint for printing, creating Word document object sender using IronWord and converting it to PDF using IronPDF for printing.
IronPrint, developed by Iron Software, is a robust and versatile print library for .NET, offering a wide array of tools for handling printing tasks in C#. It stands out with dedicated classes and methods tailored for print-related functionalities, providing developers with fine-grained control over the printing process and printer settings.
IronPrint empowers developers to customize various aspects of the printing process. This includes:
One of the standout features of IronPrint is the introduction of the Printer class. This class provides a comprehensive set of methods for printing various file types, encompassing images and PDF documents. The versatility of the Printer class allows for seamless integration into diverse printing scenarios. It also allows to display print dialog while printing in real-time applications giving more granule control for printing Word documents.
IronPrint boasts cross-platform compatibility, making it suitable for deployment across multiple environments. Whether your application runs on Windows, macOS, Android, or iOS, IronPrint ensures consistent and reliable printing functionality.
Before diving into the implementation, ensure you have the following prerequisites:
Follow these steps to set up a C# console application, create a Word document object, convert it to PDF, and finally print it using IronWord, IronPDF, and IronPrint respectively.
Follow these steps to set up a C# console application in Visual Studio:
Follow the steps to install the necessary libraries:
Install IronPrint Printing Library:
Install-Package IronPrint
Install-Package IronPrint
Let's begin by creating a simple Word document using IronWord. The following code snippet illustrates how to create a Word document with a sample text and save it:
using IronWord;
using IronWord.Models;
// Code to Create Word File
TextRun textRun = new TextRun("Sample text");
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);
WordDocument doc = new WordDocument(paragraph);
doc.SaveAs("assets/document.docx");
using IronWord;
using IronWord.Models;
// Code to Create Word File
TextRun textRun = new TextRun("Sample text");
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);
WordDocument doc = new WordDocument(paragraph);
doc.SaveAs("assets/document.docx");
Imports IronWord
Imports IronWord.Models
' Code to Create Word File
Private textRun As New TextRun("Sample text")
Private paragraph As New Paragraph()
paragraph.AddTextRun(textRun)
Dim doc As New WordDocument(paragraph)
doc.SaveAs("assets/document.docx")
In this code:
Once we have our Word document, we may need to convert it to a PDF format. IronPDF simplifies this process, allowing for seamless conversion. Here's the code snippet:
using IronPdf;
// Code to convert HTML file to PDF using IronPDF
var renderer = new DocxToPdfRenderer();
var pdf = renderer.RenderDocxAsPdf("assets/document.docx");
pdf.SaveAs("assets/word.pdf");
using IronPdf;
// Code to convert HTML file to PDF using IronPDF
var renderer = new DocxToPdfRenderer();
var pdf = renderer.RenderDocxAsPdf("assets/document.docx");
pdf.SaveAs("assets/word.pdf");
Imports IronPdf
' Code to convert HTML file to PDF using IronPDF
Private renderer = New DocxToPdfRenderer()
Private pdf = renderer.RenderDocxAsPdf("assets/document.docx")
pdf.SaveAs("assets/word.pdf")
In this code:
Printing the PDF can be achieved using IronPrint, providing flexibility and control over the print settings. If print settings are not set then default settings are used to print. The following code demonstrates how to print the generated PDF:
using IronPrint;
// Code for Printing using IronPrint
List<string> printerNames = Printer.GetPrinterNames();
PrintSettings printerSettings = new PrintSettings();
foreach(string printerName in printerNames)
{
if(printerName.Equals("Microsoft Print to PDF"))
printerSettings.PrinterName = printerName;
}
printerSettings.PaperSize = PaperSize.A4;
Margins margins = new Margins(30,10);
printerSettings.PaperMargins = margins;
Printer.Print("assets/word.pdf", printerSettings);
using IronPrint;
// Code for Printing using IronPrint
List<string> printerNames = Printer.GetPrinterNames();
PrintSettings printerSettings = new PrintSettings();
foreach(string printerName in printerNames)
{
if(printerName.Equals("Microsoft Print to PDF"))
printerSettings.PrinterName = printerName;
}
printerSettings.PaperSize = PaperSize.A4;
Margins margins = new Margins(30,10);
printerSettings.PaperMargins = margins;
Printer.Print("assets/word.pdf", printerSettings);
Imports IronPrint
' Code for Printing using IronPrint
Private printerNames As List(Of String) = Printer.GetPrinterNames()
Private printerSettings As New PrintSettings()
For Each printerName As String In printerNames
If printerName.Equals("Microsoft Print to PDF") Then
printerSettings.PrinterName = printerName
End If
Next printerName
printerSettings.PaperSize = PaperSize.A4
Dim margins As New Margins(30,10)
printerSettings.PaperMargins = margins
Printer.Print("assets/word.pdf", printerSettings)
In this code:
If you want to get control over NumberOfCopies, Print multiple pages, Grayscale, and DPI, please visit this code examples page. You can also enable printer dialog and prevent silent printing of documents.
Here are some key advantages of using IronPrint for printing jobs in C# console or windows forms application:
Asynchronous Printing:
IronPrint provides asynchronous functions, allowing print operations to be performed asynchronously. This prevents print operations from blocking threads, leading to enhanced performance and responsiveness in your application.
Versatile Printing Options:
The dedicated Printer class in IronPrint offers a comprehensive set of methods for printing various file types, including images and PDF documents. This versatility provides flexibility beyond standard printing, allowing developers to handle different types of content with ease.
Cross-Platform Support:
IronPrint supports printing across multiple platforms, including Windows, Android, iOS, and macOS. This cross-platform compatibility makes it suitable for a variety of application environments, ensuring that your printing functionality can be deployed across diverse operating systems.
Customizable Print Settings:
IronPrint allows developers to finely control print settings, providing a high degree of customization. Developers can specify various aspects of the printing process, such as paper size, orientation, DPI, number of copies, printer name, margins, and grayscale printing, through the PrintSettings class.
Seamless Integration with IronQR and IronPDF:
IronPrint integrates seamlessly with other Iron Software libraries, such as IronQR and IronPDF. This integration allows developers to create, convert, and print QR codes, PDFs, and other documents within a unified and efficient workflow.
User-Friendly API:
IronPrint features a user-friendly API that simplifies the implementation of printing functionality in C# applications. Developers can quickly add barcode and printing capabilities to their projects, reducing development time and effort.
Comprehensive Documentation and Support:
IronPrint is backed by comprehensive documentation and support from Iron Software. This ensures that developers have access to resources and assistance when implementing printing features, making it easier to troubleshoot issues and optimize the printing process.
Enhanced Control Over Printing Process:
With IronPrint, developers have enhanced control over the printing process. Features such as the ability to set paper size, margins, and other print parameters allow for precise control, ensuring that the printed output meets specific requirements and standards.
By following these steps, you can seamlessly integrate Word document handling, conversion to PDF, and printing functionality into your C# applications. IronWord, IronPDF, and IronPrint collectively provide a powerful toolkit for developers looking to enhance their document-related tasks. Whether you're working on web, mobile, desktop, or console applications, this guide serves as a comprehensive resource for leveraging these libraries effectively in your .NET projects.
For more information on how to Print efficiently, please visit this documentation page.
IronPrint options are available for various needs, starting from $749. Download the library from here and enhance your C# application with printing capabilities.
9 .NET API products for your office documents