// // PDFNet Copyright (c) 2001-2008 by PDFTron Systems Inc. All Rights Reserved. // using System; using System.Drawing; using System.Drawing.Printing; using pdftron; using pdftron.PDF; using pdftron.Common; using pdftron.Filters; namespace PDFPrintTestCS { /// /// The following sample illustrates how to print PDF document using currently selected /// default printer. PDFDraw is used to send pre-rasterized data. /// /// If you would like to rasterize page at high resolutions (e.g. more than 600 DPI), you /// should use PDFRasterizer or PDFNet vector output instead of PDFDraw. /// class PDFPrint { static PDFDoc pdfdoc = null; static PDFDraw pdfdraw = null; static PageIterator pageitr = null; static void Main(string[] args) { PDFNet.Initialize(); // Search for PDFNet resource file (i.e. 'pdfnet.res'). string resource_path = "resources"; bool found = false; for (int i=0; !found && i<7; ++i) { found = PDFNet.SetResourcesPath(resource_path); if (!found) resource_path = "../" + resource_path; } if (!found) { Console.WriteLine("PDFNet resource file 'pdfnet.res' was not found.\nSome files may not render properly."); } // Optional: Set ICC color profiles to fine tune color conversion // for PDF 'device' color spaces. You can use your own ICC profiles. // Standard Adobe color profiles can be download from Adobes site: // http://www.adobe.com/support/downloads/iccprofiles/iccprofiles_win.html // // Simply drop all *.icc files in PDFNet resource folder or you specify // the full pathname. try { // PDFNet.SetColorManagement(true); // PDFNet.SetDefaultDeviceCMYKProfile("USWebCoatedSWOP.icc"); // will search in PDFNet resource folder. // PDFNet.SetDefaultDeviceRGBProfile("AdobeRGB1998.icc"); } catch (Exception) { Console.WriteLine("The specified color profile was not found."); } // Optional: Set predefined font mappings to override default font // substitution for documents with missing fonts. For example: //--- // PDFNet.AddFontSubst("StoneSans-Semibold", "C:/WINDOWS/Fonts/comic.ttf"); // PDFNet.AddFontSubst("StoneSans", "comic.ttf"); // search for 'comic.ttf' in PDFNet resource folder. // PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_Identity, "C:/WINDOWS/Fonts/arialuni.ttf"); // PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_Japan1, "C:/Program Files/Adobe/Acrobat 7.0/Resource/CIDFont/KozMinProVI-Regular.otf"); // PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_Japan2, "c:/myfonts/KozMinProVI-Regular.otf"); // // If fonts are in PDFNet resource folder, it is not necessary to specify // the full path name. For example, //--- // PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_Korea1, "AdobeMyungjoStd-Medium.otf"); // PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_CNS1, "AdobeSongStd-Light.otf"); // PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_GB1, "AdobeMingStd-Light.otf"); string input_path = "../../../../TestFiles/"; // Relative path to the folder containing test files. try { // Open the PDF document. Console.WriteLine("Opening the input file..."); pdfdoc = new PDFDoc(input_path + "tiger.pdf"); pdfdoc.InitSecurityHandler(); // Start printing from the first page pageitr = pdfdoc.GetPageIterator(); // PDFNet includes two different rasterizer implementations. // // The two implementations offer a trade-off between print // speed and accuracy/quality, as well as a trade-off between // vector and raster output. // // e_GDIPlus rasterizer can be used to render the page // using Windows GDI+, whereas e_BuiltIn rasterizer can // be used to render bitmaps using platform-independent // graphics engine (in this case images are always converted // to bitmap prior to printing). pdfdraw = new PDFDraw(); pdfdraw.SetRasterizerType(PDFRasterizer.Type.e_GDIPlus); // pdfdraw.SetRasterizerType(PDFRasterizer.Type.e_BuiltIn); // Create a printer PrintDocument printer = new PrintDocument(); // Set the PrintPage delegate which will be invoked to print each page printer.PrintPage += new PrintPageEventHandler(PrintPage); printer.Print(); // Start printing pdfdraw.Dispose(); // Free allocated resources (generally a good idea when printing many documents). pdfdoc.Close(); // Close the file } catch (PDFNetException e) { Console.WriteLine(e.Message); } PDFNet.Terminate(); } #if NET_1_1 // In case you need to account for 'hard margin' printer property. // .NET Framework 2.x or above offers direct access 'hard margin' property. [System.Runtime.InteropServices.DllImport("gdi32.dll")] private static extern int GetDeviceCaps(IntPtr hdc, int nIndex); private const int PHYSICALOFFSETX = 112; private const int PHYSICALOFFSETY = 113; #endif // Print event hander static void PrintPage(object sender, PrintPageEventArgs ev) { Graphics gr = ev.Graphics; gr.PageUnit = GraphicsUnit.Inch; Rectangle rectPage = ev.PageBounds; //print without margins //Rectangle rectPage = ev.MarginBounds; //print using margins float dpi = gr.DpiX; if (dpi>300) dpi = 300; int example = 2; bool use_hard_margins = false; // Example 1) Print the Bitmap. if (example == 1) { pdfdraw.SetDPI(dpi); System.Drawing.Bitmap bmp = pdfdraw.GetBitmap(pageitr.Current()); gr.DrawImage(bmp, rectPage, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel); } // Example 2) Print via PDFDraw class. if (example == 2) { double left, right, top, bottom; if (use_hard_margins) // You could adjust the rectangle to account for hard and soft margins, etc. { #if NET_1_1 // This code is used to obtain printer hard margins when running on .NET 1.1x or below. IntPtr hdc = new IntPtr(); hdc = ev.Graphics.GetHdc(); // Get handle to device context. double hardMarginX = GetDeviceCaps(hdc, PHYSICALOFFSETX); double hardMarginY = GetDeviceCaps(hdc, PHYSICALOFFSETY); ev.Graphics.ReleaseHdc(hdc); // Release handle to device context. #else // If you are running on .NET Framework 2.x or above, you can directly access 'hard margin' property. double hardMarginX = ev.PageSettings.HardMarginX; double hardMarginY = ev.PageSettings.HardMarginY; #endif left = (rectPage.Left - hardMarginX) / 100.0; right = (rectPage.Right - hardMarginX) / 100.0; top = (rectPage.Top - hardMarginY) / 100.0; bottom = (rectPage.Bottom - hardMarginY) / 100.0; } else { left= rectPage.Left / 100.0; right = rectPage.Right / 100.0; top = rectPage.Top / 100.0; bottom= rectPage.Bottom / 100.0; } // The above page dimensions are in inches. We need to convert // the page dimensions to PDF units (or points). One point is // 1/72 of an inch. pdftron.PDF.Rect rect = new Rect(left*72, bottom*72, right*72, top*72); try { pdfdraw.SetDPI(dpi); pdfdraw.DrawInRect(pageitr.Current(), gr, rect); } catch (Exception ex) { Console.WriteLine("Printing Error: " + ex.ToString()); } } pageitr.Next(); // Move to the next page, if any ev.HasMorePages = pageitr.HasNext(); } } }