Create Document Viewer Using Spire.DocViewer


This article demonstrates how to create word document viewer with spire doc viewer library in WPF. This demonstrator opens a word document in doc viewer, this doc viewer has features like zooming, saving, printing etc.

Introduction



In my Wprevious article i have explained how to create word document using Spire Doc Library, in this article illustration describes how to create document (Word) Viewer in WPF using Spire.DocViewer. Here we are doing below task with wpf spre doc viewer


  • Open Word Document

  • Save Work Document(Save as)

  • Print Document

  • Zooming

  • Closing Document




Before starting illustration, i would like to describe how to add Spire Doc Viewer control into WPF toolbox. The Spire.DocViewer for WPF is not available in Manage NuGet Packages. hence you have to download the library from spire web sit, Click here to visit and download the library.


After downloading the link you will get spire.docviewer exe file with in zip file, extract the zip and install the spire.docviewer exe file into your computer. By default this will be installed in the path C:\Program Files\e-iceblue\Spire.DocViewer or C:\Program Files (x86)\e-iceblue\Spire.DocViewer


Add Spire.DocViewer Controls into Toolbox



Right click on the blank part of toolbox in Visual Studio. Click on Add Tab, a new tab panel will be added into toolbox and rename that tab to Spire Doc Controls.
Ritht click on the blank part below Spire Doc Controls =>choose items. Choose toolbox items window will open=> click on WPF Components tab


spiretool1


Then click on the Browse button open the installed path of Spire Doc Viewer=>Open bin folder=>Open the appropreate version of wpf folder then select the Spire.DocViewer.Wpf.dll. This task will add spire doc viewer tool into your WPF toolbox.


spiretool


Now we will discuss how to achieve all the features of Spire Doc Viewer, which i have mentioned above of this article. In this article in button click event i have illustrate every feature of Spire doc viewer, hence i have place some button on the top of the WPF window like this.


 














spiretool2

After placing buttons i have place Spire Document Viewer control in window like below
 


















Opening Word Document



