C# Print PDF documents from a WPF application

C# Print PDF documents from a WPF application

C# Print PDF from WPF application?

With PDFRasterizer.NET 3.0 you can transform WPF to PDF and Print PDF File using C# in a fly.

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:

Print PDF from WPF application - Wpf Window

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:

Print PDF from WPF application - Wpf Print File

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:

Print PDF from WPF application - Wpf Print Dialog

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");

Full C# Print PDF from a WPF application 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");
        }
    }
}

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.

More C# WPF code samples?

Check out other WPF code samples: