public static void addImageFromBufferedImage() throws IOException { // instantiate Document instance Document doc = new Document(); // add a page to pages collection of Pdf file Page page = doc.getPages().add(); // create image instance Image image1 = new Image(); // create BufferedImage instance java.awt.image.BufferedImage bufferedImage = ImageIO.read(new File("source.gif")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // write buffered Image to OutputStream instance ImageIO.write(bufferedImage, "gif", baos); baos.flush(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); // add image to paragraphs collection of first page page.getParagraphs().add(image1); // set image stream as OutputStream holding Buffered image image1.setImageStream(bais); // save resultant PDF file doc.save("BufferedImage.pdf"); }
public static void pdfImageApproach() throws IOException { // Instantiate Document Object Document doc = new Document(); // Add a page to pages collection of document Page page = doc.getPages().add(); // Load the source image file to Stream object java.io.FileInputStream fs = new java.io.FileInputStream("source.tif"); // Set margins so image will fit, etc. page.getPageInfo().getMargin().setBottom(0); page.getPageInfo().getMargin().setTop(0); page.getPageInfo().getMargin().setLeft(0); page.getPageInfo().getMargin().setRight(0); page.setCropBox(new Rectangle(0, 0, 400, 400)); // Create an image object Image image1 = new Image(); // Add the image into paragraphs collection of the section page.getParagraphs().add(image1); // Set the image file stream image1.setImageStream(fs); // Save resultant PDF file doc.save("Image2PDF_DOM.pdf"); }
public static void main(String[] args) { String dataDir = "PathToDir"; // Instantiate Document object Document doc = new Document(); // Add a page to PDF document doc.getPages().add(); OutputStream out = new java.io.ByteArrayOutputStream(); // Save document to Stream object doc.save(out); // Create PdfFileSignature instance PdfFileSignature pdfSignSingle = new PdfFileSignature(); // Bind the source PDF by reading contents of Stream pdfSignSingle.bindPdf(new ByteArrayInputStream(((ByteArrayOutputStream) out).toByteArray())); // Sign the PDF file using PKCS1 object pdfSignSingle.sign( 1, true, new java.awt.Rectangle(100, 100, 150, 50), new PKCS1(dataDir + "VirtualCabinetPortal (1).pfx", "password")); // Set image for signature appearance pdfSignSingle.setSignatureAppearance(dataDir + "im.jpg"); // Save final output pdfSignSingle.save(dataDir + "out_PDFNEWJAVA_33311.pdf"); }