We will discuss how a PDF doc is created and how to set the image position in it using iText library.
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.
First of all create an image object and using add() method of the Document class, add the image to the pdf. Using method setFixedPosition() of the Image class, you can insert an image in desired position.
Below are the steps to follow for setting the position of an image in the PDF document.
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: Setting the position of the image
Using the setFixedPosition() method of the Image, the image position can be set in a PDF document. As shown below, set the image position to the coordinates (100, 250) on the document.
Step 6: 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 7: 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 and setting its position using the iText library. A PDF doc with name positionOfImage.pdf is created, adds a image to it and sets it to the page center, and saves it in the path C:/itextExamples/.
Paste the below code in file and give its name as SettingPosition.java.
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.
iText Related Interview Questions
|C++ Interview Questions||Adv Java Interview Questions|
|Core Java Interview Questions||Java Interview Questions|
|Advanced C++ Interview Questions||C and C++ Interview Questions|
|Ericsson JAVA Interview Questions||Executors Java Interview Questions|
|Java 9 Interview Questions|
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.