Export Word to XpsDocument in WPF

Following example shows how to convert document to XpsDocument instance and attach it to WPF's DocumentViewer control.

Screenshot
Export to XpsDocument / DocumentViewer Control Screenshot

See the full code below.

<Window x:Class="ConvertToXpsDocumentCs.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Export to XpsDocument Sample" 
        SizeToContent="WidthAndHeight">

    <DocumentViewer x:Name="DocumentViewer"/>
    
</Window>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Xps.Packaging;
using GemBox.Document;

namespace ConvertToXpsDocumentCs
{
    public partial class MainWindow : Window
    {
        XpsDocument xpsDocument;

        public MainWindow()
        {
            InitializeComponent();

            this.SetDocumentViewer(this.DocumentViewer);
        }

        private void SetDocumentViewer(DocumentViewer documentViewer)
        {
            ComponentInfo.SetLicense("FREE-LIMITED-KEY");

            DocumentModel document = new DocumentModel();

            var section = new GemBox.Document.Section(document);
            document.Sections.Add(section);

            var paragraph = new GemBox.Document.Paragraph(document);
            section.Blocks.Add(paragraph);

            var run = new GemBox.Document.Run(document, "Hello World!");
            paragraph.Inlines.Add(run);

            // XpsDocument needs to stay referenced so that DocumentViewer can access additional required resources.
            // Otherwise, GC will collect/dispose XpsDocument and DocumentViewer will not work.
            this.xpsDocument = document.ConvertToXpsDocument(SaveOptions.XpsDefault);      
      
            documentViewer.Document = this.xpsDocument.GetFixedDocumentSequence();
        }
    }
}
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Xps.Packaging
Imports GemBox.Document

Class MainWindow

    Dim xpsDocument As XpsDocument

    Public Sub New()
        InitializeComponent()

        SetDocumentViewer(Me.DocumentViewer)
    End Sub

    Private Shared Sub SetDocumentViewer(documentViewer As DocumentViewer)

        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document = New DocumentModel()

        Dim section = New GemBox.Document.Section(document)
        document.Sections.Add(section)

        Dim paragraph = New GemBox.Document.Paragraph(document)
        section.Blocks.Add(paragraph)

        Dim run = New GemBox.Document.Run(document, "Hello World!")
        paragraph.Inlines.Add(run)

        Dim xpsDocument = document.ConvertToXpsDocument(SaveOptions.XpsDefault)
        documentViewer.Document = xpsDocument.GetFixedDocumentSequence()

        ' XpsDocument needs to stay referenced so that DocumentViewer can access additional required resources.
        ' Otherwise, GC will collect/dispose XpsDocument and DocumentViewer will not work.
        xpsDocument = document.ConvertToXpsDocument(SaveOptions.XpsDefault)

        documentViewer.Document = xpsDocument.GetFixedDocumentSequence()

    End Sub

End Class