Adding Image in Existing PDF File using Java - GeeksforGeeks

08 Dec, 2020
 These days, PDF is frequently used for report generation like to generate In order to make the java program interact with files be it of any type word, excel openCV Apache framework comes into play. External Files Required for Build this Code. The first requirement is to import the following libraries files
Pdfbox-xxx.jarorg.apache.commons.logging-xxx.jar
Pdfbox-xxx.jar
org.apache.commons.logging-xxx.jar
Algorithm:
Linking PDF document and java programCreate PDDocument objectCreate PDPageAdd page to document objectsCreate FileInputStream object for image.Create PDJpeg object by passing PDDocument object and FIleInputStream as its constructorInvoke drawXObject() object and specify coordinates with width and height to draw the image onto PDF file.Close the stream, saving the document object, and close the document.
Linking PDF document and java programCreate PDDocument objectCreate PDPageAdd page to document objectsCreate FileInputStream object for image.
Create PDDocument object
Create PDPage
Add page to document objects
Create FileInputStream object for image.
Create PDJpeg object by passing PDDocument object and FIleInputStream as its constructor
Invoke drawXObject() object and specify coordinates with width and height to draw the image onto PDF file.
Close the stream, saving the document object, and close the document.
Implementation: Considering an input image sample to illustrate the working of a program where in order to illustrate the working PDF document before is as follows:
Processing with the usage of java program to insert text in the above PDF document
Java
// Adding Image in Existing PDF using Java  // Importing openCV librariesimport java.io.File;import org.apache.pdfbox.pdmodel.PDDocument;import org.apache.pdfbox.pdmodel.PDPage;import org.apache.pdfbox.pdmodel.graphics.image PDImageXObject;import org.apache.pdfbox.pdmodel.PDPageContentStream;import java.io.IOException;  class GFG {      // Main driver method    public static void main(String[] args)        throws IOException    {          // Loading an already existing pdf document        File file = new File("D:\\javong\\pdf1.pdf");        PDDocument doc = new PDDocument.load(file);          // Retrieve the page        PDPage page = doc.getPage(0);          // Creating Object of PDImageXObject for selecting        // Image and provide the path of file in argument        PDImageXObject pdfimg            = PDImageXImage.createFromFile(                "D:\\Images\\chloro.jpg", doc);          // Creating the PDPageContentStream Object        // for Inserting Image        PDPageContentStream image            = new PDPageContentStream(doc, page);          // set the Image inside the Page        image.drawImage(pdfImage, 55, 370);        System.out.println("Image Inserted");          // Closing the page of PDF by closing        // PDPageContentStream Object        // && Saving the Document        image.close();        doc.save("D:\\javong\\pdf1.pdf");          // Closing the Document        doc.close();    }}
Output: Text inserted in the same input image.
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?
Iterate through List in Java