With the release of .NET Framework 3.0, Microsoft introduced a new file format called XPS (XML Paper Specifications) with the .Xps extension. This file format was thought of as a standard for documents portability; in fact, XPS documents can be read on every system without having installed the originating application (as it is for PDF documents).
.NET Framework 3.x fully supports this kind of documents which can be created and manipulated with extreme precision. Manipulating this kind of documents via code (particularly writing) is not often simple, especially if you need to add pictures, thumbnails, digital signatures, and other supported contents.
Even if the .NET Framework 3.x exposes several classes to read, write, and manage XPS documents, in this article, I want to show you how to create XPS documents starting from Microsoft Word 2007 documents (.docx and .doc formats) with very few steps.
In fact, we can create XPS documents by writing a few lines of Visual Basic 2008 code and referencing Visual Studio Tools for Office main assemblies. We’ll see this in action inside a WPF application. Two are the prerequisites you must adhere to: you need to have Microsoft Word 2007 installed and the XPS/PDF exporter add-in.
Using the code
First of all, open Visual Studio 2008 and create a new WPF Application for Visual Basic. Then, add a reference to the following assemblies:
As you can understand, we’re adding a reference to all VSTO assemblies for Visual Studio 2008 but without creating an Office solution.
On the presentation layer, we want to let the user choose a Word 2007 document by clicking a button and automatically show the exported document from within a WPF
DocumentViewer control. The following XAML code could accomplish this:
Title="Window1" Height="300" Width="300">
<DocumentViewer Grid.Row="1" Name="DocumentViewer1" />
Select a document</Button>
Then, we can go to write managed code. First of all, we have to add the following
Second, we could write a method to export to XPS the Word document selected by the user. Comments within the code should be helpful:
Private Function exportAsXps(ByVal fileName As String) As Boolean
Dim wordInstance As New _
‘replacing the extension with.Xps
Dim outputName As String = _
String.Concat(IO.Path.GetDirectoryName(fileName), "\", _
‘and saves specifying name and format
Dim doc As Document = wordInstance.ActiveDocument
Dim xpsDoc As New XpsDocument(outputName, IO.FileAccess.Read)
DocumentViewer1.Document = xpsDoc.GetFixedDocumentSequence
Catch ex As Exception
MessageBox.Show(ex.ToString, "", _
Once we have our method, using it is really simple. We can call it from the
Click event handler for the button we implemented to select a Word document:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
Dim ofd As New OpenFileDialog
Dim result As Nullable(Of Boolean)
.Title = "Select a Word document"
.Filter = "Documents|*.docx;*.doc|All files|*.*"
result = .ShowDialog
If result = True Then
Dim exportResult As Boolean = exportAsXps(.FileName)
The above picture shows the result of the conversion of a .docx (Word 2007) document of mine.
Points of Interest
With very few lines of code and without having Microsoft Word executing, we can create XPS documents from our Word contents, even when very articulated. The code can be surely enhanced, but I think it can be a good starting point if you’re interested in this kind of operations.
Using this technique, you can export documents not only to XPS but also to all formats which are supported by Microsoft Word 2007.
I'm a Microsoft Visual Basic MVP. I'm an Italian .NET developer and I write articles and books about Visual Basic, Visual Studio LightSwitch, and the .NET technologies.
Check out my blog at: http://community.visual-basic.it/AlessandroEnglish