How to print a Word document in C#

Document printing is very commonly used in work. This article will introduce a method for printing in Word documents through the third-party free component Free Spire.Doc for .NET in C#. There are two alternative printing methods, namely silent printing and pop-up dialog printing. The article is reproduced from http://www.cnblogs.com/Yesi/p/6000247.html 

The screenshot of the original Word document that needs to be printed is as follows:



 The following is the entire code to achieve document printing, for reference:

using System;
using Spire.Doc;
using System.Windows.Forms;

namespace Doc_Print
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Create a Documnet class object
            Document doc = new Document();
            // Load the Word document that needs to be printed
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample Document.doc");
            // Instantiate the System.Windows.Forms.PrintDialog object
            PrintDialog dialog = new PrintDialog();
            dialog.AllowPrintToFile = true;
            dialog.AllowCurrentPage = true;
            dialog.AllowSomePages = true;
            dialog.UseEXDialog = true;
            // Associate the doc.PrintDialog property with the PrintDialog object
            doc.PrintDialog = dialog;
            // background printing
            // PrintDocument printDoc = doc.PrintDocument;        
            // printDoc.Print();
            // show the print dialog and print
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                //printDoc.Print();
            }

        }
    }
}

Screenshot in XPS format after printing the document:

  

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326440607&siteId=291194637