Skip to content
Related Articles

Related Articles

Formatting the Content of a Cell in a Table of PDF using Java
  • Difficulty Level : Medium
  • Last Updated : 22 Dec, 2020
GeeksforGeeks - Summer Carnival Banner

iText is a Java library developed, to access and manipulate PDF files, that is to extract and modify the PDF content. iText is a Java library originally created by Bruno Lowagie which allows creating, reading, and manipulating PDF files. Java allows incorporating various fully developed packages and modules in order to work with PDF files.iText library enables the creation of a PDF document in the local directory and adding contents to the PDF file created using the java program. Here, formatting the contents of a table cell in a PDF using java can be achieved below mentioned in the article.

Libraries required:  

  • iText
  • slf4j (Logging Library)

 Procedure:

  1. Download the iText jar files.
  2. Download the slf4j jar file.
  3. To use the libraries add the above-downloaded jar files to the classpath of the system.
  4. Get the current working directory of the running java program to create the PDF file in the same location.
  5. Create a PdfWriter object (from itextpdf library) which writes the PDF file to the given path.
  6. Create an empty PDF Document object.
  7. Create a Table using a Table object and set Table Dimensions.
  8. Create Cells and add attributes to cell-like Text Alignment, Font Color, Background Color & Cell Content.
  9. Add the Cells to the Table.

Sample Input Image: This PDF file input image is used the below program to format the content of the cell.



Implementation: First cells are created in the above input PDF file and then the content of the cell is formatted

Example

Java




// Java Progra to format the content
// of a cell in a table of PDF
  
// Importing generic java libraries
import java.io.*;
  
// Importing itext library packages
import com.itextpdf.kernel.colors.*;
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 FormatCellPdf {
  
    // Main driver method
    public static void main(String args[]) throws Exception
    {
        // Creating a PDF file
        // Getting path of current working directory
        // in the same directory of the running java program
        String path = System.getProperty("user.dir");
        path += "/cellPDF.pdf";
  
        // Creating a PdfWriter object using the path
        PdfWriter writer = new PdfWriter(path);
  
        // Creating a PdfDocument object
        PdfDocument pdfDoc = new PdfDocument(writer);
  
        // Creating a Document object
        Document doc = new Document(pdfDoc);
  
        // Creating a table
        float[] columnWidhts = { 300F, 300F };
        Table table = new Table(columnWidhts);
  
        // Filling Row1 and adding to table
  
        // For every cell perform 4 steps below:
        // Step 1: Creating a cell
        // Step 2: Adding name to a cell
        // Step 3: Setting background color
        // Step 4: Set text alignment
  
        // For Cell 1
  
        // Step 1: Creating cell 1
        Cell cell1 = new Cell();
  
        // Step 2: Adding name to cell 1
        cell1.add(new Paragraph("Color").setFontColor(
            ColorConstants.WHITE));
  
        // Step 3: Setting background color
        cell1.setBackgroundColor(ColorConstants.BLACK);
  
        // Step 4: Setting text alignment
        cell1.setTextAlignment(TextAlignment.CENTER);
  
        // For Cell 2
        Cell cell2 = new Cell();
        cell2.add(new Paragraph("Black").setFontColor(
            ColorConstants.WHITE));
        cell2.setBackgroundColor(ColorConstants.BLUE);
        cell2.setTextAlignment(TextAlignment.CENTER);
  
        // For Cell 3
        Cell cell3 = new Cell();
        cell3.add(new Paragraph("Color").setFontColor(
            ColorConstants.WHITE));
        cell3.setBackgroundColor(ColorConstants.BLUE);
        cell3.setTextAlignment(TextAlignment.CENTER);
  
        // For Cell 4
        Cell cell4 = new Cell();
        cell4.add(new Paragraph("Red").setFontColor(
            ColorConstants.WHITE));
        cell4.setBackgroundColor(ColorConstants.BLACK);
        cell4.setTextAlignment(TextAlignment.CENTER);
  
        // Filling Row 3 and adding to table
        // For Cell 5
        Cell cell5 = new Cell();
        cell5.add(new Paragraph("Color").setFontColor(
            ColorConstants.WHITE));
        cell5.setBackgroundColor(ColorConstants.BLACK);
        cell5.setTextAlignment(TextAlignment.CENTER);
  
        // For Cell 6
        Cell cell6 = new Cell();
        cell6.add(new Paragraph("Yellow").setFontColor(
            ColorConstants.WHITE));
        cell6.setBackgroundColor(ColorConstants.BLUE);
        cell6.setTextAlignment(TextAlignment.CENTER);
  
        // Now, adding cells to table
        table.addCell(cell1);
        table.addCell(cell2);
        table.addCell(cell3);
        table.addCell(cell4);
        table.addCell(cell5);
        table.addCell(cell6);
  
        // Now, adding Table to document
        doc.add(table);
  
        // Closing the document
        doc.close();
  
        // Display message for successful compilation and
        // execution of program
        System.out.println("Table created successfully!");
    }
}

Output: In console window message is printed “Table successfully created” to show proper execution of program and changes made during execution in input file during execution is as shown.

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. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :