What is PDFBox - Inserting Image?
Apache PDFBox add Image to PDF Document. The image is located in the src/main/resources/logo.png location of our project. We can create an image using PDImageXObject.createFromFile(image, doc). Using the PDPageContentStream we can call the drawImage method. We need to calculate the dimensions of the image/position and pass it in.
Pdfbox Insert Image Online
In the previous section, we went through on how to extract text from a PDF document. In this section, we will learn how to insert image to the PDF document.
Inserting Image to a PDF Document
createFromFile() and drawImage() are the two methods of the classes PDImageXObject and PDPageContentStream respectively used to insert an image into a document.
Following are the steps to extract text from an existing PDF document.
Step 1: Loading an Existing PDF Document
To load an existing document, use the load() method of the PDDocument class. Instantiate this class to load the PDF document as depicted below.
Step 2: Retrieving a Page
Select a page in the PDF document and retrieve its page object using the getPage() method as shown below.
To retrieve the page object from a PDF document, use the getPage() method.
Step 3: Creating PDImageXObject object
Insert Image In Mail
PDImageXObject in PDFBox library is a powerful class used to perform operations related to an image. Operations such as inserting, setting height and width of an image etc.
createFromFile() method is used to create an object of this class. This method takes two parameters. One parameter is the location or path of the image which we want to add in the form of a string. Second one is the document to which we need to add the image.
Step 4: Preparing the Content Stream
PDPageContentStream class is used to insert various kinds of data elements. To the constructor of this class, we need to pass the document object and the page object. So instantiate this class by sending or passing these two objects as depicted below-
Step 5: Drawing the Image in the PDF Document
drawImage() method is used to insert an image to the document. This method takes three parameters, first one is the image object created in above step and second and third are width and height of the image as depicted below.
Step 6: Closing the PDPageContentStream
We need to close the PDPageContentStream object using close() method as depicted below.
Step 7: Saving the Document
Pdfbox Insert Image Word
Save the PDF document using save() method after adding the required content. Below is the code for the same.
Step 8: Closing the Document
Lastly, to close the document use close() method of the PDDocument class as depicted below.
Example
Let us consider a PDF document named sample.pdf with empty pages in the path C:/PdfBox_Examples/ as depicted below.
Example below explains on how to add image to a blank page of the mentioned PDF document. We will load a PDF document named sample.pdf, add image to it and save it. Save the above code in a file by name InsertingImage.java.
Once the file is saved, compile and execute it from command prompt using the below commands-
Above program will add image into specified page of the PDF document. Below message is rendered upon execution-
Pdfbox Add Image To Document
Once the program is executed, go to the specified path and you can find the PDF document with an image inserted to it.