//--------------------------------------------------------------------------------------- // Copyright (c) 2001-2008 by PDFTron Systems Inc. All Rights Reserved. // Consult legal.txt regarding legal and license information. //--------------------------------------------------------------------------------------- import java.awt.image.PixelGrabber; //import pdftron.Filters.FilterWriter; //import pdftron.Filters.StdFile; import pdftron.PDF.*; import pdftron.SDF.Obj; import pdftron.SDF.ObjSet; //--------------------------------------------------------------------------------------- // The following sample illustrates how to convert PDF documents to various raster image // formats (such as PNG, JPEG, BMP, TIFF, etc), as well as how to convert a PDF page to // GDI+ Bitmap for further manipulation and/or display in WinForms applications. //--------------------------------------------------------------------------------------- public class PDFDrawTest { public static void main(String[] args) { try { // The first step in every application using PDFNet is to initialize the // library and set the path to common PDF resources. The library is usually // initialized only once, but calling Initialize() multiple times is also fine. PDFNet.initialize(); PDFNet.setResourcesPath("../../../resources"); // Optional: Set ICC color profiles to fine tune color conversion // for PDF 'device' color spaces... //PDFNet::SetColorManagement(true); //PDFNet::SetDefaultDeviceCMYKProfile("D:/Misc/ICC/USWebCoatedSWOP.icc"); //PDFNet::SetDefaultDeviceRGBProfile("AdobeRGB1998.icc"); // will search in PDFNet resource folder. // ---------------------------------------------------- // Optional: Set predefined font mappings to override default font // substitution for documents with missing fonts... // 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::e_Identity, "C:/WINDOWS/Fonts/arialuni.ttf"); // PDFNet::AddFontSubst(PDFNet::e_Japan1, "C:/Program Files/Adobe/Acrobat 7.0/Resource/CIDFont/KozMinProVI-Regular.otf"); // PDFNet::AddFontSubst(PDFNet::e_Japan2, "c:/myfonts/KozMinProVI-Regular.otf"); // PDFNet::AddFontSubst(PDFNet::e_Korea1, "AdobeMyungjoStd-Medium.otf"); // PDFNet::AddFontSubst(PDFNet::e_CNS1, "AdobeSongStd-Light.otf"); // PDFNet::AddFontSubst(PDFNet::e_GB1, "AdobeMingStd-Light.otf"); } catch (Exception e) { e.printStackTrace(); } // Relative path to the folder containing test files. String input_path = "../../TestFiles/"; String output_path = "../../TestFiles/Output/"; PDFDraw draw=new PDFDraw(); // PDFDraw class is used to rasterize PDF pages. ObjSet hint_set=new ObjSet(); //-------------------------------------------------------------------------------- // Example 1) Convert the first page to PNG and TIFF at 92 DPI. // A three step tutorial to convert PDF page to an image. try { // A) Open the PDF document. PDFDoc doc=new PDFDoc((input_path + "tiger.pdf")); // Initialize the security handler, in case the PDF is encrypted. doc.initSecurityHandler(); // B) The output resolution is set to 92 DPI. draw.setDPI(92); // C) Rasterize the first page in the document and save the result as PNG. Page pg = doc.getPage(1); draw.export(pg, (output_path + "tiger_92dpi.png")); System.out.println("Example 1: " + output_path + "tiger_92dpi.png" + ". Done."); // Export the same page as TIFF draw.export(pg, (output_path + "tiger_92dpi.tif"), "TIFF"); } catch(Exception e) { e.printStackTrace(); } //-------------------------------------------------------------------------------- // Example 2) Convert the all pages in a given document to JPEG at 72 DPI. System.out.println("Example 2:"); try { PDFDoc doc=new PDFDoc((input_path + "newsletter.pdf")); // Initialize the security handler, in case the PDF is encrypted. doc.initSecurityHandler(); draw.setDPI(72); // Set the output resolution is to 72 DPI. // Use optional encoder parameter to specify JPEG quality. Obj encoder_param=hint_set.createDict(); encoder_param.putNumber("Quality", 80); // Traverse all pages in the document. for (PageIterator itr=doc.getPageIterator(); itr.hasNext();) { Page current=(Page)(itr.next()); String filename=output_path+"newsletter"+current.getIndex() + ".jpg"; System.out.println(filename); draw.export(current, filename, "JPEG", encoder_param); } System.out.println("Done."); } catch(Exception e) { e.printStackTrace(); } // Examples 3-5 try { // Common code for remaining samples. PDFDoc tiger_doc=new PDFDoc((input_path + "tiger.pdf")); // Initialize the security handler, in case the PDF is encrypted. tiger_doc.initSecurityHandler(); Page page = (Page)(tiger_doc.getPageIterator().next()); //-------------------------------------------------------------------------------- // Example 3) Convert the first page to raw bitmap. Also, rotate the // page 90 degrees and save the result as RAW. draw.setDPI(100); // Set the output resolution is to 100 DPI. draw.setRotate(Page.e_90); // Rotate all pages 90 degrees clockwise. java.awt.Image image = draw.getBitmap(page); int width=image.getWidth(null), height=image.getHeight(null); int[] arr = new int[width*height]; PixelGrabber pg=new PixelGrabber(image, 0, 0, width, height, arr, 0, width); pg.grabPixels(); // Save the raw RGB data to disk. /*StdFile file=new StdFile((output_path + "tiger_100dpi_rot90.raw"), StdFile.e_write_mode); FilterWriter writer=new FilterWriter(file); writer.writeBuffer(arr); writer.flush();*/ //System.out.println("Example 3: " + output_path + "tiger_100dpi_rot90.raw. Done."); draw.setRotate(Page.e_0); // Disable image rotation for remaining samples. //-------------------------------------------------------------------------------- // Example 4) Convert PDF page to a fixed image size. Also illustrates some // other features in PDFDraw class such as rotation, image stretching, exporting // to grayscale, or monochrome. // Initialize render 'gray_hint' parameter, that is used to control the // rendering process. In this case we tell the rasterizer to export the image as // 1 Bit Per Component (BPC) image. Obj mono_hint=hint_set.createDict(); mono_hint.putNumber("BPC", 1); // SetImageSize can be used instead of SetDPI() to adjust page scaling // dynamically so that given image fits into a buffer of given dimensions. draw.setImageSize(1000, 1000); // Set the output image to be 1000 wide and 1000 pixels tall draw.export(page, (output_path + "tiger_1000x1000.png"), "PNG", mono_hint); System.out.println("Example 4: " + output_path + "tiger_1000x1000.png. Done."); draw.setImageSize(200, 400); // Set the output image to be 200 wide and 300 pixels tall draw.setRotate(Page.e_180); // Rotate all pages 90 degrees clockwise. // 'gray_hint' tells the rasterizer to export the image as grayscale. Obj gray_hint=hint_set.createDict(); gray_hint.putName("ColorSpace", "Gray"); draw.export(page, (output_path + "tiger_200x400_rot180.png"), "PNG", gray_hint); System.out.println("Example 4: " + output_path + "tiger_200x400_rot180.png. Done."); draw.setImageSize(400, 200, false); // The third parameter sets 'preserve-aspect-ratio' to false. draw.setRotate(Page.e_0); // Disable image rotation. draw.export(page, (output_path + "tiger_400x200_stretch.jpg"), "JPEG"); System.out.println("Example 4: " + output_path + "tiger_400x200_stretch.jpg. Done."); //-------------------------------------------------------------------------------- // Example 5) Zoom into a specific region of the page and rasterize the // area at 200 DPI and as a thumbnail (i.e. a 50x50 pixel image). Rect zoom_rect=new Rect(216, 522, 330, 600); page.setCropBox(zoom_rect); // Set the page crop box. // Select the crop region to be used for drawing. draw.setPageBox(Page.e_crop); draw.setDPI(900); // Set the output image resolution to 900 DPI. draw.export(page, (output_path + "tiger_zoom_900dpi.png"), "PNG"); System.out.println("Example 5: " + output_path + "tiger_zoom_900dpi.png. Done."); draw.setImageSize(50, 50); // Set the thumbnail to be 50x50 pixel image. draw.export(page, (output_path + "tiger_zoom_50x50.png"), "PNG"); System.out.println("Example 5: " + output_path + "tiger_zoom_50x50.png. Done."); } catch(Exception e) { e.printStackTrace(); } // Calling Terminate when PDFNet is no longer in use is a good practice, but // is not required. PDFNet.terminate(); } }