Setting the Position of the Image in PDF Document using Java
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:
Below is the implementation of the above approach:
Image position set successfully in pdf