//--------------------------------------------------------------------------------------- // Copyright (c) 2001-2008 by PDFTron Systems Inc. All Rights Reserved. // Consult legal.txt regarding legal and license information. //--------------------------------------------------------------------------------------- import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import pdftron.Common.Matrix2D; import pdftron.Common.PDFNetException; import pdftron.PDF.*; import pdftron.SDF.SDFDoc; //----------------------------------------------------------------------------------- // This sample illustrates how to embed various raster image formats // (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document. // // Note: On Windows platform this sample utilizes GDI+ and requires GDIPLUS.DLL to // be present in the system path. //----------------------------------------------------------------------------------- public class AddImageTest { public static void main(String[] args) { PDFNet.initialize(); PDFNet.setResourcesPath("../../../resources"); // Relative path to the folder containing test files. String input_path = "../../TestFiles/"; String output_path = "../../TestFiles/Output/"; try { System.out.println("-------------------------------------------------"); PDFDoc doc=new PDFDoc(); ElementBuilder f=new ElementBuilder(); // Used to build new Element objects ElementWriter writer=new ElementWriter(); // Used to write Elements to the page Page page = doc.pageCreate(); // Start a new page writer.begin(page); // Begin writing to this page // ---------------------------------------------------------- // Add JPEG image to the output file Image img = Image.create(doc.getSDFDoc(), (input_path + "peppers.jpg")); Element element = f.createImage(img, new Matrix2D(200, 0, 0, 250, 50, 500)); writer.writePlacedElement(element); //#ifdef _WIN32 // ---------------------------------------------------------- // Add a PNG image to the output file java.awt.Image image = null; try { // Read from a file File file = new File(input_path + "butterfly.png"); image = ImageIO.read(file); } catch (IOException e) { e.printStackTrace(); } img = Image.create(doc.getSDFDoc(), image); element = f.createImage(img, new Matrix2D(img.getImageWidth(), 0, 0, img.getImageHeight(), 300, 500)); writer.writePlacedElement(element); // ---------------------------------------------------------- // Add a GIF image to the output file image = null; try { // Read from a file File file = new File(input_path + "pdfnet.gif"); image = ImageIO.read(file); } catch (IOException e) { e.printStackTrace(); } img = Image.create(doc.getSDFDoc(), image); element = f.createImage(img, new Matrix2D(img.getImageWidth(), 0, 0, img.getImageHeight(), 50, 350)); writer.writePlacedElement(element); // ---------------------------------------------------------- // Add a TIFF image to the output file // Note: encoder hints can be used to select between different compression methods. // For example to instuct PDFNet to compress the image using Flate/PNG compression, // use can use the following encoder hint: // SDF::Array flate_hint; // flate_hint.PushBack(new Name("Flate")); // img = PDF::Image::Create(*doc, (input_path + "grayscale.tif").c_str(), &flate_hint); /*img = Image.create(doc.getSDFDoc(), (input_path + "grayscale.tif")); element = f.createImage(img, new Matrix2D(img.getImageWidth(), 0, 0, img.getImageHeight(), 10, 50)); writer.writePlacedElement(element); writer.end(); // Save the page doc.pagePushBack(page); // Add the page to the document page sequence // ---------------------------------------------------------- // Embed a monochrome TIFF. Compress the image using lossy JBIG2 filter. page = doc.pageCreate(new Rect(0, 0, 612, 794)); writer.begin(page); // begin writing to this page //ObjSet hint_set=new ObjSet(); //Obj enc=hint_set.createArray(); // Initilaize encoder 'hint' parameter //enc.PushBackName("JBIG2"); //enc.PushBackName("Lossy"); img = Image.create(doc.getSDFDoc(), (input_path + "multipage.tif")); element = f.createImage(img, new Matrix2D(612, 0, 0, 794, 0, 0)); writer.writePlacedElement(element);*/ //#endif // _WIN32 writer.end(); // Save the page doc.pagePushBack(page); // Add the page to the document page sequence // ---------------------------------------------------------- // Add a JPEG2000 (JP2) image to the output file // Create a new page page = doc.pageCreate(); writer.begin(page); // Begin writing to the page // Embed the image. img = Image.create(doc.getSDFDoc(), (input_path + "palm.jp2")); // Position the image on the page. element = f.createImage(img, new Matrix2D(img.getImageWidth(), 0, 0, img.getImageHeight(), 96, 80)); writer.writePlacedElement(element); // Write 'JPEG2000 Sample' text string under the image. writer.writeElement(f.createTextBegin(Font.create(doc.getSDFDoc(), Font.e_times_roman), 32)); element = f.createTextRun("JPEG2000 Sample"); element.setTextMatrix(1, 0, 0, 1, 190, 30); writer.writeElement(element); writer.writeElement(f.createTextEnd()); writer.end(); // Finish writing to the page doc.pagePushBack(page); // ---------------------------------------------------------- // doc.Save((output_path + "addimage.pdf").c_str(), Doc.e_remove_unused, 0); doc.save((output_path + "addimage.pdf"), SDFDoc.e_linearized, null); System.out.println("Done. Result saved in addimage.pdf..."); } catch(PDFNetException e) { e.printStackTrace(); System.out.println(e); } catch(InterruptedException e) { e.printStackTrace(); } PDFNet.terminate(); } }