How do I add an image into PDF document in iText?

The following example demonstrate how to add an image into a PDF document using the iText library. Image is created using the com.itextpdf.text.Image class. To create an instance of image we can use the Image.getInstance() method. Below we create an image from an image file name an a URL that point to an image resource.

package org.kodejava.example.itextpdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;


public class ImageDemo {
    public static void main(String[] args) {
        Document doc = new Document();
        try {
            PdfWriter.getInstance(doc, new FileOutputStream("ImageDemo.pdf"));

            // Creating image by file name
            String filename = "other-sample/src/main/resources/java.gif";
            Image image = Image.getInstance(filename);

            // The following line to prevent the "Server returned 
            // HTTP response code: 403" error.
            System.setProperty("http.agent", "Chrome");

            // Creating image from a URL
            String url = "";
            image = Image.getInstance(url);
        } catch (DocumentException | IOException e) {
        } finally {

Maven Dependencies

<!-- -->

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.