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.

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.Presentation;
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            PresentationDocument presentation = new PresentationDocument();
24
25            Slide slide = presentation.Slides.AddNew(SlideLayoutType.Custom);
26
27            GemBox.Presentation.TextBox textBox = slide.Content.AddTextBox(
28                ShapeGeometryType.Rectangle, 2, 2, 8, 4, LengthUnit.Centimeter);
29            textBox.Shape.Format.Outline.Fill.SetSolid(Color.FromName(ColorName.DarkGray));
30
31            TextRun run = textBox.AddParagraph().AddRun("Hello World!");
32            run.Format.Fill.SetSolid(Color.FromName(ColorName.Black));
33
34            // XpsDocument needs to stay referenced so that DocumentViewer 
35            // can access additional required resources.
36            // Otherwise, GC will collect/dispose XpsDocument and DocumentViewer 
37            // will not work.
38            this.xpsDocument = presentation.ConvertToXpsDocument(SaveOptions.Xps);
39
40            documentViewer.Document = this.xpsDocument.GetFixedDocumentSequence();
41        }
42    }
43}
1Imports System.Windows.Xps.Packaging
2Imports GemBox.Presentation
3
4Class MainWindow
5
6    Dim xpsDocument As XpsDocument
7
8    Public Sub New()
9        InitializeComponent()
10
11        SetDocumentViewer(Me.DocumentViewer)
12    End Sub
13
14    Private Shared Sub SetDocumentViewer(documentViewer As DocumentViewer)
15
16        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
17
18        Dim presentation = New PresentationDocument()
19
20        Dim slide As Slide = presentation.Slides.AddNew(SlideLayoutType.Custom)
21
22        Dim textBox As TextBox = slide.Content.AddTextBox(
23            ShapeGeometryType.Rectangle, 2, 2, 8, 4, LengthUnit.Centimeter)
24        textBox.Shape.Format.Outline.Fill.SetSolid(Color.FromName(ColorName.DarkGray))
25
26        Dim run As TextRun = textBox.AddParagraph().AddRun("Hello World!")
27        run.Format.Fill.SetSolid(Color.FromName(ColorName.Black))
28
29        Dim xpsDocument = presentation.ConvertToXpsDocument(SaveOptions.Xps)
30        documentViewer.Document = xpsDocument.GetFixedDocumentSequence()
31
32        ' XpsDocument needs to stay referenced so that DocumentViewer 
33        ' can access additional required resources.
34        ' Otherwise, GC will collect/dispose XpsDocument and DocumentViewer 
35        ' will not work.
36        xpsDocument = presentation.ConvertToXpsDocument(SaveOptions.Xps)
37
38        documentViewer.Document = xpsDocument.GetFixedDocumentSequence()
39
40    End Sub
41
42End Class

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