C#/VB.NET - How to Print Word Documents

Alexander Stock
4 min readAug 23, 2023

--

Printing Word documents is a fundamental function that allows users to convert their digital text and images into physical hard copies. It remains a common practice in various professional, academic, and personal settings. By printing Word documents, individuals can share information, create tangible records, review drafts, and distribute documents offline. This article aims to introduce how to send Word documents to a physical or virtual printer in C# and VB.NET using Free Spire.Doc for .NET.

  • Print Word Documents
  • Silently Print Word Documents
  • Print Word to PDF

Install Free Spire.Doc for .NET

Free Spire.Doc for .NET is a free and professional .NET Word library that allows programmers to create, edit, and convert Word documents in any .NET application. You can search for “FreeSpire.Doc” through “NuGet Package Manager” and install it in seconds.

Note: The free version is limited to 500 paragraphs and 25 tables per document when loading or creating a Word document. To get rid of the restriction, you’ll need use the Pro Edition of Spire.Doc.

Print Word Documents in C#, VB.NET

The following code snippet demonstrates how to use the Spire.Doc library to print a Word document using a specific printer. Here’s an explanation of the code:

  1. A new Document object named doc is created. This object represents the Word document that will be printed.
  2. The LoadFromFile method is used to load a Word document from a specified file path into the doc object.
  3. The PrintDocument property of the doc object is accessed to obtain a PrintDocument object named printDoc. This object provides functionality for printing the Word document.
  4. The PrinterName property of the printDoc.PrinterSettings object is set to specify the name of the printer that will be used for printing.
  5. The FromPage and ToPage properties of printDoc.PrinterSettings are set to specify the range of pages to be printed.
  6. The Copies property of printDoc.PrinterSettings is set to determine the number of copies to be printed.
  7. Finally, the Print method of the printDoc object is called to initiate the printing process.

[C#]

using Spire.Doc;
using System.Drawing.Printing;

namespace PrintWordDocument
{
class Program
{
static void Main(string[] args)
{
//Create a Document object
Document doc = new Document();

//Load a Word document
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\input.docx");

//Get the PrintDocument object
PrintDocument printDoc = doc.PrintDocument;

//Specify the printer name
printDoc.PrinterSettings.PrinterName = "NPI7FE2DF (HP Color LaserJet MFP M281fdw)";

//Specify the range of pages to print
printDoc.PrinterSettings.FromPage = 1;
printDoc.PrinterSettings.ToPage = 10;

//Set the number of copies to print
printDoc.PrinterSettings.Copies = 1;

//Print the document
printDoc.Print();
}
}
}

[VB.NET]

Imports Spire.Doc
Imports System.Drawing.Printing

Namespace PrintWordDocument
Class Program
Shared Sub Main(ByVal args() As String)
'Create a Document object
Document doc = New Document()

'Load a Word document
doc.LoadFromFile("C:\Users\Administrator\Desktop\input.docx")

'Get the PrintDocument object
Dim printDoc As PrintDocument = doc.PrintDocument

'Specify the printer name
printDoc.PrinterSettings.PrinterName = "NPI7FE2DF (HP Color LaserJet MFP M281fdw)"

'Specify the range of pages to print
printDoc.PrinterSettings.FromPage = 1
printDoc.PrinterSettings.ToPage = 10

'Set the number of copies to print
printDoc.PrinterSettings.Copies = 1

'Print the document
printDoc.Print()
End Sub
End Class
End Namespace

Silently Print Word Documents in C#, VB.NET

This code snippet demonstrates how to silently print a Word document using the Spire.Doc library in C#. Here’s a breakdown of the code:

  1. A new instance of the Document class is created.
  2. The Word document is loaded into the doc object using the LoadFromFile method.
  3. The PrintDocument property of the doc object is accessed to obtain the corresponding PrintDocument object. This allows interaction with the printing system.
  4. The name of the desired printer is specified by setting the PrinterName property of the PrinterSettings object within the PrintDocument.
  5. To ensure silent printing without any user interface, the PrintController property of the PrintDocument is set to a new instance of the StandardPrintController class.
  6. Finally, the Print method is called on the PrintDocument object to initiate the printing process, which sends the document to the specified printer for printing.

[C#]

using Spire.Doc;
using System.Drawing.Printing;

namespace SilentlyPrintWord
{
class Program
{
static void Main(string[] args)
{
//Create a Document object
Document doc = new Document();

//Load a Word document
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\input.docx");

//Get the PrintDocument object
PrintDocument printDoc = doc.PrintDocument;

//Specify the printer name
printDoc.PrinterSettings.PrinterName = "NPI7FE2DF (HP Color LaserJet MFP M281fdw)";

//Specify the print controller to StandardPrintController
printDoc.PrintController = new StandardPrintController();

//Print the document
printDoc.Print();
}
}
}

[VB.NET]

Imports Spire.Doc
Imports System.Drawing.Printing

Namespace SilentlyPrintWord
Class Program
Shared Sub Main(ByVal args() As String)
'Create a Document object
Document doc = New Document()

'Load a Word document
doc.LoadFromFile("C:\Users\Administrator\Desktop\input.docx")

'Get the PrintDocument object
Dim printDoc As PrintDocument = doc.PrintDocument

'Specify the printer name
printDoc.PrinterSettings.PrinterName = "NPI7FE2DF (HP Color LaserJet MFP M281fdw)"

'Specify the print controller to StandardPrintController
printDoc.PrintController = New StandardPrintController()

'Print the document
printDoc.Print()
End Sub
End Class
End Namespace

Print Word to PDF in C#, VB.NET

In addition to printing Word documents with a physical printer, you can also print documents with virtual printers, such as Microsoft Print to PDF and Microsoft XPS Document Writer. The following code snippet loads a Word document, prints it as a PDF using the “Microsoft Print to PDF” printer, and saves the resulting PDF file to a specified location.

[C#]

using Spire.Doc;
using System.Drawing.Printing;

namespace PrintWordToPdf
{
class Program
{
static void Main(string[] args)
{
//Create a Document object
Document doc = new Document();

//Load a Word document
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\input.docx");

//Get the PrintDocument object
PrintDocument printDoc = doc.PrintDocument;

//Print the document to file
printDoc.PrinterSettings.PrintToFile = true;

//Specify the printer name
printDoc.PrinterSettings.PrinterName = "Microsoft Print to PDF";

//Specify the output file path and name
printDoc.PrinterSettings.PrintFileName = @"C:\Users\Administrator\Desktop\ToPDF.pdf";

//Print the document
printDoc.Print();
}
}
}

[VB.NET]

Imports Spire.Doc
Imports System.Drawing.Printing

Namespace PrintWordToPdf
Class Program
Shared Sub Main(ByVal args() As String)
'Create a Document object
Document doc = New Document()

'Load a Word document
doc.LoadFromFile("C:\Users\Administrator\Desktop\input.docx")

'Get the PrintDocument object
Dim printDoc As PrintDocument = doc.PrintDocument

'Print the document to file
printDoc.PrinterSettings.PrintToFile = True

'Specify the printer name
printDoc.PrinterSettings.PrinterName = "Microsoft Print to PDF"

'Specify the output file path and name
printDoc.PrinterSettings.PrintFileName = "C:\Users\Administrator\Desktop\ToPDF.pdf"

'Print the document
printDoc.Print()
End Sub
End Class
End Namespace

--

--

Alexander Stock

I'm Alexander Stock, a software development consultant and blogger with 10+ years' experience. Specializing in office document tools and knowledge introduction.