USING IRONPRINT

How to print a Word document in C#

Published April 3, 2024
Share:

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.

How to print a Word document in C#

  1. Create a Visual Studio Project
  2. Install IronWord, IronPDF and IronPrint libraries
  3. Create Word Document using IronWord WordDocument class
  4. Save Word document using SaveAs method
  5. Create PDF document using IronPDF's DocxToPdfRenderer method
  6. Adjust PrinterSettings using IronPrint
  7. Print using IronPrint Printer.Print method

IronPrint

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.

Key Features of IronPrint

1. Comprehensive Print Settings

IronPrint empowers developers to customize various aspects of the printing process. This includes:

  • Paper size
  • Orientation (Portrait or Landscape)
  • DPI (Dots Per Inch)
  • Number of copies
  • Printer name
  • Margins (Top, Bottom, Left, Right)
  • Grayscale printing

2. Versatile Printing with Printer Class

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.

3. Cross-Platform Support

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.

Prerequisites

Before diving into the implementation, ensure you have the following prerequisites:

  1. Visual Studio: Install Microsoft Visual Studio, a powerful integrated development environment for C#. Download it from the official website.
  2. IronWord Library: This library is essential for creating and manipulating Excel files. Install it using the NuGet Package Manager Console or directly from the official IronWord NuGet website.
  3. IronPDF Library: IronPDF will be used to convert the Excel file to PDF. Install it using the same NuGet installation method.
  4. IronPrint Library: Finally, install the IronPrint library to facilitate seamless printing in your C# application.

Steps to Create, Convert, and Print Word Documents

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.

Step 1: Create a C# Console Application in Visual Studio

Follow these steps to set up a C# console application in Visual Studio:

  1. Open Visual Studio and create a new C# Console Application
  2. Configure the Project as following and the click "Next"
  3. From Additional Information, choose the .NET Framework and click "Create".

Step 2: Install Necessary Libraries via NuGet Packages Manager

Follow the steps to install the necessary libraries:

  1. Open NuGet Package Manager Console or NuGet Package Manager for Solutions using tools menu or Solution Explorer in Visual Studio project.
  2. In the browse tab of NuGet, search for the libraries and click install.
  3. Install IronPrint Printing Library:

    • Using the NuGet Package Manager Console, add the following command:
    Install-Package IronPrint
    • Using the Manage NuGet Packages for Solutions

    How to print a Word document in C#: Figure 1 - 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.

  4. Do the same for installing IronWord and IronPDF library using the Manage NuGet Packages for Solutions. To install IronWord and IronPDF using the NuGet Package Manager Console, use the following command:
Install-Package IronPrint

Step 3: Create a Word Document using IronWord

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")
VB   C#

In this code:

  • We create a TextRun containing the sample text.
  • A Paragraph is created, and the TextRun is added to it.
  • Finally, a Word Document doc object is created with the paragraph, and the new document is saved as the file name "document.docx".

Output Word Document

How to print a Word document in C#: Figure 2 - Output DOCX file generated using IronWord: document.docx

Step 4: Convert Word Document to PDF using IronPDF

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")
VB   C#

In this code:

  • We use the DocxToPdfRenderer() method to render the Word document as a PDF.
  • The resulting PDF is saved as "document.pdf".

Step 5: Print the PDF using IronPrint

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)
VB   C#

In this code:

  • We fetch available printer names using Printer.GetPrinterNames().
  • Set the specific printer name (in this case, "Microsoft Print to PDF"). If the physical printer is not available then by default IronPrint takes this as the default printer.
  • Configure print settings, specifying paper size as A4 and setting margins.
  • Finally, print the PDF using Printer.Print() method.

How to print a Word document in C#: Figure 3 - Printable output PDF document using IronPrint: word.pdf

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.

Advantages of IronPrint for Printing in C#

Here are some key advantages of using IronPrint for printing jobs in C# console or windows forms application:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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.

Conclusion

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.

< PREVIOUS
How to Print a QR code in C#
NEXT >
Dotnet PDF Printer (Developer Tutorial) | IronPrint

Ready to get started? Version: 2024.10 just released

Free NuGet Download Total downloads: 10,762 View Licenses >