We will discuss how a PDF doc is created and a table is added and an image is added to 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. Thereafter, the Table class should be instantiated to add an areabreak to the document and add this object to document using the add() method.
First of all instantiate the Cell class and create an image object and using add() method of the Cell class, add the image to the cell object.
Below are the steps to follow for inserting an image into the cell of a table.
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 a Table object
The Table class belongs to the package com.itextpdf.layout.element. The Table class represents a two-dimensional grid filled with cells ordered in rows and columns.
As shown below, the Table class is instantiated.
Step 5: Creating the cell
By instantiating the Cell class of the package com.itextpdf.layout.element, a cell object is created.
Step 6: Creating an Image
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.
As below, use the add() method of the cell class to add the image object.
Step 7: Adding cell to the table
Call the addCell() method of the Table class to add this cell to the table. Pass the cell object as a parameter to this method.
Step 8: Adding table to the document
Add the table object created in the previous step using the add() method of the Document class, as below.
Step 9: 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 table and adding image to cells using the iText library. A PDF doc with name addingImage.pdf is created, adds a table to it, inserts an image (javafxLogo.jpg) to one of its cells and saves it in the path C:/itextExamples/.
Paste the below code in file and give its name as AddingImageToTable.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 © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.