This project is read-only.

DocumentViewer in WPF

Aug 8, 2012 at 11:13 PM
Edited Sep 19, 2012 at 11:22 PM

I am trying to open an XPS document in a WPF DocumentViewer without success.  I can click on my xps file and view it in xpsViewer, but not in my WPF app. The only code I could find in my books or on the internet is below. I can step through the code using the debugger, and it appears to recognize and grasp the information in my xps document, everything that is but the DocumentViewer named docViewer2. Can anyone tell me what I need to do to open this file?

The Xps Class, at the bottom, was suggested and developed by the intellisense of the display code. I’m using VS 2010 Express, and I’m beginning to suspect that the Express version doesn’t have the ability to open a WPF DocumentViewer. On page 968 of Matthew MacDonald’s book, Pro WPF in VB 2010, he states that there are FlowDocument containers (FlowDocumentReader, FlowDocumentScrollViewer, and FlowDocumentPageViewer), and for Fixed Documents (DocumentViewer). But there is neither DocumentReader or DocumentPageViewer in the Express version, only the ScrollViewer and DocumentViewer. He gives the code to load an XPS file into memory and show it in a DocumentViewer that is similar to the code below, but it doesn’t work either. What’s the answer?

I found the answer in Rod Stephens book, WPF Programmer’s Reference - 2010, by Wiley Publishing, Inc.

Finally, you can make a WPF application display an XPS file inside a DocumentViewer control.

To do that, first add a reference to the ReachFramework library.

In Expression Blend, open the Project menu and select "Add Reference." Browse to the

ReachFramework DLL (on my system, it’s at \Program Files\Reference Assemblies\Microsoft\

Framework\v3.0\ReachFramework.dll) and click Open.

In Visual Studio, open the Project menu and select "Add Reference." On the Add Reference dialog, select the .NET tab, select the ReachFramework entry, and click OK.

Now you can write code-behind to load an XPS document into a DocumentViewer control. To make using the necessary classes easier, you can add the following using statement to the program:

I could then use the code below. 

Imports System.IO
Imports System.Windows.Xps
Imports System.Windows.Xps.Packaging
' &&&&&&&&&&&& Display the new XPS document in docViewer2 &&&&&&&&&&&&
        ' Documents have to be stored in bin/Debug of VS 2012 in order for them to be found?
        Dim doc As New XpsDocument("QueueCode1.xps", FileAccess.Read)
        docViewer2.Document = doc.GetFixedDocumentSequence()