Print document

Printing
8/24/2015

In this sample you can get an impression of how to print a PDF document using C#. We won't be using a graphical user interface as the purpose of this sample is to show the mechanic of printing PDFs. This document will open the specified PDF and will print each page on the standard printer and paper specified by the users operating system. If you want to see how you can use this in a GUI, please refer to the code sample in which you obtain when downloading PDF Rasterizer. The main point of printing using PDF Rasterizer is the drawing of each page. During printing, an event will be thrown for each page. Each page is then drawn onto the graphical surface provided by the PrintPageEventHandler function.

1 static int currentPage = 0; 2 static Document d; 3 static PrintDocument printDocument; 4 5 static void Main(string[] args) 6 { 7 using (FileStream fs = new FileStream(@"..\..\input.pdf", FileMode.Open, FileAccess.Read)) 8 { 9 d = new Document(fs); 10 printDocument = new PrintDocument(); 11 //printDocument.PrinterSettings = this.printerSettings; 12 printDocument.DocumentName = d.DocumentInfo.Title; 13 printDocument.DefaultPageSettings.Margins.Left = 0; 14 printDocument.DefaultPageSettings.Margins.Right = 0; 15 printDocument.DefaultPageSettings.Margins.Bottom = 0; 16 printDocument.DefaultPageSettings.Margins.Top = 0; 17 printDocument.OriginAtMargins = true; 18 19 // uncomment to avoid status dialog 20 printDocument.PrintController = new StandardPrintController(); 21 22 printDocument.PrintPage += new PrintPageEventHandler(printPage); 23 printDocument.Print(); 24 } 25 } 26 27 private static void printPage(object sender, PrintPageEventArgs e) 28 { 29 e.HasMorePages = false; 30 if (currentPage <= d.Pages.Count) 31 { 32 e.Graphics.PageUnit = GraphicsUnit.Point; 33 Page page = d.Pages[currentPage]; 34 35 e.Graphics.MultiplyTransform(GetPrintTransformation(page, e.Graphics.VisibleClipBounds)); 36 page.Draw(e.Graphics); 37 if (currentPage < d.Pages.Count - 1) 38 { 39 currentPage++; 40 e.HasMorePages = true; 41 } 42 } 43 } 44 45 private static Matrix GetPrintTransformation(Page page, RectangleF printableArea) 46 { 47 Matrix transformation = new Matrix(); 48 49 float pdfWidth = (float)page.Width; 50 float pdfHeight = (float)page.Height; 51 float pdfRatio = pdfWidth / pdfHeight; 52 53 PaperSize paperSize = printDocument.PrinterSettings.PaperSizes[0]; 54 float paperWidth = ((float)paperSize.Width * 72f) / 100f; 55 float paperHeight = ((float)paperSize.Height * 72f) / 100f; 56 57 // It is possible that a driver returns bogus data for the printable area. If that is 58 // obviously the case, we are going to ignore the usePrintableArea settings (otherwise 59 // we get invalid transforms). 60 paperWidth = printableArea.Width; 61 paperHeight = printableArea.Height; 62 transformation.Translate(printableArea.Left, printableArea.Top); 63 64 float paperRatio = paperWidth / paperHeight; 65 66 float scaledPdfWidth = pdfWidth; 67 float scaledPdfHeight = pdfHeight; 68 69 bool rotate = false; 70 if ((pdfRatio > 1 && paperRatio < 1) || (pdfRatio < 1 && paperRatio > 1)) 71 { 72 // need to rotate 73 rotate = true; 74 float temp = scaledPdfWidth; 75 scaledPdfWidth = scaledPdfHeight; 76 scaledPdfHeight = temp; 77 pdfRatio = 1 / pdfRatio; 78 } 79 80 float scale = 1; 81 if (pdfRatio > paperRatio) 82 scale = paperWidth / scaledPdfWidth; 83 else 84 scale = paperHeight / scaledPdfHeight; 85 86 scaledPdfWidth *= scale; 87 scaledPdfHeight *= scale; 88 89 // auto-rotate 90 if (rotate) 91 { 92 transformation.Rotate(-90); 93 transformation.Translate(-scaledPdfHeight, 0); 94 } 95 96 // scale 97 transformation.Scale(scale, scale); 98 99 return transformation; 100 }