private void btnOpen_Click(object sender, RoutedEventArgs e)
{
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");

if (dialog.ShowDialog().Value == true)
{
try
{
//Load DOC document from file.
this.viewer1.LoadFromFile(dialog.FileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}


Above code is for opening word document in the spire document viewer. from line no 03 to 07 is for creating object of open file dialog which helps find out the document you want to open it and sets some properties of dialog box like filter, title etc.



code no 09 opens dialog box and checks whether user has pressed OK button of dialog box, if user presses ok button of dialog box the value dialog result will be true and code not 14 will be executed. The LoadFromFile method of document viewer (here viewer1 is document viewer) helps display document content in the document viewer, by default name of the document which we have provide in this above code with the help of open file dialog


Saving Work Document(Save as)



With the help of Spire Doc Viewer, you can save the opened document in your drive. You can save it any format, here in this illustration The Spire doc viewer save in three format(.doc, .docx and .pdf).The below code helps to save the document.


 
private void btnSaveAS_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog savefile = new SaveFileDialog();
savefile.Filter = "Word97-2003 files(*.doc)|*.doc|Word2007-2010 files (*.docx)|*.docx|Pdf Document(*.pdf)|*.pdf";
savefile.Title = "Save";
if (savefile.ShowDialog().Value == true)
{
try
{
//Save PDF documetns
this.viewer1.SaveAs(savefile.FileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}


Code 03 to code 05 creates object of Save file dialog box which helps to find out path, where you want save the document and sets some properties of save file dialog like filter, title etc.



SaveAs function in code of spire document viewer controls helps to save document in the drive. here code 10 uses the function to save file in drive and this function takes filename as default parameter, which we have provided with the help of save file dialog.


Printing Document


 
private void btnPrint_Click(object sender, RoutedEventArgs e)
{
PrintDialog dialog = new PrintDialog();
dialog.CurrentPageEnabled = true;
dialog.PageRangeSelection = PageRangeSelection.AllPages;
if (dialog.ShowDialog().Value == true)
{
this.viewer1.PrintDialog = dialog;
dialog.PrintDocument(this.viewer1.PrintDocument.DocumentPaginator, "Print");
}

}


code no 03 to 04 creates object of print dialog and sets the page property, code 06 open print dialog and if user click print button then the dialog box return true value. code 08 sets the print dialog of spire document viewer to .net print dialog and code 09 calls function PrintDocument of Print dialog.


Zooming Document



WPF spire document viewer provide four types of zooming. that are Default




  1. Default

  2. Fit To Page

  3. Fit To Width

  4. Percent zoom




For 1,2,3 type zooming WPF spire document viewer provide following code, what i have used.


 
this.viewer1.ZoomMode = ZoomMode.Default;
this.viewer1.ZoomMode = ZoomMode.FitPage;
this.viewer1.ZoomMode = ZoomMode.FitWidth;


For percent zooming spire document viewer privides a function called ZoomTo, by default it takes numberic value to set document size.
Zooming Code of this article


 
private void btnFPage_Click(object sender, RoutedEventArgs e)
{
this.viewer1.ZoomMode = ZoomMode.FitPage;
this.ZoomSize = 100;

}

private void btnFWidth_Click(object sender, RoutedEventArgs e)
{
this.viewer1.ZoomMode = ZoomMode.FitWidth;
this.ZoomSize = 150;
}

private void btnDefault_Click(object sender, RoutedEventArgs e)
{
this.viewer1.ZoomMode = ZoomMode.Default;

}

private void btnPlus_Click(object sender, RoutedEventArgs e)
{
this.ZoomSize = this.ZoomSize + 10;
this.viewer1.ZoomTo(this.ZoomSize);

}

private void btnMinus_Click(object sender, RoutedEventArgs e)
{
if (this.ZoomSize > 10)
this.ZoomSize = this.ZoomSize - 10;

this.viewer1.ZoomTo(this.ZoomSize);
}


In the above code the variable ZoomSize is global variable for contains current zoom size of document. it is used in the Zoomto function of document viewer control of increasing percentage zooming. The default zoom size of document in document viewer control is 100, hence the default value of zoom size variable is 100.


Closing Document



The CloseDocument function of document viewer control is for closing document. see the below code



private void btnClose_Click(object sender, RoutedEventArgs e)
{
this.viewer1.CloseDocument();
}

Full xaml code
 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DocViewer"
xmlns:Wpf="clr-namespace:Spire.DocViewer.Wpf;assembly=Spire.DocViewer.Wpf" x:Class="DocViewer.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">



















Full code behinds
 
using Microsoft.Win32;
using Spire.DocViewer.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DocViewer
{
///
/// Interaction logic for MainWindow.xaml
///

public partial class MainWindow : Window
{
private int ZoomSize = 100;
public MainWindow()
{
InitializeComponent();
}

private void btnOpen_Click(object sender, RoutedEventArgs e)
{
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");

if (dialog.ShowDialog().Value == true)
{
try
{
//Load DOC document from file.
this.viewer1.LoadFromFile(dialog.FileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}

private void btnClose_Click(object sender, RoutedEventArgs e)
{
this.viewer1.CloseDocument();
}

private void btnFPage_Click(object sender, RoutedEventArgs e)
{
this.viewer1.ZoomMode = ZoomMode.FitPage;
this.ZoomSize = 100;

}

private void btnFWidth_Click(object sender, RoutedEventArgs e)
{
this.viewer1.ZoomMode = ZoomMode.FitWidth;
this.ZoomSize = 150;
}

private void btnDefault_Click(object sender, RoutedEventArgs e)
{
this.viewer1.ZoomMode = ZoomMode.Default;

}

private void btnPlus_Click(object sender, RoutedEventArgs e)
{
this.ZoomSize = this.ZoomSize + 10;
this.viewer1.ZoomTo(this.ZoomSize);

}

private void btnMinus_Click(object sender, RoutedEventArgs e)
{
if (this.ZoomSize > 10)
this.ZoomSize = this.ZoomSize - 10;

this.viewer1.ZoomTo(this.ZoomSize);
}

private void btnPrint_Click(object sender, RoutedEventArgs e)
{
PrintDialog dialog = new PrintDialog();
dialog.CurrentPageEnabled = true;
dialog.PageRangeSelection = PageRangeSelection.AllPages;
if (dialog.ShowDialog().Value == true)
{
this.viewer1.PrintDialog = dialog;
dialog.PrintDocument(this.viewer1.PrintDocument.DocumentPaginator, "Print");
}

}

private void btnSaveAS_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog savefile = new SaveFileDialog();
savefile.Filter = "Word97-2003 files(*.doc)|*.doc|Word2007-2010 files (*.docx)|*.docx|Pdf Document(*.pdf)|*.pdf";
savefile.Title = "Save";
if (savefile.ShowDialog().Value == true)
{
try
{
//Save PDF documetns
this.viewer1.SaveAs(savefile.FileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}
}


spiretool3


In the above we have discussed how to create document viewer using WPF spire doc viewer and the features of spire doc viewer. Hope you have got the discussion.
Thanks


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: