USING IRONPRINT

How to print a Word document in C#

In the ever-evolving landscape of software development, the ability to generate and print Word documents 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 a 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 a Word Document using IronWord WordDocument class
  4. Save the Word document using SaveAs method
  5. Create a 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 displaying a print dialog while printing in real-time applications, giving more granular 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

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

Step 2: Install Necessary Libraries via NuGet Package Manager

  1. Open the NuGet Package Manager Console or NuGet Package Manager for Solutions using the Tools menu or Solution Explorer in the 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, use the following command:

      Install-Package IronPrint
      Install-Package IronPrint
      SHELL
  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 commands:

    Install-Package IronWord
    Install-Package IronPdf
    Install-Package IronWord
    Install-Package IronPdf
    SHELL

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 sample text and save it:

using IronWord;
using IronWord.Models;

// Code to Create Word File

// Create a TextRun object with sample text
TextRun textRun = new TextRun("Sample text");

// Create a paragraph and add the TextRun to it
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);

// Create a Word document object with the paragraph and save it as a .docx file
WordDocument doc = new WordDocument(paragraph);
doc.SaveAs("assets/document.docx");
using IronWord;
using IronWord.Models;

// Code to Create Word File

// Create a TextRun object with sample text
TextRun textRun = new TextRun("Sample text");

// Create a paragraph and add the TextRun to it
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);

// Create a Word document object with the paragraph and save it as a .docx file
WordDocument doc = new WordDocument(paragraph);
doc.SaveAs("assets/document.docx");
Imports IronWord
Imports IronWord.Models

' Code to Create Word File

' Create a TextRun object with sample text
Private textRun As New TextRun("Sample text")

' Create a paragraph and add the TextRun to it
Private paragraph As New Paragraph()
paragraph.AddTextRun(textRun)

' Create a Word document object with the paragraph and save it as a .docx file
Dim doc As New WordDocument(paragraph)
doc.SaveAs("assets/document.docx")
$vbLabelText   $csharpLabel

In this code:

  • We create a TextRun containing the sample text.
  • A Paragraph is created, and the TextRun is added to it.
  • Finally, a WordDocument is created with the paragraph, and the new document is saved as "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 DOCX file to PDF using IronPDF

// Create a DocxToPdfRenderer instance
var renderer = new DocxToPdfRenderer();

// Render the DOCX document as a PDF
var pdf = renderer.RenderDocxAsPdf("assets/document.docx");

// Save the resulting PDF
pdf.SaveAs("assets/word.pdf");
using IronPdf;

// Code to convert DOCX file to PDF using IronPDF

// Create a DocxToPdfRenderer instance
var renderer = new DocxToPdfRenderer();

// Render the DOCX document as a PDF
var pdf = renderer.RenderDocxAsPdf("assets/document.docx");

// Save the resulting PDF
pdf.SaveAs("assets/word.pdf");
Imports IronPdf

' Code to convert DOCX file to PDF using IronPDF

' Create a DocxToPdfRenderer instance
Private renderer = New DocxToPdfRenderer()

' Render the DOCX document as a PDF
Private pdf = renderer.RenderDocxAsPdf("assets/document.docx")

' Save the resulting PDF
pdf.SaveAs("assets/word.pdf")
$vbLabelText   $csharpLabel

In this code:

  • We use the DocxToPdfRenderer() method to render the Word document as a PDF.
  • The resulting PDF is saved as "word.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;
using System.Collections.Generic;

// Code for Printing using IronPrint

// Fetch printer names available in the system
List<string> printerNames = Printer.GetPrinterNames();

// Configure print settings
PrintSettings printerSettings = new PrintSettings();
foreach(string printerName in printerNames)
{
    if(printerName.Equals("Microsoft Print to PDF"))
    {
        printerSettings.PrinterName = printerName;
    }
}

// Set paper size to A4 and configure margins
printerSettings.PaperSize = PaperSize.A4;
Margins margins = new Margins(30, 10);
printerSettings.PaperMargins = margins;

// Print the PDF with the specified settings
Printer.Print("assets/word.pdf", printerSettings);
using IronPrint;
using System.Collections.Generic;

// Code for Printing using IronPrint

// Fetch printer names available in the system
List<string> printerNames = Printer.GetPrinterNames();

// Configure print settings
PrintSettings printerSettings = new PrintSettings();
foreach(string printerName in printerNames)
{
    if(printerName.Equals("Microsoft Print to PDF"))
    {
        printerSettings.PrinterName = printerName;
    }
}

// Set paper size to A4 and configure margins
printerSettings.PaperSize = PaperSize.A4;
Margins margins = new Margins(30, 10);
printerSettings.PaperMargins = margins;

// Print the PDF with the specified settings
Printer.Print("assets/word.pdf", printerSettings);
Imports IronPrint
Imports System.Collections.Generic

' Code for Printing using IronPrint

' Fetch printer names available in the system
Private printerNames As List(Of String) = Printer.GetPrinterNames()

' Configure print settings
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

' Set paper size to A4 and configure margins
printerSettings.PaperSize = PaperSize.A4
Dim margins As New Margins(30, 10)
printerSettings.PaperMargins = margins

' Print the PDF with the specified settings
Printer.Print("assets/word.pdf", printerSettings)
$vbLabelText   $csharpLabel

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 a 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 applications:

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 and enhance your C# application with printing capabilities.

Frequently Asked Questions

What is IronPrint?

IronPrint is a robust and versatile print library for .NET developed by Iron Software. It offers tools for handling printing tasks in C#, providing developers with fine-grained control over the printing process and printer settings.

How can I print a Word document in C# using IronPrint?

To print a Word document in C# using IronPrint, follow these steps: create a Visual Studio Project, install IronWord, IronPDF, and IronPrint libraries, create a Word Document using IronWord, convert it to PDF using IronPDF's DocxToPdfRenderer method, and use IronPrint for printing.

What are the key features of IronPrint?

IronPrint features comprehensive print settings customization, versatile printing with the Printer class, and cross-platform support, making it suitable for deployment across multiple environments.

What prerequisites are needed to use IronPrint?

To use IronPrint, you need to have Visual Studio installed, along with IronWord, IronPDF, and IronPrint libraries. These can be installed via the NuGet Package Manager Console.

How do I create a Word document using IronWord?

Create a Word document using IronWord by creating a TextRun with sample text, adding it to a Paragraph, and then creating a WordDocument with the paragraph. Save it using the SaveAs method.

How can I convert a Word document to PDF using IronPDF?

Use IronPDF's DocxToPdfRenderer to convert a Word document to PDF. Create a DocxToPdfRenderer instance, render the DOCX as a PDF, and save the resulting PDF.

What advantages does IronPrint offer for printing in C#?

IronPrint offers advantages like asynchronous printing, versatile printing options, cross-platform support, customizable print settings, seamless integration with IronQR and IronPDF, and a user-friendly API.

Can IronPrint be used for cross-platform printing?

Yes, IronPrint supports cross-platform compatibility, making it suitable for deployment across Windows, macOS, Android, and iOS.

How do I install IronPrint using NuGet?

To install IronPrint using NuGet, open the NuGet Package Manager Console in Visual Studio and use the command: Install-Package IronPrint.

What kind of print settings can I customize with IronPrint?

With IronPrint, you can customize print settings such as paper size, orientation, DPI, number of copies, printer name, margins, and grayscale printing.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.
< PREVIOUS
Print QR Codes in C# with IronPrint
NEXT >
.NET PDF Printer Tutorial: Using IronPrint

Ready to get started? Version: 2025.6 just released

View Licenses >