Print Word and PDF files in C# and VB.NET

The following sample demonstrates how to print Word and PDF documents 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 Word and PDF 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.Document;
5using Microsoft.Win32;
6
7namespace PrintCs
8{
9    public partial class MainWindow : Window
10    {
11        private DocumentModel document;
12
13        public MainWindow()
14        {
15            ComponentInfo.SetLicense("FREE-LIMITED-KEY");
16
17            InitializeComponent();
18
19            this.EnableControls();
20        }
21
22        private void LoadFileBtn_Click(object sender, RoutedEventArgs e)
23        {
24            OpenFileDialog fileDialog = new OpenFileDialog();
25            fileDialog.Filter = "DOCX files (*.docx, *.docm, *.dotm, *.dotx)|*.docx;*.docm;*.dotm;*.dotx|DOC files (*.doc, *.dot)|*.doc;*.dot|PDF files (*.pdf)|*.pdf|HTML files (*.html, *.htm)|*.html;*.htm|RTF files (*.rtf)|*.rtf|TXT files (*.txt)|*.txt";
26
27            if (fileDialog.ShowDialog() == true)
28            {
29                this.document = DocumentModel.Load(fileDialog.FileName);
30
31                this.ShowPrintPreview();
32                this.EnableControls();
33            }
34        }
35
36        private void SimplePrint_Click(object sender, RoutedEventArgs e)
37        {
38            // Print to default printer using default options
39            this.document.Print();
40        }
41
42        private void AdvancedPrint_Click(object sender, RoutedEventArgs e)
43        {
44            // We can use PrintDialog for defining print options
45            PrintDialog printDialog = new PrintDialog();
46            printDialog.UserPageRangeEnabled = true;
47
48            if (printDialog.ShowDialog() == true)
49            {
50                PrintOptions printOptions = new PrintOptions(printDialog.PrintTicket.GetXmlStream());
51
52                printOptions.FromPage = printDialog.PageRange.PageFrom - 1;
53                printOptions.ToPage = printDialog.PageRange.PageTo == 0 ? int.MaxValue : printDialog.PageRange.PageTo - 1;
54
55                this.document.Print(printDialog.PrintQueue.FullName, printOptions);
56            }
57        }
58
59        // We can use DocumentViewer for print preview (but we don't need).
60        private void ShowPrintPreview()
61        {
62            // XpsDocument needs to stay referenced so that DocumentViewer can access additional required resources.
63            // Otherwise, GC will collect/dispose XpsDocument and DocumentViewer will not work.
64            XpsDocument xpsDocument = document.ConvertToXpsDocument(SaveOptions.XpsDefault);
65            this.DocViewer.Tag = xpsDocument;
66
67            this.DocViewer.Document = xpsDocument.GetFixedDocumentSequence();
68        }  
69
70        private void EnableControls()
71        {
72            bool isEnabled = this.document != null;
73
74            this.DocViewer.IsEnabled = isEnabled;
75            this.SimplePrintFileBtn.IsEnabled = isEnabled;
76            this.AdvancedPrintFileBtn.IsEnabled = isEnabled;
77        }
78    }
79}
1Imports System.Windows
2Imports System.Windows.Controls
3Imports System.Windows.Xps.Packaging
4Imports Microsoft.Win32
5Imports GemBox.Document
6
7Class MainWindow
8
9    Dim document As DocumentModel
10
11    Public Sub New()
12        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
13
14        InitializeComponent()
15
16        Me.EnableControls()
17    End Sub
18
19    Private Sub LoadFileBtn_Click(sender As Object, e As RoutedEventArgs)
20
21        Dim fileDialog = New OpenFileDialog()
22        fileDialog.Filter = "DOCX files (*.docx, *.docm, *.dotm, *.dotx)|*.docx;*.docm;*.dotm;*.dotx|DOC files (*.doc, *.dot)|*.doc;*.dot|PDF files (*.pdf)|*.pdf|HTML files (*.html, *.htm)|*.html;*.htm|RTF files (*.rtf)|*.rtf|TXT files (*.txt)|*.txt"
23
24        If (fileDialog.ShowDialog() = True) Then
25            Me.document = DocumentModel.Load(fileDialog.FileName)
26
27            Me.ShowPrintPreview()
28            Me.EnableControls()
29        End If
30
31    End Sub
32
33    Private Sub SimplePrint_Click(sender As Object, e As RoutedEventArgs)
34
35        ' Print to default printer using default options
36        Me.document.Print()
37
38    End Sub
39
40    Private Sub AdvancedPrint_Click(sender As Object, e As RoutedEventArgs)
41
42        ' We can use PrintDialog for defining print options
43        Dim printDialog = New PrintDialog()
44        printDialog.UserPageRangeEnabled = True
45
46        If (printDialog.ShowDialog() = True) Then
47
48            Dim printOptions = New PrintOptions(printDialog.PrintTicket.GetXmlStream())
49
50            printOptions.FromPage = printDialog.PageRange.PageFrom - 1
51            If (printDialog.PageRange.PageTo = 0) Then
52                printOptions.ToPage = Int32.MaxValue
53            Else
54                printOptions.ToPage = printDialog.PageRange.PageTo - 1
55            End If
56
57            Me.document.Print(printDialog.PrintQueue.FullName, printOptions)
58        End If
59
60    End Sub
61
62    ' We can use DocumentViewer for print preview (but we don't need).
63    Private Sub ShowPrintPreview()
64
65        ' XpsDocument needs to stay referenced so that DocumentViewer can access additional required resources.
66        ' Otherwise, GC will collect/dispose XpsDocument and DocumentViewer will not work.
67        Dim xpsDocument = document.ConvertToXpsDocument(SaveOptions.XpsDefault)
68        Me.DocViewer.Tag = xpsDocument
69
70        Me.DocViewer.Document = xpsDocument.GetFixedDocumentSequence()
71
72    End Sub
73
74    Private Sub EnableControls()
75
76        Dim isEnabled As Boolean = Me.document IsNot Nothing
77
78        Me.DocViewer.IsEnabled = isEnabled
79        Me.SimplePrintFileBtn.IsEnabled = isEnabled
80        Me.AdvancedPrintFileBtn.IsEnabled = isEnabled
81
82    End Sub
83
84End Class

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