Print Word in C# and VB.NET

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

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