Export Word to XpsDocument in WPF

Following sample 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>
1using System.Windows;
2using System.Windows.Controls;
3using System.Windows.Xps.Packaging;
4using GemBox.Document;
5
6namespace ConvertToXpsDocumentCs
7{
8    public partial class MainWindow : Window
9    {
10        XpsDocument xpsDocument;
11
12        public MainWindow()
13        {
14            InitializeComponent();
15
16            this.SetDocumentViewer(this.DocumentViewer);
17        }
18
19        private void SetDocumentViewer(DocumentViewer documentViewer)
20        {
21            ComponentInfo.SetLicense("FREE-LIMITED-KEY");
22
23            DocumentModel document = new DocumentModel();
24
25            var section = new GemBox.Document.Section(document);
26            document.Sections.Add(section);
27
28            var paragraph = new GemBox.Document.Paragraph(document);
29            section.Blocks.Add(paragraph);
30
31            var run = new GemBox.Document.Run(document, "Hello World!");
32            paragraph.Inlines.Add(run);
33
34            // XpsDocument needs to stay referenced so that DocumentViewer can access additional required resources.
35            // Otherwise, GC will collect/dispose XpsDocument and DocumentViewer will not work.
36            this.xpsDocument = document.ConvertToXpsDocument(SaveOptions.XpsDefault);      
37      
38            documentViewer.Document = this.xpsDocument.GetFixedDocumentSequence();
39        }
40    }
41}
1Imports System.Windows
2Imports System.Windows.Controls
3Imports System.Windows.Xps.Packaging
4Imports GemBox.Document
5
6Class MainWindow
7
8    Dim xpsDocument As XpsDocument
9
10    Public Sub New()
11        InitializeComponent()
12
13        SetDocumentViewer(Me.DocumentViewer)
14    End Sub
15
16    Private Shared Sub SetDocumentViewer(documentViewer As DocumentViewer)
17
18        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
19
20        Dim document = New DocumentModel()
21
22        Dim section = New GemBox.Document.Section(document)
23        document.Sections.Add(section)
24
25        Dim paragraph = New GemBox.Document.Paragraph(document)
26        section.Blocks.Add(paragraph)
27
28        Dim run = New GemBox.Document.Run(document, "Hello World!")
29        paragraph.Inlines.Add(run)
30
31        Dim xpsDocument = document.ConvertToXpsDocument(SaveOptions.XpsDefault)
32        documentViewer.Document = xpsDocument.GetFixedDocumentSequence()
33
34        ' XpsDocument needs to stay referenced so that DocumentViewer can access additional required resources.
35        ' Otherwise, GC will collect/dispose XpsDocument and DocumentViewer will not work.
36        xpsDocument = document.ConvertToXpsDocument(SaveOptions.XpsDefault)
37
38        documentViewer.Document = xpsDocument.GetFixedDocumentSequence()
39
40    End Sub
41
42End Class

Check next sample or find out more about GemBox.Document and GemBox Software.