Print a Word Document Programmatically in 5 Steps

Background

This technical article mainly discuss the method that how Spire.Doc fulfill its printing function. Word document developed by Microsoft is now widely used for office users. It is a common need when people need to print word document through a printer. While for programmers, it would not be a simple task to load word document into program and pint it out. Especially when it comes to web printing, it is difficult to realize background printing without interaction.

Aimed at these two points, we developed Spire.Doc for .NET, by which you can easily load word document into your .NET applications, and print it entirely out. In fact, it can be applied in console platform, WinForm, and Asp.net application.

Approach

The following method is based on the example of console platform. Firstly, you can click here to download Spire.Doc for .NET. Below is the screenshot of the original word document:

Step1: Create a new project in Visual Studio. Then set its target framework to be .NET framework 2.0. Add reference assembly "System.windows.forms.dll, System.Drawing.dll, Spire.Doc.dll"

Step2: Instantiated an object of Spire.Doc.Document and call its method of LoadFromFile in your project to load Word document.

[C#]
Document doc = new Document(); 
doc.LoadFromFile("sample.doc");

Step3: Instantiated an object of System.Windows.Forms.PrintDialog and set some of its relative properties. Here, one key point has to be noticed that you should associate System.Windows.Forms.PrintDialog object with Spire.Doc.Document while in this process.

[C#]
PrintDialog dialog = new PrintDialog();
dialog.AllowPrintToFile = true; 
dialog.AllowCurrentPage = true;
dialog.AllowSomePages = true;
dialog.UseEXDialog = true;                     
doc.PrintDialog = dialog;  

Step4: Start background printing. You should get System.Drawing.Printing.PrintDocument object by using PrintDocument properties of Spire.Doc.Document. Now you can print entire pages with default printer without interaction. (This printing mode is also suitable for Web printing)

[C#]
PrintDocument printDoc = doc.PrintDocument;
printDoc.Print();

Step5: Implement interaction printing.Call the ShowDialog method of System.Windows.Forms.PrintDialog to print word document

[C#]
if (dialog.ShowDialog() == DialogResult.OK)
{               
printDoc.Print();
}

The following screenshot reveals the result after printing word document to XPS format:

This is the complete c# code throughout this process:

[C#]
using System;
using System.Collections.Generic;
using System.Text;
using Spire.Doc;
using System.Windows.Forms;
using System.Drawing.Printing;


namespace Doc_Print
{
    class Program
    {
        static void Main(string[] args)
        {
            // Instantiated an object of Spire.Doc.Document
            Document doc = new Document(); 
            //Load word document 
            doc.LoadFromFile("sample.doc");
            // Instantiated System.Windows.Forms.PrintDialog object .
            PrintDialog dialog = new PrintDialog();
            dialog.AllowPrintToFile = true; 
            dialog.AllowCurrentPage = true;
            dialog.AllowSomePages = true;
            dialog.UseEXDialog = true;
       // associate System.Windows.Forms.PrintDialog object with Spire.Doc.Document  
            doc.PrintDialog = dialog;               
            PrintDocument printDoc = doc.PrintDocument;
            //Background printing  
            printDoc.Print();
            //Interaction printing  
            if (dialog.ShowDialog() == DialogResult.OK)
            {               
                printDoc.Print();
            }            

        }
    }
}