How to Print Word Document using Spire.DocViewer

This article has demonstrated how to create a Windows Forms Application to open and close Word file using Spire.DocViewer. Now, we are adding a print button to Form1 to fulfill the print feature for this Word document viewer.

Detailed Steps

Step 1: Add another button to Form1, and set the following properties for it in the Properties Window:

  • Name: btnPrint
  • DisplayStyle: Image
  • Image: Print.Properties.Resources.Print

Step 2: Double click the button to write code. In this button code, we firstly instantiate an object of System.Windows.Forms.PrintDialog and set some of its relative properties. Call ShowDialog method to make the print dialog visible.

    private void btnPrint_Click(object sender, EventArgs e)
        {
            //Show a Print Dialog
            PrintDialog dialog = new PrintDialog();
            dialog.AllowCurrentPage = true;
            dialog.AllowSomePages = true;
            dialog.UseEXDialog = true;
            DialogResult result = dialog.ShowDialog();
            //...
         }

Step 3: Set print parameters of DocDocumentViewer.PrintDialog and print the document.

            //...
                 if (result == DialogResult.OK)
            {
                try
                {
                    //Set print parameters.
                    this.docDocumentViewer1.PrintDialog = dialog;
                    //Gets the PrintDocument.
                    dialog.Document = docDocumentViewer1.PrintDocument;
                    dialog.Document.Print();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

Run the program, open an existing Word file and click 'Print' button, you'll get a dialog as below:

How to Print Word Document using Spire.DocViewer