Insert Image to PDF Document in Java using Apache PDFBox
Tags: Java Apache PDFBox PDF
Introduction
In this tutorial we will learn how to create a PDF document using Apache PDFBox libary in Java and then insert an image to a PDF document page.
Apache PDFBox Overview
The Apache PDFBox is an open source library for working with PDF documents in Java. You can get more information about the project at pdfbox.apache.org
Adding Apache PDFBox Dependencies
Adding below dependencies to build.gradle file if you are using Gradle build tool.
compile group: 'org.apache.pdfbox', name: 'pdfbox', version: '2.0.18'
Adding below XML to pom.xml file if you are using Maven build tool.
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.18</version>
</dependency>
Or you can download the pdfbox-2.0.18.jar file from pdfbox.apache.org/download.cgi
Step 1 - Creating empty PDF document
try(PDDocument document = new PDDocument()) {
} catch (IOException e) {
e.printStackTrace();
}
Step 2 - Adding new empty page to PDF document
PDPage page = new PDPage();
document.addPage(page);
Step 3 - Insert Image to page
For example we have an image file located at D:\SimpleSolution\simple_solution.png
PDPageContentStream pageContentStream = new PDPageContentStream(document, page);
PDImageXObject imageXObject = PDImageXObject.createFromFile("D:\\SimpleSolution\\simple_solution.png", document);
pageContentStream.drawImage(imageXObject, 100, 400);
pageContentStream.close();
Step 4 - Save PDF document
document.save("D:\\SimpleSolution\\ImageDocument.pdf");
Complete Application Source Code
package dev.simplesolution;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
public class InsertImagePdfDocument {
public static void main(String... args) {
try(PDDocument document = new PDDocument()) {
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream pageContentStream = new PDPageContentStream(document, page);
PDImageXObject imageXObject = PDImageXObject.createFromFile("D:\\SimpleSolution\\simple_solution.png", document);
pageContentStream.drawImage(imageXObject, 100, 400);
pageContentStream.close();
document.save("D:\\SimpleSolution\\ImageDocument.pdf");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Execute the application above you will get the ImageDocument.pdf file as below.
Make the image in the center of PDF page
To insert the image at the center of PDF page we need to calculate x-coordinate and y-coordinate when draw the image based on image size. For example the Java code below to insert the image to center of PDF page.
package dev.simplesolution;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
public class InsertCenterImagePdfDocument {
public static void main(String... args) {
try(PDDocument document = new PDDocument()) {
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream pageContentStream = new PDPageContentStream(document, page);
PDRectangle mediaBox = page.getMediaBox();
PDImageXObject imageXObject = PDImageXObject.createFromFile("D:\\SimpleSolution\\simple_solution.png", document);
float startX = (mediaBox.getWidth() - imageXObject.getWidth()) / 2;
float startY = (mediaBox.getHeight() - imageXObject.getHeight()) / 2;
pageContentStream.drawImage(imageXObject, startX, startY);
pageContentStream.close();
document.save("D:\\SimpleSolution\\CenterImageDocument.pdf");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Execute the application above you will get the CenterImageDocument.pdf file as below.
Download Source Code
The source code in this article can be found at: github.com/simplesolutiondev/ApachePDFBoxInsertImagePdfDocument
or download at:
Happy Coding 😊
Related Articles
Creating PDF Document File in Java using Apache PDFBox
Apache PDFBox Adding Metadata to PDF Document in Java
Creating Bookmarks for PDF Document in Java with Apache PDFBox
Creating PDF Document Page Labels in Java with Apache PDFBox