Formatting the Content of a Cell in a Table of PDF using Java
PDF files are important in document management and manipulation because of their universality and reliability. When it comes to Java-based solutions for accessing and editing PDF content, iText stands out as a strong and versatile library. Bruno Lowagie created iText, which offers a comprehensive set of tools for creating, reading, and manipulating PDF files. One of iText's most notable features is the ability to precisely and easily format the contents of table cells within PDF documents. In the following article, we will demonstrate how to use iText in Java to format PDF table cells effectively.
Libraries Required:
Before delving into the implementation details, it is critical to ensure that all necessary libraries are in place. The primary libraries required for the task include:
1. iText :
iText is a Java library that allows you to create, read, and manipulate PDF files within Java applications. iText, created by Bruno Lowagie, offers a comprehensive set of features for working with PDF files, making it a popular choice among developers for document-related tasks.
2. SLF4J (Simple Logging Facade for Java):
SLF4J is a logging facade that provides a simple abstraction layer for various logging frameworks commonly used in Java applications. It was created as an alternative to Java's standard logging API (java.util.logging), with the goal of addressing common logging issues while also providing a unified interface for logging across frameworks.
Algorithm
Let's look at how to format PDF table cells using iText in Java.
Step 1: Download the necessary iText and SLF4J JAR files.
Step 2: Add the JAR files to the Java project's classpath.
Step 3: Get the current working directory of the running Java program.
Step 4: Create a PdfWriter object from the iText library.
Step 5: Create an empty PDF Document object.
Step 6: Use iText's Table class to create a table structure and adjust the table's dimensions to meet the requirements we have.
Step 7: Create individual cells in the table using Cell objects and apply the desired formatting attributes to each cell, including text alignment, font color, background color, and cell content.
Step 8: After creating and formatting the cells, add them to the table.
Step 9: Close the document and save it in the specified location.
To demonstrate the preceding steps, consider a sample Java program that generates a PDF document containing a formatted table with different cell attributes:
FileName: PdfTableFormattingExample.java
import java.io.*; // Importing iText library packages import com.itextpdf.kernel.color.*; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.Cell; import com.itextpdf.layout.element.Paragraph; import com.itextpdf.layout.element.Table; import com.itextpdf.layout.property.TextAlignment; public class PdfTableFormattingExample { public static void main(String args[]) throws Exception { // Setting up file path for the generated PDF String filePath = System.getProperty("user.dir") + "/formattedCellPDF.pdf"; PdfWriter writer = new PdfWriter(filePath); PdfDocument pdfDocument = new PdfDocument(writer); Document document = new Document(pdfDocument); // Defining column widths for the table float[] columnWidths = {300F, 300F}; // Creating a Table object with defined column widths Table table = new Table(columnWidths); // Creating cells with different background colors and text Cell cell1 = new Cell(); cell1.add(new Paragraph("Green").setFontColor(Color.WHITE)); cell1.setBackgroundColor(Color.GREEN); cell1.setTextAlignment(TextAlignment.CENTER); Cell cell2 = new Cell(); cell2.add(new Paragraph("Yellow").setFontColor(Color.WHITE)); cell2.setBackgroundColor(Color.YELLOW); cell2.setTextAlignment(TextAlignment.CENTER); Cell cell3 = new Cell(); cell3.add(new Paragraph("Orange").setFontColor(Color.WHITE)); cell3.setBackgroundColor(Color.ORANGE); cell3.setTextAlignment(TextAlignment.CENTER); Cell cell4 = new Cell(); cell4.add(new Paragraph("Cyan").setFontColor(Color.WHITE)); cell4.setBackgroundColor(Color.CYAN); cell4.setTextAlignment(TextAlignment.CENTER); Cell cell5 = new Cell(); cell5.add(new Paragraph("Magenta").setFontColor(Color.WHITE)); cell5.setBackgroundColor(Color.MAGENTA); cell5.setTextAlignment(TextAlignment.CENTER); Cell cell6 = new Cell(); cell6.add(new Paragraph("Pink").setFontColor(Color.WHITE)); cell6.setBackgroundColor(Color.PINK); cell6.setTextAlignment(TextAlignment.CENTER); // Adding cells to the table table.addCell(cell1); table.addCell(cell2); table.addCell(cell3); table.addCell(cell4); table.addCell(cell5); table.addCell(cell6); // Adding the table to the document document.add(table); document.close(); System.out.println("Table created successfully!"); } }
Output: