iText Adding Image to a PDF - iText

What is iText Adding Image to a PDF?

We will discuss how a PDF doc is created and an image is added to it using iText library.

Adding Image to a Pdf

By instantiating the Document class an empty PDF Document can be created. a PdfDocument object should be passed as a parameter while instantiating this class to its constructor. Thereafter, an object of the image is created and add this object to document using the add() method of the Document class.

Below are the steps to follow for inserting an image into pdf.

Step 1: Creating a PdfWriter object

The package which this class belongs to is com.itextpdf.kernel.pdf. It accepts a string, that takes the path where the PDF should be created.

As shown below, lets start with Instantiating the PdfWriter class by passing a string value to its constructor.

After creating this object and passing to a PdfDocument (class) will add every element added to this document to the file specified in the path.

Step 2: Creating a PdfDocument object

The PdfDocument class is the class that represents the PDF Document in iText. The package com.itextpdf.kernel.pdf is where this class belongs to. We have to pass an object of class PdfWriter to its constructor to instantiate this class in writing mode.

Below is what a PdfDocument class Instantiation done by passing the above created PdfWriter object to its constructor.

We have methods for each element like page, file attachment, font, event handler which are provided by its class once a PdfDocument object is created.

Step 3: Creating the Document object

For creating a self-sufficient PDF the Document class of the package com.itextpdf.layout is the root element. Object of the class PdfDocument is one of the constructors of this class.

Follow the step below to instantiate the Document class by passing the object of the class PdfDocument created in earlier steps.

Step 4: Creating an Image object

In order to create an image object, firstly create an ImageData object. Use create() method of the ImageDataFactory class for this. As a parameter of this method, pass a string parameter representing the path of the image, as shown below.

We should instantiate the Image class of the com.itextpdf.layout.element package by passing the ImageData object created above, as a parameter to its constructor.

Step 5: Adding image to the document

Using the add() method of the Document class, the image object created in the previous step is added, as shown below.

Step 6: Closing the Document

Use close() method of the Document class to close the document below.


Below Java program demonstrates the PDF Document creation with a image using the iText library. A PDF doc with name addingImage.pdf is created, adds a image to it, and saves it in the path C:/itextExamples/.

Paste the below code in file and give its name as

Using Command prompt, Compile and execute the saved Java file using the following commands –

We will get the below message once above code is executed.

The path which we gave while creating a PDF will have the PDF created as below.


All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

iText Topics