javaitextpdf-generationitext7cellspacing

How to give separations between the cells of a table in Itext7?


Problem Statement:-

I am using Itext7 in JAVA to create a PDF having a table. I need to give the separations between the cells of the table.

Red and blue arrows in the image are the pin points from where I want to separate them.

Any help regarding the issue is highly appreciated!!

Code:-

    package com.example.pdfcreator;
    
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    import com.itextpdf.*;

    @SpringBootApplication
    public class PdfcreatorApplication {
    public static final String DEST = "D:\\generate_pdf\\hello.pdf";
     public static void main(String args[]) throws IOException, java.io.IOException {
       PdfDocument pdf = new PdfDocument(new PdfWriter(DEST));
       Document document = new Document(pdf);
     var table = new Table(new float[] { 3,3,3,3,3,3,3}).setWidth(UnitValue.createPercentValue(100)).setFixedLayout().setFontSize(8).setMarginTop(4);   
    Cell cell11 = new Cell(1, 2).setBorder(Border.NO_BORDER).add(new Paragraph("label1 :"));
    Cell cell12 = new Cell(1, 5).add(new Paragraph(""));
    Cell cell21 = new Cell(1, 2).setBorder(Border.NO_BORDER).add(new Paragraph("label2 :"));
    Cell cell22 = new Cell(1, 5).add(new Paragraph(""));
    Cell cell31 = new Cell(1, 2).setBorder(Border.NO_BORDER).add(new Paragraph("label3 :"));
    Cell cell32 = new Cell(1, 5).add(new Paragraph(""));
    Cell cell41 = new Cell(1, 2).setBorder(Border.NO_BORDER).add(new Paragraph("label4 :"));
    Cell cell42 = new Cell(1, 5).add(new Paragraph(""));
    table.addCell(cell11);  
    table.addCell(cell12);  
    table.addCell(cell21);  
    table.addCell(cell22);  
    table.addCell(cell31);  
    table.addCell(cell32);  
    table.addCell(cell41);  
    table.addCell(cell42);  
    document.add(table);
    var table99 = new Table(new float[] { 3,3,3,3,3,3,3}).setWidth(UnitValue.createPercentValue(100)).setFixedLayout().setFontSize(8);
    Cell cell = new Cell(1,2).setBorder(Border.NO_BORDER).add(new Paragraph("label9 : "));
    table99.addCell(cell);
    cell = new Cell(1,4).add(new Paragraph("  "));
    table99.addCell(cell);
    Cell cell23 = new Cell(5, 1).add(new Paragraph("Photo").setMarginLeft(23).setMarginTop(28));
    table99.addCell(cell23);
    cell = new Cell(1,2).setBorder(Border.NO_BORDER).add(new Paragraph(" label10: "));
    table99.addCell(cell);
    cell = new Cell(1,4).add(new Paragraph("  "));
    table99.addCell(cell);
    cell = new Cell(1,2).setBorder(Border.NO_BORDER).add(new Paragraph(" label11: "));
    table99.addCell(cell);
    cell = new Cell(1,4).add(new Paragraph("  "));
    table99.addCell(cell);
    cell = new Cell(1,2).setBorder(Border.NO_BORDER).add(new Paragraph(" label12: "));
    table99.addCell(cell);
    cell = new Cell(1,4).add(new Paragraph("  "));
    table99.addCell(cell);
    cell = new Cell(1,2).setBorder(Border.NO_BORDER).add(new Paragraph(" label13: "));
    table99.addCell(cell);
    cell = new Cell(1,4).add(new Paragraph("  "));
    table99.addCell(cell);
    document.add(table99);   }}

Solution

  • I have given the seperations using the empty cells with borders removed and it has worked properly as per my requirement. Demo code line which i have implemented are written as below.

    I have created one style in which i have given the minimum height required for my spacing and removed the borders from the cell.

    Style spacingCellStyle = new Style().setBorder(Border.NO_BORDER).setHeight(1);
    

    Then, i have simply just added the cell with this style wherever in the table, spacing was required.

    Table.addCell(new Cell(1, 6).addStyle(spacingCellStyle));
    

    As per my requirement, this was the only possible way to do so and it executed perfectly.