Export Word to XpsDocument in WPF

Besides converting a document to a different file format, GemBox.Document also supports converting a document to an XpsDocument object with the DocumentModel.ConvertToXpsDocument method.

If you require more than displaying a file's content, for instance if you want to be able to modify it with some GUI control, take a look at this Word Editor in WPF example.

The following example shows how you can convert a Word file to XPS and attach it to WPF's DocumentViewer control.

Converted Word file to XpsDocument and attached to WPF's DocumentViewer control
Screenshot of Word file exported to WPF's DocumentViewer control
<Window x:Class="ConvertToXpsDocument.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Export to XpsDocument / DocumentViewer Control"
        SizeToContent="WidthAndHeight">

    <DocumentViewer x:Name="DocumentViewer"/>

</Window>
using System.Windows;
using System.Windows.Xps.Packaging; // Add reference for 'ReachFramework'.
using GemBox.Document;

namespace ConvertToXpsDocument
{
    public partial class MainWindow : Window
    {
        private XpsDocument xpsDocument;

        public MainWindow()
        {
            this.InitializeComponent();

            // If using Professional version, put your serial key below.
            ComponentInfo.SetLicense("FREE-LIMITED-KEY");

            this.SetDocumentViewer("%#Reading.docx%");
        }

        private void SetDocumentViewer(string path)
        {
            var document = DocumentModel.Load(path);

            // XpsDocument object must stay referenced so that DocumentViewer can access its resources.
            // Otherwise, GC will collect/dispose XpsDocument and DocumentViewer will no longer work.
            this.xpsDocument = document.ConvertToXpsDocument(SaveOptions.XpsDefault);

            this.DocumentViewer.Document = this.xpsDocument.GetFixedDocumentSequence();
        }
    }
}
Imports System.Windows
Imports System.Windows.Xps.Packaging ' Add reference for 'ReachFramework'.
Imports GemBox.Document

Namespace ConvertToXpsDocument
    Partial Public Class MainWindow
        Inherits Window

        Private xpsDocument As XpsDocument

        Public Sub New()

            Me.InitializeComponent()

            ' If using Professional version, put your serial key below.
            ComponentInfo.SetLicense("FREE-LIMITED-KEY")

            Me.SetDocumentViewer("%#Reading.docx%")

        End Sub

        Private Sub SetDocumentViewer(path As String)

            Dim document = DocumentModel.Load(path)

            ' XpsDocument object must stay referenced so that DocumentViewer can access its resources.
            ' Otherwise, GC will collect/dispose XpsDocument and DocumentViewer will no longer work.
            Me.xpsDocument = document.ConvertToXpsDocument(SaveOptions.XpsDefault)

            Me.DocumentViewer.Document = Me.xpsDocument.GetFixedDocumentSequence()

        End Sub

    End Class
End Namespace

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Document yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.