Print PDF (WPF)

Printing
7/3/2014

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

1 public partial class MainWindow : Window 2 { 3 public MainWindow() 4 { 5 InitializeComponent(); 6 } 7 8 private void ButtonPrintPressed(object sender, RoutedEventArgs e) 9 { 10 //create and show a printer dialog 11 var printDialog = new PrintDialog(); 12 printDialog.PageRangeSelection = PageRangeSelection.AllPages; 13 printDialog.UserPageRangeEnabled = true; 14 var doPrint = printDialog.ShowDialog(); 15 if (doPrint != true) 16 { 17 return; 18 } 19 20 FixedDocument fixedDocument; 21 //open the input pdf 22 using (var pdfFile = new FileStream("input.pdf", FileMode.Open, FileAccess.Read)) 23 { 24 var document = new Document(pdfFile); 25 //set the redering options and setting 26 var renderSettings = new RenderSettings(); 27 var renderOptions = new ConvertToWpfOptions { ConvertToImages = false }; 28 renderSettings.RenderPurpose = RenderPurpose.Print; 29 renderSettings.ColorSettings.TransformationMode = ColorTransformationMode.HighQuality; 30 //using the settings create the printable document 31 fixedDocument = document.ConvertToWpf(renderSettings, renderOptions); 32 } 33 //using the selected printer, print the PDF 34 printDialog.PrintDocument(fixedDocument.DocumentPaginator, "Print"); 35 } 36 }