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.
Create a new Java Maven project and add the following dependencies in the pom.xml file. This step adds all the required jar files into the Maven dependencies.
Setting the background to a PDF Table
Step 1: Create an empty PDF file.
- Assign the path of the empty PDF to a String variable.
- Import PdfWriter from the package com.itextpdf.kernel.pdf. (PdfWriter allows us to write content on our PDF file)
- PdfWriter accepts a String variable as it’s a parameter which represents the destination of the PDF.
- Initialize the PdfWriter object by passing the path of the PDF file.
The following code creates an empty PDF.
String path = "F:/Table.pdf"; PdfWriter pdfWriter = new PdfWriter(path);
Step 2: Create a PDF document which represents the empty PDF file.
- Import PdfDocument from the package com.itextpdf.kernel.pdf. (PdfDocument is used represent the pdf in code. This later can be used to add or modify various features such as font, images, table, etcetera)
- PdfDocument accepts a PdfWriter or PdfReader object as it’s parameter.
- Initialize the PdfDocument by passing the PdfWriter object.
The following code creates a PdfDocument which represents the empty PDF file.
PdfDocument pdfDoc = new PdfDocument(pdfWriter);
Step 3: Create a Document.
- Import Document from the package com.itextpdf.layout.
- A Document object is created to make a readable version of the PDF.
- One of the constructors of the Document class accepts PdfDocument object as it’s parameters.
- Initialize the document by passing the PdfDocument object.
The following code creates a document.
Document doc = new Document(pdfDoc);
Step 4: Create a Table.
- Import Table from the package com.itextpdf.layout.element.
- A table is created by cells, and each cell occupies a unit row and column.
- The table constructor accepts the number of columns as it’s parameters.
- Initialize the table object by passing the number of columns as it’s a parameter.
The following code creates a Table.
Table table = new Table(3);
Step 5: Add cells to the table with a favoured background using setBackgroundColor(Color.color, float opacity).
- Import Cell from the package com.itextpdf.layout.element.
- Import Color from the package com.itextpdf.kernel.color.
- Initialize the number of cells you want to add to the table.
- Add text to the cells using add(String text).
- Set the background color of the cells using setBackgroundColor(Color.color, float opacity).
- Add the cells to the table.
The following code creates and adds cells to the Table.
Cell cell = new Cell(); cell.add("Geeks for Geeks"); cell.setBackgroundColor(Color.GREEN, 2); table.add(cell);
Step 6: Add the table to document.
- Add the table created to the document using document.add(Table table).
- Close the document using document.close().
The following code adds the table to the PDF Document.
The following code snippet illustrates how tables with a varied background in a technical scenario.
Output: A Table of the following format is created
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.