How to put absolute position of image in PDF document using java iText library

In this example you will learn how to add the image in absolute position in PDF document using iText java library.

Please note we are using the iText version 5 for this example.

You can set the absolute position of an image using the setAbsolutePosition() method. You have set the absolute position of image before adding the image to the PDF document. The method the setAbsolutePosition() takes two parameters X and Y coordinate of the lower left corner of the image.

Below is the code example of absolute positioning of an image in iText.

package com.java.connect.itext;
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
 
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfWriter;
 
public class PDFAbsolutePositionImageExample {
    public static void main(String[] args) throws DocumentException, MalformedURLException, IOException {
        // Create new PDF document
        Document document = new Document();
        // Create an output stream of PDF file.
        OutputStream out = new FileOutputStream("PDF_Absolute_Position_Image_Example.pdf");
        // Get a PdfWriter instance to write in PDF document.
        PdfWriter.getInstance(document, out);
        // Open the PDF document
        document.open();
        // Create an image of lord ganesh.
        Image image = Image.getInstance("lord_ganesh.jpg");
        // Set the position of image
        image.setAbsolutePosition(300f, 600f);
        // Add paragraph to PDF document.
        document.add(image);
        // Close the PDf document after use.
        document.close();
    }
 
}

The generated PDF document with absolute position of image looks like below screen.

iText-absolute-position-of-image-in-PDF-document-using-java-iText-library_files