How to Save Word File to PDF using Spire.DocViewer

This article presents how to create a Windows Forms Application to open a Word file and convert it to PDF format using Spire.DocViewer. In fact, conversion from Word to HTML, RTF, DOC, DOCX are also supported with this DocViewer control. You can try almost the same way demonstrated in the following section to convert Word to any other format.

First of all, download Spire.DocViewer, add DocViewer Control to VS Toolbox.

Save Word File to PDF using Spire.DocViewer

Then create a Windows Forms application, design your Form1 as below.

  • Add 'Open' button to open a file from existing Word document.
  • Add 'Close' button to shut down the open file.
  • Add 'ToPDF' button to save Word file as PDF format.
  • Drag 'DocDocumentViewer Control' into Form1, which is used to display Word document.

Save Word File to PDF using Spire.DocViewer

Code Snippet:

Step 1: Initialize components in Form1.

       public Form1()
       {
          InitializeComponent(); 
       }

Step 2: Create an OpenFileDialog to select the correct file type that we want to load, otherwise error message 'Cannot detect current file type' will appear.

       private void bntOpen_Click(object sender, EventArgs e)
        {
            //Open a doc document          
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Word97-2003 files(*.doc)|*.doc|Word2007-2010 files (*.docx)|*.docx|All files (*.*)|*.*";
            dialog.Title = "Select a DOC file";
            dialog.Multiselect = false;
            dialog.InitialDirectory = System.IO.Path.GetFullPath(@"..\..\..\..\..\..\Data");

            DialogResult result = dialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                try
                {
                    this.docDocumentViewer1.LoadFromFile(dialog.FileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

Step 3: In the 'ToPDF' button click event, create a SaveFileDialog, set filter as PDF type, then call DocDocumentViewer.SaveAs() method to save Word as PDF with customized file name.

       private void btnSaveToPdf_Click(object sender, EventArgs e)
        {
            SaveFileDialog savefile = new SaveFileDialog();
            savefile.Filter = "Pdf Document(*.pdf)|*.pdf";
            savefile.Title = "Save";

            DialogResult result = savefile.ShowDialog();
            if (result == DialogResult.OK)
            {
                try
                {
                    //Save PDF documetns
                    this.docDocumentViewer1.SaveAs(savefile.FileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

Step 4: Close the open file.

        private void btnClose_Click(object sender, EventArgs e)
        {
            //Close current doc document.
            this.docDocumentViewer1.CloseDocument();
        }

After debugging the code, I opened a sample Word file. The save dialog box will pop up by clicking 'ToPDF' button.

Save Word File to PDF using Spire.DocViewer