Working with FixedDocuments in WPF



This article introduces the FixedDocument class, the DocumentViewer control, and describes how to work with FixedDocuments in WPF.

WPF provides support to work with documents. It considers documents to be classified as either fixed documents or flow documents.

You use fixed documents when you want a "what you see is what you get" (WYSIWYG) display. Here, maintaining the original design of a document even after it is rendered in WPF is very important. Scenarios such as desktop publishing, form layout, and so forth will need the use of fixed documents. Flow documents, on the other hand, are primarily used for reading and viewing documents, rather than laying focus on the design and formatting.

Here, we will look at using a FixedDocument inside a WPF Grid.

Create a new WPF application named WPFDemo.

The default MainWindow.xaml will look as follows:

<Window x:Class="WpfDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
    </Grid>
</Window>


From the Toolbox, drag and drop a DocumentViewer control between the <Grid></Grid> class. This control provides you the ability to view documents and host content such as an XpsDocument. Such content is represented using a FixedDocument.

Add a FixedDocument between the DocumentViewer tags as shown in the following markup:

<Window x:Class="WpfDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DocumentViewer x:Name="documentViewer1">
            <FixedDocument>

            </FixedDocument>
        </DocumentViewer>
    </Grid>
</Window>


Right-click References under the project name in the Solution Explorer.

Select Add Reference and in the Add Reference dialog box, choose ReachFramework.

FixdocWPF1.gif

Figure 1

This framework defines the System.Windows.Xps namespace. The ReachFramework.dll is present under Program Files\Reference Assemblies\Microsoft\Framework\<version>. When you create a Visual Studio 2008 or 2010 project, the ReachFramework.dll is not automatically referenced. That's why you must manually add a reference to it.

In the code-behind of MainWindow.xaml, that is, MainWindow.xaml.cs, add the following code:

using System.Windows.Xps.Packaging;

...
InitializeComponent();
XpsDocument document1 = new XpsDocument(@"d:\test.xps", System.IO.FileAccess.Read);
documentViewer1.Document = document1.GetFixedDocumentSequence();


This code will read the contents of the XPS document and populate it into the fixed document inside the DocumentViewer control. Hence, when you build and execute the application, you will see the contents of the XPS document. Note, if you don't know how to create an XPS document, just open any PDF, click Print and select Microsoft XPS Document Writer as the printer. When you click OK, it will prompt you for filename and location. Specify desired file name and location but then you will have to edit the code given above to include that file name and location.

The outcome of the application with a sample XPS document is shown in Figure 2.

FixdocWPF2.gif

Figure 2

It is possible to control the visual appearance and style of the DocumentViewer control by using a ControlTemplate.

The following markup shows an example of this:

<DocumentViewer x:Name="documentViewer1">
            <DocumentViewer.Template>
                <ControlTemplate>
                    <Border>
                        <Border.BorderThickness>
                            <Thickness Bottom="8" Left="8" Right="8" Top="8"></Thickness>
                        </Border.BorderThickness>
                        <Border.Background>
                            <SolidColorBrush x:Name="BorderBrush" Color="DarkMagenta"/>
                        </Border.Background>
                        <Border>
                            <Border.BorderThickness>
                                <Thickness Bottom="4" Left="4" Right="4"
                                Top
="4"></Thickness>
                            </Border.BorderThickness>
                            <Border.Background>
                                <SolidColorBrush x:Name="InnerBorderBrush"
                                       Color
="LightCoral"/>
                            </Border.Background>

                            <ScrollViewer CanContentScroll="true"
              HorizontalScrollBarVisibility="Auto"
              x:Name="scrollviewer1"
              IsTabStop="true"/>
                        </Border>
                    </Border>
                </ControlTemplate>
            </DocumentViewer.Template>
            <FixedDocument>
            </FixedDocument>
        </DocumentViewer>

This markup renders an outside border and an inside border and a scroll bar for the document.

The outcome of this is shown in figure 3:

FixdocWPF3.gif

Figure 3

Similarly, you can try various other styles with the DocumentViewer control. You can also customize other controls inside the DocumentViewer control template such as toolbuttons and so forth.

Conclusion: This article discussed how to use the FixedDocument class to display XPS documents in a DocumentViewer control.