Print PowerPoint files in C# and VB.NET

The following example demonstrates how to print PowerPoint presentations in C# and VB.NET with default and advanced print options specified via WPF's PrintDialog box.

Note that you can print to the default printer or any other local or networked printer.

Screenshot

Print PowerPoint Screenshot

See the full code below.

<Window x:Class="PrintCs.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Printing" Height="600" Width="800">
    <DockPanel>
        <StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="2">
            <Button x:Name="LoadFileBtn" Content="Load file" Width="100" Margin="2,0" Click="LoadFileBtn_Click"/>
            <Button x:Name="SimplePrintFileBtn" Content="Print" Width="65" Click="SimplePrint_Click" Margin="2,0"/>
            <Button x:Name="AdvancedPrintFileBtn" Content="Print (select options)" Width="130" Click="AdvancedPrint_Click" Margin="2,0"/>
        </StackPanel>
        <DocumentViewer x:Name="DocViewer"/>
    </DockPanel>
</Window>
1using System.Windows;
2using System.Windows.Controls;
3using System.Windows.Xps.Packaging;
4using GemBox.Presentation;
5using Microsoft.Win32;
6
7// This is very simple project so we won't use MVVM pattern, ICommand, etc. (but we can)
8namespace PrintCs
9{
10    /// <summary>
11    /// Interaction logic for MainWindow.xaml
12    /// </summary>
13    public partial class MainWindow : Window
14    {
15        private PresentationDocument presentation;
16
17        public MainWindow()
18        {
19            ComponentInfo.SetLicense("FREE-LIMITED-KEY");
20
21            InitializeComponent();
22
23            this.EnableControls();
24        }
25
26        private void LoadFileBtn_Click(object sender, RoutedEventArgs e)
27        {
28            OpenFileDialog fileDialog = new OpenFileDialog();
29            fileDialog.Filter = "PPTX files (*.pptx, *.pptm, *.potx, *.potm)|*.pptx;*.pptm;*.potx;*.potm";
30
31            if (fileDialog.ShowDialog() == true)
32            {
33                this.presentation = PresentationDocument.Load(fileDialog.FileName);
34
35                this.ShowPrintPreview();
36                this.EnableControls();
37            }
38        }
39
40        private void SimplePrint_Click(object sender, RoutedEventArgs e)
41        {
42            // Print to default printer using default options
43            this.presentation.Print();
44        }
45
46        private void AdvancedPrint_Click(object sender, RoutedEventArgs e)
47        {
48            // We can use PrintDialog for defining print options
49            PrintDialog printDialog = new PrintDialog();
50            printDialog.UserPageRangeEnabled = true;
51
52            if (printDialog.ShowDialog() == true)
53            {
54                PrintOptions printOptions = new PrintOptions(printDialog.PrintTicket.GetXmlStream());
55
56                printOptions.FromSlide = printDialog.PageRange.PageFrom - 1;
57                printOptions.ToSlide = printDialog.PageRange.PageTo == 0 ? int.MaxValue : printDialog.PageRange.PageTo - 1;
58
59                this.presentation.Print(printDialog.PrintQueue.FullName, printOptions);
60            }
61        }
62
63        // We can use DocumentViewer for print preview (but we don't need).
64        private void ShowPrintPreview()
65        {
66            // XpsDocument needs to stay referenced so that DocumentViewer can access additional required resources.
67            // Otherwise, GC will collect/dispose XpsDocument and DocumentViewer will not work.
68            XpsDocument xpsDocument = this.presentation.ConvertToXpsDocument(SaveOptions.Xps);
69            this.DocViewer.Tag = xpsDocument;
70
71            this.DocViewer.Document = xpsDocument.GetFixedDocumentSequence();
72        }
73
74        private void EnableControls()
75        {
76            bool isEnabled = this.presentation != null;
77
78            this.DocViewer.IsEnabled = isEnabled;
79            this.SimplePrintFileBtn.IsEnabled = isEnabled;
80            this.AdvancedPrintFileBtn.IsEnabled = isEnabled;
81        }
82    }
83}
1' This is very simple project so we won't use MVVM pattern, ICommand, etc. (but we can)
2Imports GemBox.Presentation
3Imports Microsoft.Win32
4
5Class MainWindow
6
7    Dim presentation As PresentationDocument
8
9    Public Sub New()
10        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
11
12        InitializeComponent()
13
14        Me.EnableControls()
15    End Sub
16
17    Private Sub LoadFileBtn_Click(sender As Object, e As RoutedEventArgs)
18
19        Dim fileDialog = New OpenFileDialog()
20        fileDialog.Filter = "PPTX files (*.pptx, *.pptm, *.potx, *.potm)|*.pptx;*.pptm;*.potx;*.potm"
21
22        If (fileDialog.ShowDialog() = True) Then
23            Me.presentation = PresentationDocument.Load(fileDialog.FileName)
24
25            Me.ShowPrintPreview()
26            Me.EnableControls()
27        End If
28
29    End Sub
30
31    Private Sub SimplePrint_Click(sender As Object, e As RoutedEventArgs)
32
33        ' Print to default printer using default options
34        Me.presentation.Print()
35
36    End Sub
37
38    Private Sub AdvancedPrint_Click(sender As Object, e As RoutedEventArgs)
39
40        ' We can use PrintDialog for defining print options
41        Dim printDialog = New PrintDialog()
42        printDialog.UserPageRangeEnabled = True
43
44        If (printDialog.ShowDialog() = True) Then
45
46            Dim printOptions = New PrintOptions(printDialog.PrintTicket.GetXmlStream())
47
48            printOptions.FromSlide = printDialog.PageRange.PageFrom - 1
49            If (printDialog.PageRange.PageTo = 0) Then
50                printOptions.ToSlide = Int32.MaxValue
51            Else
52                printOptions.ToSlide = printDialog.PageRange.PageTo - 1
53            End If
54
55            Me.presentation.Print(printDialog.PrintQueue.FullName, printOptions)
56        End If
57
58    End Sub
59
60    ' We can use DocumentViewer for print preview (but we don't need).
61    Private Sub ShowPrintPreview()
62
63        ' XpsDocument needs to stay referenced so that DocumentViewer can access additional required resources.
64        ' Otherwise, GC will collect/dispose XpsDocument and DocumentViewer will not work.
65        Dim xpsDocument = presentation.ConvertToXpsDocument(SaveOptions.Xps)
66        Me.DocViewer.Tag = xpsDocument
67
68        Me.DocViewer.Document = xpsDocument.GetFixedDocumentSequence()
69
70    End Sub
71
72    Private Sub EnableControls()
73
74        Dim isEnabled As Boolean = Me.presentation IsNot Nothing
75
76        Me.DocViewer.IsEnabled = isEnabled
77        Me.SimplePrintFileBtn.IsEnabled = isEnabled
78        Me.AdvancedPrintFileBtn.IsEnabled = isEnabled
79
80    End Sub
81
82End Class

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