Export PowerPoint to XpsDocument in WPF

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

PowerPoint exported to XpsDocument with GemBox.Presentation
Screenshot of PowerPoint exported to XpsDocument with GemBox.Presentation
<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">

    <DocumentViewer x:Name="DocumentViewer"/>

</Window>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Xps.Packaging;
using GemBox.Presentation;

public partial class MainWindow : Window
{
    XpsDocument xpsDocument;

    public MainWindow()
    {
        InitializeComponent();

        this.SetDocumentViewer(this.DocumentViewer);
    }

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

        var presentation = new PresentationDocument();

        var slide = presentation.Slides.AddNew(SlideLayoutType.Custom);

        var textBox = slide.Content.AddTextBox(ShapeGeometryType.Rectangle, 2, 2, 8, 4, LengthUnit.Centimeter);
        textBox.Shape.Format.Outline.Fill.SetSolid(Color.FromName(ColorName.DarkGray));

        var run = textBox.AddParagraph().AddRun("Hello World!");
        run.Format.Fill.SetSolid(Color.FromName(ColorName.Black));

        // 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 = presentation.ConvertToXpsDocument(SaveOptions.Xps);

        documentViewer.Document = this.xpsDocument.GetFixedDocumentSequence();
    }
}
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Xps.Packaging
Imports GemBox.Presentation

Class MainWindow

    Dim xpsDoc As XpsDocument

    Public Sub New()

        InitializeComponent()

        Me.SetDocumentViewer(Me.DocumentViewer)
    End Sub

    Private Sub SetDocumentViewer(documentViewer As DocumentViewer)

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

        Dim presentation = New PresentationDocument()

        Dim slide = presentation.Slides.AddNew(SlideLayoutType.Custom)

        Dim textBox = slide.Content.AddTextBox(ShapeGeometryType.Rectangle, 2, 2, 8, 4, LengthUnit.Centimeter)
        textBox.Shape.Format.Outline.Fill.SetSolid(Color.FromName(ColorName.DarkGray))

        Dim run = textBox.AddParagraph().AddRun("Hello World!")
        run.Format.Fill.SetSolid(Color.FromName(ColorName.Black))

        ' 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 = presentation.ConvertToXpsDocument(SaveOptions.Xps)

        documentViewer.Document = Me.xpsDoc.GetFixedDocumentSequence()
    End Sub
End Class

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.Presentation 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.