Export PDF file to XpsDocument in WPF

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

PDF file exported to XpsDocument with GemBox.Pdf
Screenshot of PDF file exported to XpsDocument with GemBox.Pdf
<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Export to XpsDocument Example" 
        SizeToContent="WidthAndHeight">

    <DockPanel>
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="_File">
                <MenuItem Header="_Open" Click="MenuItem_Click" />
            </MenuItem>
        </Menu>
        <DocumentViewer x:Name="DocumentViewer"/>
    </DockPanel>

</Window>
using System.Windows;
using System.Windows.Xps.Packaging;
using GemBox.Pdf;
using Microsoft.Win32;

public partial class MainWindow : Window
{
    XpsDocument xpsDocument;

    public MainWindow()
    {
        InitializeComponent();

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

    private void MenuItem_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog fileDialog = new OpenFileDialog();
        fileDialog.Filter = "PDF files (*.pdf)|*.pdf";

        if (fileDialog.ShowDialog() == true)
        {
            using (var document = PdfDocument.Load(fileDialog.FileName))
            {
                // 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.Xps);

                this.DocumentViewer.Document = this.xpsDocument.GetFixedDocumentSequence();
            }
        }
    }
}
Imports System.Windows.Xps.Packaging
Imports GemBox.Pdf
Imports Microsoft.Win32

Class MainWindow

    Dim xpsDoc As XpsDocument

    Public Sub New()

        InitializeComponent()

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

    Private Sub MenuItem_Click(sender As Object, e As RoutedEventArgs)

        Dim fileDialog = New OpenFileDialog()
        fileDialog.Filter = "PDF files (*.pdf)|*.pdf"

        If (fileDialog.ShowDialog() = True) Then
            Using document = PdfDocument.Load(fileDialog.FileName)
                ' XpsDocument needs to stay referenced so that DocumentViewer can access additional required resources.
                ' Otherwise, GC will collect/dispose XpsDocument and DocumentViewer will not work.
                Me.xpsDoc = document.ConvertToXpsDocument(SaveOptions.Xps)

                Me.DocumentViewer.Document = Me.xpsDoc.GetFixedDocumentSequence()
            End Using
        End If

    End Sub
End Class

See also


Next steps

GemBox.Pdf is a .NET component that enables developers to read, merge and split PDF files or execute low-level object manipulations from .NET applications in a simple and efficient way.

Download Buy