XPS is a file format that is started to be commonly used with the release of Windows Vista. Word, Excel, PowerPoint files can be translated into XPS format and can be displayed even in a computer in which Office is not installed via the help of an explorer program such as Internet Explorer 7. In this post, I will try to explain how to display XPS formatted files in WPF (Windows Presentation Foundation) applications with DocumentViewer control.
We need an XpsDocument object in order to display a XPS file on DocumentView that is a control comes with WPF. Because this class is not included in mscorlib.dll, we must add ReachFramework.dll to our project.
Adding ReachFramework.dll to project's references
We can move to our application part after adding related dll file to our project references. In this part the procedure is very simple actually; we add a DocumentViewer control to our application form and we add two code lines that can be seen below to the Loaded event of the application form.
Window1.xaml
<Window x:Class="WpfDocumentViewerXps.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DocumentViewer ile XPS Dosyalarının Görüntülemesi" Height="320" Width="600">
<Grid>
<DocumentViewer Name="documentViewer1" />
</Grid>
</Window>
Window1.xaml.cs
...
using System.Windows.Xps.Packaging; //Required namespace
namespace WpfDocumentViewerXps
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//With GetFixedDocumentSequence method, XpsDocument can get XPS file content
XpsDocument xps = new XpsDocument(@"D:\test.xps", System.IO.FileAccess.Read);
documentViewer1.Document = xps.GetFixedDocumentSequence();
}
}
}
XPS document can be seen on DocumentViewer control