PDFBox Example | Create PDF File With Images in java

by Azeem Tariq

Posted 1 year ago

Last Updated 8 months ago




Hi , this article we will see how to add images into pdf file using PDFBox lib , so far from our previous tutorials we learned creating pdf file , adding text into pdf file and do some formatting on text in pdf file but we don't know how to add images , lets see show to do this in this article

Java Code to Create PDF file with Images

import java.io.FileInputStream;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;

 * @author azeem

public class Main {

    public static void main(String[] args) {

        String fileName = "pdfWithImage.pdf";
        String imageName = "Logo.jpg";

        try {

            PDDocument doc = new PDDocument();
            PDPage page = new PDPage();


            PDXObjectImage image = new PDJpeg(doc, new FileInputStream(imageName));

            PDPageContentStream content = new PDPageContentStream(doc, page);

            content.drawImage(image, 180, 700);




        } catch (Exception e) {


pdf file with image output

    if you have any problem then comment below i will try to help you :)