GemBox.Document is a .NET component that enables you to read, write, edit, convert, and print document files from your .NET applications using one simple API.

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

The following example 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>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Xps.Packaging;
using GemBox.Document;
using Microsoft.Win32;

namespace PrintCs
{
    public partial class MainWindow : Window
    {
        private DocumentModel document;

        public MainWindow()
        {
            ComponentInfo.SetLicense("FREE-LIMITED-KEY");

            InitializeComponent();

            this.EnableControls();
        }

        private void LoadFileBtn_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            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";

            if (fileDialog.ShowDialog() == true)
            {
                this.document = DocumentModel.Load(fileDialog.FileName);

                this.ShowPrintPreview();
                this.EnableControls();
            }
        }

        private void SimplePrint_Click(object sender, RoutedEventArgs e)
        {
            // Print to default printer using default options
            this.document.Print();
        }

        private void AdvancedPrint_Click(object sender, RoutedEventArgs e)
        {
            // We can use PrintDialog for defining print options
            PrintDialog printDialog = new PrintDialog();
            printDialog.UserPageRangeEnabled = true;

            if (printDialog.ShowDialog() == true)
            {
                PrintOptions printOptions = new PrintOptions(printDialog.PrintTicket.GetXmlStream());

                printOptions.FromPage = printDialog.PageRange.PageFrom - 1;
                printOptions.ToPage = printDialog.PageRange.PageTo == 0 ? int.MaxValue : printDialog.PageRange.PageTo - 1;

                this.document.Print(printDialog.PrintQueue.FullName, printOptions);
            }
        }

        // We can use DocumentViewer for print preview (but we don't need).
        private void ShowPrintPreview()
        {
            // XpsDocument needs to stay referenced so that DocumentViewer can access additional required resources.
            // Otherwise, GC will collect/dispose XpsDocument and DocumentViewer will not work.
            XpsDocument xpsDocument = document.ConvertToXpsDocument(SaveOptions.XpsDefault);
            this.DocViewer.Tag = xpsDocument;

            this.DocViewer.Document = xpsDocument.GetFixedDocumentSequence();
        }  

        private void EnableControls()
        {
            bool isEnabled = this.document != null;

            this.DocViewer.IsEnabled = isEnabled;
            this.SimplePrintFileBtn.IsEnabled = isEnabled;
            this.AdvancedPrintFileBtn.IsEnabled = isEnabled;
        }
    }
}
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Xps.Packaging
Imports Microsoft.Win32
Imports GemBox.Document

Class MainWindow

    Dim document As DocumentModel

    Public Sub New()
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        InitializeComponent()

        Me.EnableControls()
    End Sub

    Private Sub LoadFileBtn_Click(sender As Object, e As RoutedEventArgs)

        Dim fileDialog = New OpenFileDialog()
        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"

        If (fileDialog.ShowDialog() = True) Then
            Me.document = DocumentModel.Load(fileDialog.FileName)

            Me.ShowPrintPreview()
            Me.EnableControls()
        End If

    End Sub

    Private Sub SimplePrint_Click(sender As Object, e As RoutedEventArgs)

        ' Print to default printer using default options
        Me.document.Print()

    End Sub

    Private Sub AdvancedPrint_Click(sender As Object, e As RoutedEventArgs)

        ' We can use PrintDialog for defining print options
        Dim printDialog = New PrintDialog()
        printDialog.UserPageRangeEnabled = True

        If (printDialog.ShowDialog() = True) Then

            Dim printOptions = New PrintOptions(printDialog.PrintTicket.GetXmlStream())

            printOptions.FromPage = printDialog.PageRange.PageFrom - 1
            If (printDialog.PageRange.PageTo = 0) Then
                printOptions.ToPage = Int32.MaxValue
            Else
                printOptions.ToPage = printDialog.PageRange.PageTo - 1
            End If

            Me.document.Print(printDialog.PrintQueue.FullName, printOptions)
        End If

    End Sub

    ' We can use DocumentViewer for print preview (but we don't need).
    Private Sub ShowPrintPreview()

        ' XpsDocument needs to stay referenced so that DocumentViewer can access additional required resources.
        ' Otherwise, GC will collect/dispose XpsDocument and DocumentViewer will not work.
        Dim xpsDocument = document.ConvertToXpsDocument(SaveOptions.XpsDefault)
        Me.DocViewer.Tag = xpsDocument

        Me.DocViewer.Document = xpsDocument.GetFixedDocumentSequence()

    End Sub

    Private Sub EnableControls()

        Dim isEnabled As Boolean = Me.document IsNot Nothing

        Me.DocViewer.IsEnabled = isEnabled
        Me.SimplePrintFileBtn.IsEnabled = isEnabled
        Me.AdvancedPrintFileBtn.IsEnabled = isEnabled

    End Sub

End Class

Check next sample.