Document Viewer Control in WPF
The DocumentViewer provides a very easy way to display the contents of a document, edit a template's form fields and to navigate around a document within a web browser. The server component returns standard web file formats (HTML, CSS, JS and JPG etc.), thus documents can be displayed in the browser without having to install any third party plugins, nor extensions.
WPF does not support functionality to view Microsoft Word documents but there is a work around this problem. WPF DocumentViewer control is used to display fixed documents such as an XPS (XML Paper Specification) document. We can open a Word document if we can convert a Word document to an XPS document. This conversion is possible by using Office Interop and Office Tools frameworks that is used to work with Office documents.
Add Reference to XPS and Office Interop Assemblies
Before we do any actual work, we must add reference to the following assemblies.
ReachFramework.dll
Microsoft.Office.Tools.v9.0.dll
Microsoft.Office.Tools.Word.v9.0dll
Microsoft.VisualStudio.Tools.Office.Runtime.v9.0.dll
Microsoft.Office.Interop.Word.dll
The first assembly, ReachFramework.dll hosts the functionality for XPS documents and rest of the assemblies hosts the functionality Office Interop and Office Tools support.
To add reference to these assemblies, you right click on Add Reference on the project name in Solution Explorer. On the .NET Framework, select ReachFramework and other assemblies from the list and click OK button. a sshown in Figure below-
You may have multiple assemblies installed on your machine. Make sure you select Version 12 for Microsoft.Office.Interop.Word assemblies as you see in Figure bellow, otherwise your conversion will fail.
The ConvertWordDocToXPSDoc method takes a full path of a word document file and new full path of XPS document and converts doc file to an xps file.
The DocumentViewer provides a very easy way to display the contents of a document, edit a template's form fields and to navigate around a document within a web browser. The server component returns standard web file formats (HTML, CSS, JS and JPG etc.), thus documents can be displayed in the browser without having to install any third party plugins, nor extensions.
WPF does not support functionality to view Microsoft Word documents but there is a work around this problem. WPF DocumentViewer control is used to display fixed documents such as an XPS (XML Paper Specification) document. We can open a Word document if we can convert a Word document to an XPS document. This conversion is possible by using Office Interop and Office Tools frameworks that is used to work with Office documents.
Add Reference to XPS and Office Interop Assemblies
Before we do any actual work, we must add reference to the following assemblies.
ReachFramework.dll
Microsoft.Office.Tools.v9.0.dll
Microsoft.Office.Tools.Word.v9.0dll
Microsoft.VisualStudio.Tools.Office.Runtime.v9.0.dll
Microsoft.Office.Interop.Word.dll
The first assembly, ReachFramework.dll hosts the functionality for XPS documents and rest of the assemblies hosts the functionality Office Interop and Office Tools support.
To add reference to these assemblies, you right click on Add Reference on the project name in Solution Explorer. On the .NET Framework, select ReachFramework and other assemblies from the list and click OK button. a sshown in Figure below-
You may have multiple assemblies installed on your machine. Make sure you select Version 12 for Microsoft.Office.Interop.Word assemblies as you see in Figure bellow, otherwise your conversion will fail.
Once you have added the reference to assemblies, you must import the following namespaces to your code behind.
using System.IO;
using Microsoft.Office.Interop.Word;
using Microsoft.Win32;
using System.Windows.Xps.Packaging;
using System.IO;
using Microsoft.Office.Interop.Word;
using Microsoft.Win32;
using System.Windows.Xps.Packaging;
Convert Doc to XPS
The SaveAs method of Document class available in OfficeInterop allows us to save a word document as an XPS document. However, you must make sure you have version 12 of assembly added to your project as I mentioned before.
The SaveAs method of Document class available in OfficeInterop allows us to save a word document as an XPS document. However, you must make sure you have version 12 of assembly added to your project as I mentioned before.
The ConvertWordDocToXPSDoc method takes a full path of a word document file and new full path of XPS document and converts doc file to an xps file.
private XpsDocument ConvertWordDocToXPSDoc(string wordDocName, string xpsDocName)
{
// Create a WordApplication and add Document to it
Microsoft.Office.Interop.Word.Application
wordApplication = new Microsoft.Office.Interop.Word.Application();
wordApplication.Documents.Add(wordDocName);
Document doc = wordApplication.ActiveDocument;
// You must make sure you have Microsoft.Office.Interop.Word.Dll version 12.
// Version 11 or previous versions do not have WdSaveFormat.wdFormatXPS option
try
{
doc.SaveAs(xpsDocName, WdSaveFormat.wdFormatXPS);
wordApplication.Quit();
XpsDocument xpsDoc = new XpsDocument(xpsDocName, System.IO.FileAccess.Read);
return xpsDoc;
}
catch (Exception exp)
{
string str = exp.Message;
}
return null;
}
DocumentViewer Control in XAML
<Grid> <DocumentViewer HorizontalAlignment="Left" Margin="0,42,0,0" Name="documentViewer1" VerticalAlignment="Top" Height="508" Width="766" /> <TextBox Height="29" HorizontalAlignment="Left" Margin="6,6,0,0" Name="SelectedFileTextBox" VerticalAlignment="Top" Width="276" /> <Button Content="Browse" Height="30" HorizontalAlignment="Right" Margin="0,6,353,0" Name="BrowseButton" VerticalAlignment="Top" Width="122" Click="BrowseButton_Click" /> </Grid>
No comments:
Post a Comment