C# Print PDF documents from a WPF application

This code sample shows how to print PDF documents from a WPF application using C#.

First create a window with a print button in XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" 
        Height="156" 
        Width="250">
    <Grid>
        <Button Name="buttonPrint" Content="Print a PDF" 
                Height="26"  Width="100" Margin="50,50,0,0"
                HorizontalAlignment="Left" VerticalAlignment="Top" 
                Click="ButtonPrintPressed" />
    </Grid>
</Window>

The windows looks like this:

WpfWindow

Add the following handler for the button pressed event:

private void ButtonPrintPressed(object sender, RoutedEventArgs e)
{
  var openFileDialog = new Microsoft.Win32.OpenFileDialog
                     {
                       DefaultExt = ".pdf", 
                       Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*"
                     };
  var fileOpenResult = openFileDialog.ShowDialog();
  if (fileOpenResult != true)
  {
    return;
  }
  ...

Pressing the “print” button now shows the familiar file open dialog:

WpfPrintFile

After the user selects the PDF file, we open a print dialog:

...
PrintDialog printDialog = new PrintDialog();
printDialog.PageRangeSelection = PageRangeSelection.AllPages;
printDialog.UserPageRangeEnabled = true;
bool? doPrint = printDialog.ShowDialog();
if (doPrint != true)
{
  return;
}
...

Note the ‘PageRangeSelection = true’ as it is disabled by default. After selecting a PDF, this print dialog appears:

WpfPrintDialog

After the “print” button is pressed it is time to render the PDF document to WPF (using PDFRasterizer.NET) and to actually print it:

//open the pdf file
FixedDocument fixedDocument;
using (FileStream pdfFile = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read))
{
    Document document = new Document(pdfFile);
    RenderSettings renderSettings = new RenderSettings();
    ConvertToWpfOptions renderOptions = new ConvertToWpfOptions { ConvertToImages = false };
    renderSettings.RenderPurpose = RenderPurpose.Print;
    renderSettings.ColorSettings.TransformationMode = ColorTransformationMode.HighQuality;
    //convert the pdf with the rendersettings and options to a fixed-size document which can be printed
    fixedDocument = document.ConvertToWpf(renderSettings, renderOptions);
}
printDialog.PrintDocument(fixedDocument.DocumentPaginator, "Print");

The full code sample

Here are the above snippets stitched together:

using Microsoft.Win32;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using TallComponents.PDF.Rasterizer;
using TallComponents.PDF.Rasterizer.Configuration;

namespace PrintPDFSimple
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonPrintPressed(object sender, RoutedEventArgs e)
        {
            //get the pdf file the user selected
            OpenFileDialog openFileDialog = new OpenFileDialog
                               {
                                   DefaultExt = ".pdf",
                                   Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*"
                               };
            bool? fileOpenResult = openFileDialog.ShowDialog();
            if (fileOpenResult != true)
            {
                return;
            }

            //show a printdialog to user where attributes can be changed
            PrintDialog printDialog = new PrintDialog();
            printDialog.PageRangeSelection = PageRangeSelection.AllPages;
            printDialog.UserPageRangeEnabled = true;
            bool? doPrint = printDialog.ShowDialog();
            if (doPrint != true)
            {
                return;
            }

            //open the pdf file
            FixedDocument fixedDocument;
            using (FileStream pdfFile = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read))
            {
                Document document = new Document(pdfFile);
                RenderSettings renderSettings = new RenderSettings();
                ConvertToWpfOptions renderOptions = new ConvertToWpfOptions { ConvertToImages = false };
                renderSettings.RenderPurpose = RenderPurpose.Print;
                renderSettings.ColorSettings.TransformationMode = ColorTransformationMode.HighQuality;
                //convert the pdf with the rendersettings and options to a fixed-size document which can be printed
                fixedDocument = document.ConvertToWpf(renderSettings, renderOptions);
            }
            printDialog.PrintDocument(fixedDocument.DocumentPaginator, "Print");
        }
    }
}

Print dialog limitations

The print dialog has the following limitiations:

  • No preview
  • No scale to fit the printable area.
  • No progress indicator
  • No 2-up printing

It appears that it is designed for selecting a printer and using its capabilities, but not so much for the document which is being printed.

Download PDFRasterizer.NET 3.0
We will send you a download link
Why do we ask your email address?
We send tips that speed up your evaluation
We let you know about bug fixes
You can always unsubscribe with one click
We never share your address with a 3rd party
Thank you for your download

We have sent an email with a download link. Alternatively, you may want to use the NuGet package manager to install our library.

Nuget ID

Use the NugetID and start right away, or download the package and install it handmatically