View and Convert Word File in Winform using Spire.DocViewer

This article is focusing on how to create a Windows Forms Application that enable users to:

  • Open and view an existing Word file
  • Close the open file
  • Save Doc file as other format file such as PDF, Txt, Docx

Before start we need to download Spire.DocViewer and install it on system, which is a Word viewer control capable of displaying and processing Word file at any .NET platform. After installation, you can follow the tutorials in below link to add Spire.DocViewer controls to the Toolbox of your Visual Studio.

http://www.e-iceblue.com/Tutorials/Spire.Doc/Demos/Quick-guide/How-to-add-Controls-to-Toolbox.html

DocViewerWinForm_Toolbox_6

In the following section, let’s create the application step by step.

Step 1: Create a Windows Forms Application and design form1 as below.

  • Add “Open” button under “File” to open an existing Word file.
  • Add “Exit” button under “File” to shut down the procedure.
  • Add “Close” button to close the open file.
  • Add “ToPDF”, ”ToTxt”, “ToDocx” button under “Save” to convert .doc file as other format file.
  • Drag “DocDocumentViewer Control” into Form1, which is used to display Word document.

Form-1

Form-2

Step 2: Add Spire.Doc.dll and Spire.DocViewer.Forms.dll to Reference.

reference

Step 3: Use following code to implement desired functions.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace View_Word_in_Winform

{

    public partial class Form1 : Form

    {

        public Form1()

        {

           InitializeComponent();

        }

 

        private void openToolStripMenuItem_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 file

           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

               {

                   //Load DOC document from file.

                   this.docDocumentViewer1.LoadFromFile(dialog.FileName);

               }

               catch (Exception ex)

               {

                    MessageBox.Show(ex.Message, “Error!”, MessageBoxButtons.OK,
MessageBoxIcon.Error);

               }

            }

        }

        private void btnClose_Click(object sender, EventArgs e)

        {

            //Close current doc document.

            this.docDocumentViewer1.CloseDocument();

        }

 

        private void eixtToolStripMenuItem_Click(object sender, EventArgs e)

        {

            //Close the program

            this.Close();

        }

        private void toPDFToolStripMenuItem_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 as PDF documetns

                   this.docDocumentViewer1.SaveAs(savefile.FileName);

               }

               catch (Exception ex)

               {

                   MessageBox.Show(ex.Message, “Error”, MessageBoxButtons.OK, MessageBoxIcon.Error);

               }

            }

        }

        private void toTxtToolStripMenuItem_Click(object sender, EventArgs e)

        {

            SaveFileDialog savefile = new SaveFileDialog();

           savefile.Filter = “Txt(*.txt)|*.txt”;

           savefile.Title = “Save to Word2007-2010”;

 

            DialogResult result = savefile.ShowDialog();

 

            if (result == DialogResult.OK)

            {

               try

               {

                   //Save as Text File

                   this.docDocumentViewer1.SaveAs(savefile.FileName);

               }

               catch (Exception ex)

                {

                   MessageBox.Show(ex.Message, “Error”, MessageBoxButtons.OK, MessageBoxIcon.Error);

               }

            }

         }  

        private void toDocxToolStripMenuItem_Click(object sender, EventArgs e)

        {

            SaveFileDialog savefile = new SaveFileDialog();

           savefile.Filter = “Word2007-2010 Document(*.docx)|*.docx”;

           savefile.Title = “Save to Word2007-2010”;

 

            DialogResult result = savefile.ShowDialog();

 

            if (result == DialogResult.OK)

            {

               try

               {

                   //Save as Word2007-2010 documetns

                   this.docDocumentViewer1.SaveAs(savefile.FileName);

               }

               catch (Exception ex)

               {

                   MessageBox.Show(ex.Message, “Error”, MessageBoxButtons.OK, MessageBoxIcon.Error);

               }

            }      

        }

Run the program and open a test document (.doc or .docx), you’ll get following result. If you would like to export the .doc file as PDF, Text, Docx file, just click Save to find the conversion function.

2015-11-03_095914

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s