Open In App

Setting the Position of the Image in PDF Document using Java

Last Updated : 17 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

To set the Position of the Image in a PDF document using Java multiple external dependencies need to download first. Setting the Position of the Image in a PDF, use the iText library. These are the steps that should be followed to Set the Position of the Image in a PDF using java.

1. Creating a PdfWriter object: The PdfWriter class represents the DocWriter for a PDF. The constructor of this class accepts a string, i.e. the path of the file where the PDF is to be created.
 

2. Creating a PdfDocument object: The PdfDocument class is the class that represents the PDF Document in iText, to instantiate this class in write mode, you need to pass an object of the class PdfWriter (i.e. pdfwriter from above code) to its constructor.
 

3. Creating the Document object: The Document class is the root element when creating a self-sufficient PDF. One of the constructors of this class accepts an object of the class PdfDocument (i.e. pdfdocument).
 

4. Create an Image object: We need the image object to manage the images. In order to create an image object, we need to create an ImageData object. We can create it bypassing the string parameter that represents the path of the image to create() method of the ImageDataFactory class. Now we can create an image object by passing the ImageData object, as a parameter to the constructor of the Image class.
5. Setting the position of the image: We will use setFixedPosition() method of the Image to set the position of the image in a PDF document. We pass the desired coordinates of position to setFixedPosition() method.
 

6. Add image to the Pdf Document: Add the image object using the add() method of the Document class, and close the document using the close() method of the Document class.
The following are dependencies required for executing the program:

io-7.1.13.jar
kernel-7.1.13.jar
layout-7.1.13.jar

Below is the implementation of the above approach:

Java




// Setting the Position of the Image
// in PDF Document using 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 SetImagePosition {
    public static void main(String args[]) throws Exception
    {
        try {
            // path where the pdf is to be created.
            String path = "F:/JavaPdf/setImagePosition.pdf";
            PdfWriter pdfwriter = new PdfWriter(path);
 
            // Creating a PdfDocument object.
            // passing PdfWriter object constructor
            PdfDocument pdfdocument
                = new PdfDocument(pdfwriter);
 
            // Creating a Document and
            // passing pdfDocument object
            Document document = new Document(pdfdocument);
 
            // Create an ImageData object
            String imageFile = "F:/JavaPdf/image.png";
            ImageData data
                = ImageDataFactory.create(imageFile);
            // Creating an Image object
            Image image = new Image(data);
 
            // Set the position of the image.
            image.setFixedPosition(200, 300);
 
            // Adding image to the document
            document.add(image);
            // Closing the document
            document.close();
 
            System.out.println(
                "Image  position set successfully in pdf");
        }
        catch (Exception e) {
            System.out.println(
                "unable to set image position due to " + e);
        }
    }
}


 
 

Output:

 

Image  position set successfully in pdf

PDF:

 

 



Similar Reads

Rotating an Image in a PDF Document Using Java
In this article, we will learn how to Rotating an Image in a PDF document using Java. For Rotating an Image in a PDF, we will use the iText library. These are the steps that should be followed to Rotating an Image in a PDF using java. 1. Creating a PdfWriter object The PdfWriter class represents the DocWriter for a PDF. The constructor of this clas
3 min read
Creating an Empty PDF Document using Java
For creating a PDF document using Java, we need to know the packages/libraries which are necessary for writing the code. So, for creating a PDF doc, we will be using the iText 7 library. To know how to install this library in your workspace, you can follow this link. Creating An Empty PDF Doc To create an empty PDF Document, we first need to instan
3 min read
Removing Pages from a PDF Document using Java
Program to remove pages from an existing document a PDF document. The external jar file is required to import in the program. Below is the implementation for the same. Remove a page from an existing PDF document using the removePage() method of the PDDocument class. Approach: Load an existing PDF document using the static method load() of the PDDoc
2 min read
Drawing a Line in a PDF Document using Java
In this article, we will learn how to Draw a line in a PDF document using Java. For drawing a line in a PDF, we will use the iText library. These are the steps that should be followed to Draw a line in a PDF using java. 1. Creating a PdfWriter object The PdfWriter class represents the DocWriter for a PDF. The constructor of this class accepts a str
3 min read
Adding Pages to a PDF Document using Java
PDDocument class of 'org.apache.pdfbox.pdmodel' package which extends 'java.lang.Object'. is used. Declaration: public class PDDocument extends Object implements Pageable, Closeable Pre-requisite: Constructors PDDocument(): This constructor used to construct a new PDF document with zero pages.PDDocument(COSDocument doc): This constructor uses alrea
4 min read
Setting Background to a Table in a PDF using Java
Have you ever wondered how different cells in a PDF table are set to various colors? Well, in this article we'll be seeing how we can set the background of various cells in a PDF table using the iText library in your favorite language Java. Downloading iText Create a new Java Maven project and add the following dependencies in the pom.xml file. Thi
6 min read
How to Convert a Document to PDF in Java?
In software projects, there is quite often a requirement for conversion of a given file (HTML/TXT/etc.,) to a PDF file and similarly, any PDF file needs to get converted to HTML/TXT/etc., files. Even PDFs need to be stored as images of type PNG or GIF etc., Via a sample maven project, let us see the same. As it is the maven project, necessary depen
7 min read
Adding Image in Existing PDF File using Java
These days, PDF is frequently used for report generation like to generate In order to make the java program interact with files be it of any type word, excel openCV Apache framework comes into play. External Files Required for Build this Code. The first requirement is to import the following libraries files Pdfbox-xxx.jarorg.apache.commons.logging-
2 min read
Image Processing in Java - Colored Image to Grayscale Image Conversion
Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and set PixelsIn this article, we will be converting a colored image to a grayscale image. RGB Color Model - The RGB color model is an additive mixing model in which red, green, and blue light are added together in various ways to reproduce a broad array of color
3 min read
Image Processing in Java - Colored image to Negative Image Conversion
Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored image to Grayscale Image Conversion In this set, we will be converting a colored image to a negative image. Colored Image (RGB Color Model) - The RGB color model is an additive mixing model in which red, green, and
3 min read
Article Tags :
Practice Tags :