How to Print Word Documents in C#: The Ultimate Guide

Printing Word documents programmatically in C# can streamline business workflows, automate reporting, and enhance document management systems. This comprehensive guide explores how to print Word documents in C# using Spire.Doc for .NET, covering everything from basic printing to advanced customization techniques. We'll walk through practical code examples for each scenario, ensuring you can implement these solutions in real-world applications.
- .NET Library for Printing Word Documents
- Print Word Documents in C#
- Customize Printing Options
- Silently Print Word Documents
- Print Multiple Pages on One Sheet
- Conclusion
- FAQs
.NET Library for Printing Word Documents
Spire.Doc for .NET is a robust, standalone library that supports comprehensive Word document processing without requiring Microsoft Office to be installed. It provides intuitive APIs for loading, editing, and printing Word files (DOC/DOCX) while maintaining perfect formatting fidelity.
To get started, install the library via NuGet Package Manager:
Install-Package Spire.Doc
Alternatively, you can download Spire.Doc for .NET from our official website and reference the DLL file manually.
Print Word Documents in C#
The foundation of Word document printing in C# involves three key steps demonstrated in the following code. First, we create a Document object to represent our Word file, then load the actual document, and finally access the printing functionality through the PrintDocument class.
- C#
using Spire.Doc;
using System.Drawing.Printing;
namespace PrintWordDocument
{
internal class Program
{
static void Main(string[] args)
{
// Initialize a new Document instance
Document doc = new Document();
// Load the Word file from specified path
doc.LoadFromFile("Input.docx");
// Access the PrintDocument object for printing operations
PrintDocument printDoc = doc.PrintDocument;
// Send document to default printer
printDoc.Print();
}
}
}
This basic implementation handles the entire printing process, from document loading to physical printing, with just a few lines of code. The PrintDocument object abstracts all the underlying printing operations, making the process straightforward for developers.
Customize Printing Options
Beyond basic printing, Spire.Doc offers extensive customization via the PrinterSettings class, providing developers with granular control over the printing process. These settings allow you to tailor the output to specific needs, such as selecting particular pages or configuring advanced printer features.
To obtain the PrinterSettings object associated with the current document, use the following line of code:
- C#
PrinterSettings settings = printDoc.PrinterSettings;
Now, let’s explore the specific settings.
1. Specify the Printer Name
- C#
settings.PrinterName = "Your Printer Name";
This code snippet demonstrates how to target a specific printer in environments with multiple installed printers. The PrinterName property accepts the exact name of the printer as it appears in the system's printer list.
2. Specify Pages to Print
- C#
settings.FromPage = 1;
settings.ToPage = 5;
These settings are particularly useful when dealing with large documents, allowing you to print only the relevant sections and conserve resources.
3. Specify Number of Copies to Print
- C#
settings.Copies = 2;
The Copies property controls how many duplicates of the document will be printed, with the printer handling the duplication process efficiently.
4. Enable Duplex Printing
- C#
if (settings.CanDuplex)
{
settings.Duplex = Duplex.Default;
}
This example first checks for duplex printing support before enabling two-sided printing, ensuring compatibility across different printer hardware.
5. Print on a Custom Paper Size
- C#
settings.DefaultPageSettings.PaperSize = new PaperSize("custom", 800, 500);
Here we create a custom paper size (800x500 units) for specialized printing requirements, demonstrating Spire.Doc's flexibility in handling non-standard document formats.
6. Print Word to File
- C#
settings.PrintToFile = true;
settings.PrinterName = "Microsoft Print to PDF";
settings.PrintFileName = @"C:\Output.pdf";
This configuration uses the system's PDF virtual printer to create a PDF file instead of physical printing, showcasing how Spire.Doc can be used for document conversion as well.
Silently Print Word Documents
In automated environments, you may need to print documents without any user interaction or visible dialogs. The following implementation achieves silent printing by using the StandardPrintController.
- C#
using Spire.Doc;
using System.Drawing.Printing;
namespace SilentlyPrintWord
{
class Program
{
static void Main(string[] args)
{
// Initialize a new Document instance
Document doc = new Document();
// Load the Word file from specified path
doc.LoadFromFile("Input.docx");
// Access the PrintDocument object for printing operations
PrintDocument printDoc = doc.PrintDocument;
// Disable the print dialog
printDoc.PrintController = new StandardPrintController();
// Exexute printing
printDoc.Print();
}
}
}
The key to silent printing lies in assigning the StandardPrintController to the PrintController property, which suppresses all printing-related dialogs and progress indicators. This approach is ideal for server-side applications or batch processing scenarios where user interaction is not possible or desired.
Print Multiple Pages on One Sheet
For economizing paper usage or creating compact document versions, Spire.Doc supports printing multiple document pages on a single physical sheet. The PrintMultipageToOneSheet method simplifies this process with predefined layout options.
- C#
using Spire.Doc;
using Spire.Doc.Printing;
using System.Drawing.Printing;
namespace PrintMultiplePagesOnOneSheet
{
internal class Program
{
static void Main(string[] args)
{
// Initialize a new Document instance
Document doc = new Document();
// Load the Word file from specified path
doc.LoadFromFile("Input.docx");
// Configure 2-page-per-sheet printing and execute printing
doc.PrintMultipageToOneSheet(PagesPreSheet.TwoPages, false);
}
}
}
The PagesPreSheet enumeration offers several layout options (OnePage, TwoPages, FourPages, etc.), while the boolean parameter determines whether to include a page border on the printed sheet. This feature is particularly valuable for creating booklet layouts or draft versions of documents.
P.S. This scenario works only with .NET Framework versions earlier than 5.0.
Conclusion
This guide has demonstrated how Spire.Doc for .NET provides a comprehensive solution for Word document printing in C#. It simplifies the process with features such as:
- Basic & silent printing.
- Customizable print settings (printer selection, duplex, copies).
- Multi-page per sheet printing to reduce paper usage.
By integrating these techniques, developers can efficiently automate document printing in enterprise applications, enhancing productivity and reducing manual effort. Overall, Spire.Doc empowers developers to create robust printing solutions that meet diverse business requirements.
FAQs
Q1. Can I print encrypted or password-protected Word files?
A: Yes, Spire.Doc supports printing password-protected documents after loading them with the correct password:
- C#
doc.LoadFromFile("Protected.docx", FileFormat.Docx, "password");
After successful loading, you can print it like any other document, with all the same customization options available.
Q2. How can I print only selected text from a Word document?
A: You can extract specific content by accessing document sections and paragraphs:
- C#
Section section = doc.Sections[0];
Paragraph paragraph = section.Paragraphs[0];
// Create new document with selected content
Document newDoc = new Document();
newDoc.Sections.Add(section.Clone());
newDoc.Print();
This approach gives you precise control over which document portions get printed.
Q3. Can I print documents in landscape mode or adjust margins programmatically?
A: Yes! Modify the DefaultPageSettings properties:
- C#
printDoc.DefaultPageSettings.Landscape = true;
printDoc.DefaultPageSettings.Margins = new Margins(50, 50, 50, 50);
Q4. Can I print other file formats (e.g., PDF, Excel) using Spire.Doc?
A: Spire.Doc is designed for Word files (DOC/DOCX). For PDFs, use Spire.PDF; for Excel, use Spire.XLS.
Get a Free License
To fully experience the capabilities of Spire.Doc for Python without any evaluation limitations, you can request a free 30-day trial license.
C#/VB.NET: How to Print Word on a Custom Paper Size
Sometimes you may want to print Word documents in accordance with your own preferences, for instance, print your files on custom paper sizes to make them more personalized. In this article, you will learn how to achieve this function using Spire.Doc for .NET.
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.
- Package Manager
PM> Install-Package Spire.Doc
Print Word on a Custom Paper Size
The table below shows a list of core classes, methods and properties utilized in this scenario.
| Name | Description |
| Document Class | Represents a document model for Word. |
| PaperSize Class | Specifies the size of a piece of paper. |
| PrintDocument Class | Defines a reusable object that sends output to a printer, when printing from a Windows Forms application. |
| PrintDocument.DefaultPageSettings Property | Gets or sets page settings that are used as defaults for all pages to be printed. |
| Document.PrintDocument Property | Gets the PrintDocument object. |
| DefaultPageSettings.PaperSize Property | Sets the custom paper size. |
| Document.LoadFromFile() Method | Loads the sample document. |
| PrintDocument.Print() Method | Prints the document. |
The following are the steps to print Word on a custom paper size.
- Instantiate a Document object
- Load the sample document using Document.LoadFromFile() method.
- Get the PrintDocument object using Document.PrintDocument property.
- Set the custom paper size using DefaultPageSettings.PaperSize Property.
- Print the document using PrintDocument.Print() method.
- C#
- VB.NET
using Spire.Doc;
using System.Drawing.Printing;
namespace PrintWord
{
class Program
{
static void Main(string[] args)
{
//Instantiate a Document object.
Document doc = new Document();
//Load the document
doc.LoadFromFile(@"Sample.docx");
//Get the PrintDocument object
PrintDocument printDoc = doc.PrintDocument;
//Customize the paper size
printDoc.DefaultPageSettings.PaperSize = new PaperSize("custom", 900, 800);
//Print the document
printDoc.Print();
}
}
}
Imports Spire.Doc
Imports System.Drawing.Printing
Namespace PrintWord
Class Program
Private Shared Sub Main(args As String())
'Instantiate a Document object.
Dim doc As New Document()
'Load the document
doc.LoadFromFile("Sample.docx")
'Get the PrintDocument object
Dim printDoc As PrintDocument = doc.PrintDocument
'Customize the paper size
printDoc.DefaultPageSettings.PaperSize = New PaperSize("custom", 900, 800)
'Print the document
printDoc.Print()
End Sub
End Class
End Namespace
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.