iText - Setting Position of the Image



In this chapter, we will see how to set the position of an image in a PDF document using the iText library.

Setting the Position of the Image

You can create an empty PDF Document by instantiating the Document class. While instantiating this class, you need to pass a PdfDocument object as a parameter to its constructor.

To add an image to the pdf, create and an object of the image that is required to be added and add it using the add() method of the Document class. You can insert the image in a desired position on the document using the method setFixedPosition() of the Image class.

Following are the steps to set the position of an image in the PDF document.

Step 1: Creating a PdfWriter object

The PdfWriter class represents the DocWriter for a PDF. This class belongs to the package com.itextpdf.kernel.pdf. The constructor of this class accepts a string, representing the path of the file where the PDF is to be created.

Instantiate the PdfWriter class by passing a string value (representing the path where you need to create a PDF) to its constructor, as shown below.

// Creating a PdfWriter 
String dest = "C:/itextExamples/positionOfImage.pdf"; 
PdfWriter writer = new PdfWriter(dest); 

When an object of this type is passed to a PdfDocument (class), every element added to this document will be written to the file specified.

Step 2: Creating a PdfDocument object

The PdfDocument class is the class that represents the PDF Document in iText. This class belongs to the package com.itextpdf.kernel.pdf. To instantiate this class (in writing mode), you need to pass an object of the class PdfWriter to its constructor.

Instantiate the PdfDocument class by passing the above created PdfWriter object to its constructor, as shown below.

// Creating a PdfDocument  
PdfDocument pdfDoc = new PdfDocument(writer); 

Once a PdfDocument object is created, you can add various elements like page, font, file attachment, and event handler using the respective methods provided by its class.

Step 3: Creating the Document object

The Document class of the package com.itextpdf.layout is the root element while creating a self-sufficient PDF. One of the constructors of this class accepts an object of the class PdfDocument.

Instantiate the Document class by passing the object of the class PdfDocument created in the previous steps, as shown below.

// Creating a Document  
Document document = new Document(pdfDoc); 

Step 4: Creating an Image object

To create the image object, first of all, create an ImageData object using the create() method of the ImageDataFactory class. As a parameter of this method, pass a string parameter representing the path of the image, as shown below.

// Creating an ImageData object 
String imageFile = "C:/itextExamples/javafxLogo.jpg"; 
ImageData data = ImageDataFactory.create(imageFile); 

Now, instantiate the Image class of the com.itextpdf.layout.element package. While instantiating, pass the ImageData object as a parameter to its constructor, as shown below.

// Creating an Image object 
Image img = new Image(data);

Step 5: Setting the position of the image

You can set the position of the image in a PDF document using the setFixedPosition() method of the Image. Set the position of the image to the coordinates (100, 250) on the document using this method, as shown below.

// Setting the position of the image to the center of the page 
image.setFixedPosition(100, 250);

Step 6: Adding image to the document

Now, add the image object, created in the previous step, using the add() method of the Document class, as shown below.

// Adding image to the document 
document.add(img); 

Step 7: Closing the Document

Close the document using the close() method of the Document class, as shown below.

// Closing the document 
document.close(); 

Example

The following Java program demonstrates how to set an image at a desired position on a PDF document using the iText library. It creates a PDF document with the name positionOfImage.pdf, adds an image to it, sets it nearer to the center of the page, and saves it in the path C:/itextExamples/

Save this code in a file with the name SettingPosition.java.

import com.itextpdf.io.image.ImageData; 
import com.itextpdf.io.image.ImageDataFactory; 

import com.itextpdf.kernel.pdf.PdfDocument; 
import com.itextpdf.kernel.pdf.PdfWriter; 

import com.itextpdf.layout.Document; 
import com.itextpdf.layout.element.Image;  

public class SettingPosition {      
   public static void main(String args[]) throws Exception {              
      // Creating a PdfWriter       
      String dest = "C:/EXAMPLES/itextExamples/3images/positionOfImage.pdf";       
      PdfWriter writer = new PdfWriter(dest);               
      
      // Creating a PdfDocument       
      PdfDocument pdfDoc = new PdfDocument(writer);              
      
      // Creating a Document        
      Document document = new Document(pdfDoc);              
      
      // Creating an ImageData object       
      String imFile = "C:/EXAMPLES/itextExamples/3images/logo.jpg";       
      ImageData data = ImageDataFactory.create(imFile);             

      // Creating an Image object        
      Image image = new Image(data);                
      
      // Setting the position of the image to the center of the page       
      image.setFixedPosition(100, 250);                  
      
      // Adding image to the document       
      document.add(image);              
      
      // Closing the document       
      document.close();
      
      System.out.println("Image added");    
   } 
}

Compile and execute the saved Java file from the command prompt using the following commands.

javac SettingPosition.java 
java SettingPosition 

Upon execution, the above program creates a PDF document, displaying the following message.

Image added

If you verify the specified path, you can find the created PDF document as shown below.

Position Of Image
Advertisements