Site iconJava PDF Blog

How to insert an image into a PDF

Recently, we released JPedal 2023.07 which contains the ability to insert images into PDF files.

All you need is a copy of JPedal, a BufferedImage, and a few lines of Java.

final WritableAnnotation[] annotations = new WritableAnnotation[1];

annotations[0] = new ImageAnnotation(page, x0, y0, x1, y1, bufferedImage);

AnnotationWriter.writeAnnotations(new File("input.pdf"), new File("output.pdf"), annotations);

The bottom left corner of the image will be at (x0, y0) and the top right corner will be at (x1, y1). The image is scaled to fit within these bounds.