简体   繁体   中英

itext 7 table border issue

The current release of itext 7 has a problem rendering borders for cells in a table where some cells span a couple rows.

The correct (by documentation, shown below) way of rendering a border results in a null pointer exception.

.setBorder(Border.NO_BORDER)
.setBorderTop(new SolidBorder(1f))

The intuitive way(shown below) ignores no-border requests and just renders all borders I only ask for the top border

.setBorderBottom(Border.NO_BORDER)
.setBorderLeft(Border.NO_BORDER)
.setBorderRight(Border.NO_BORDER)
.setBorderTop(new SolidBorder(1f))

This only happens for the first row, last to the right cell (labeled "1.4" in my case). Otherwise, I find iText7 to be an amazing product and an example of great software engineering. Thanks!

Below is the complete source code illustrating these 2 cases:

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.border.Border;
import com.itextpdf.layout.border.SolidBorder;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.VerticalAlignment;
import java.io.File;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;


public class TableBorderTest {

public static final String OUTPUT_FOLDER = "./target/test/";

@BeforeClass
public static void beforeClass() {
    new File(OUTPUT_FOLDER).mkdirs();
}

@AfterClass
public static void tearDownClass() throws Exception {
}

/**
 * Test of generate method, of class SummaryResultsVsAll.
 * @throws java.lang.Exception
 */
@Test
public void testGenerate1() throws Exception {
    System.out.println("generate");

    String outPdf = OUTPUT_FOLDER + "TableBorderTest.pdf";
    PdfWriter writer = new PdfWriter(outPdf);
    PdfDocument pdfDocument;
    pdfDocument = new PdfDocument(writer);
    Document document = new Document(pdfDocument);

    Table table = new Table(new float[]{10f, 10f, 20.0F, 70.0F});
    table.setWidthPercent(100)
            .setPadding(0)
            .setFontSize(9);

    Cell aCell = new Cell(7, 1);
    aCell.add("1.1").setBorder(Border.NO_BORDER).setBorderBottom(new SolidBorder(1f)).setBorderTop(new SolidBorder(1f));

    table.addCell(aCell);

    //create row
    Cell bCell = new Cell(3, 1);
    Paragraph paragraph = new Paragraph("1.2");
    paragraph.setBold();
    bCell.add(paragraph);
    bCell.setBorder(Border.NO_BORDER).setBorderTop(new SolidBorder(1f));
    table.addCell(bCell);

    //row
    paragraph = new Paragraph("1.3").setFontSize(7).setPaddingTop(10);
    Cell someCell = createTableCellNoBorder();
    someCell.add(paragraph).setBorderTop(new SolidBorder(1f));
    table.addCell(someCell);

    someCell = createTableCellNoBorder();
    someCell.add("1.4").setBorderTop(new SolidBorder(1f));
    table.addCell(someCell);

    // row        
    paragraph = new Paragraph("2.1").setFontSize(7);
    someCell = createTableCellNoBorder();
    someCell.add(paragraph);
    table.addCell(someCell);

    someCell = createTableCellNoBorder();
    someCell.add("2.2");
    table.addCell(someCell);

    //row
    paragraph = new Paragraph("3.1").setFontSize(7);
    someCell = createTableCellNoBorder();
    someCell.add(paragraph);
    table.addCell(someCell);

    someCell = createTableCellNoBorder();
    someCell.add("3.2");
    table.addCell(someCell);

    //row
    someCell = createDoubleTableCell();
    paragraph = new Paragraph("4.1");
    paragraph.setBold();
    someCell.add(paragraph).setBorderBottom(new SolidBorder(1f)).setBorderTop(new SolidBorder(1f));
    table.addCell(someCell);

    someCell = createTableCellWithTopAndBottomBorders();
    someCell.add("4.2");
    table.addCell(someCell);

    //create row
    someCell = createDoubleTableCell();
    paragraph = new Paragraph("5.1");
    paragraph.setBold();
    someCell.add(paragraph).setBorderBottom(new SolidBorder(1f)).setBorderTop(new SolidBorder(1f));
    table.addCell(someCell);

    someCell = createTableCellWithTopAndBottomBorders();

    someCell.add("5.2");
    table.addCell(someCell);

    //create row
    someCell = createDoubleTableCell();
    paragraph = new Paragraph("6.1");
    paragraph.setBold();
    someCell.add(paragraph).setBorderBottom(new SolidBorder(1f)).setBorderTop(new SolidBorder(1f));
    table.addCell(someCell);

    someCell = createTableCellWithTopAndBottomBorders();

    someCell.add("6.2");
    table.addCell(someCell);

    //create row
    someCell = createDoubleTableCell();
    paragraph = new Paragraph("7.1");
    paragraph.setBold();
    someCell.add(paragraph).setBorderBottom(new SolidBorder(1f)).setBorderTop(new SolidBorder(1f));
    table.addCell(someCell);

    someCell = createTableCellWithTopAndBottomBorders();

    someCell.add("7.2");
    table.addCell(someCell);

    document.add(table);

    pdfDocument.close();
    writer.flush();
    writer.close();

}
    /**
 * Test of generate method, of class SummaryResultsVsAll.
 * @throws java.lang.Exception
 */
@Test
public void testGenerate2() throws Exception {
    System.out.println("generate");

    String outPdf = OUTPUT_FOLDER + "TableBorderTest2.pdf";
    PdfWriter writer = new PdfWriter(outPdf);
    PdfDocument pdfDocument;
    pdfDocument = new PdfDocument(writer);
    Document document = new Document(pdfDocument);

    Table table = new Table(new float[]{10f, 10f, 20.0F, 70.0F});
    table.setWidthPercent(100)
            .setPadding(0)
            .setFontSize(9);

    Cell aCell = new Cell(7, 1);
    aCell.add("1.1").setBorder(Border.NO_BORDER).setBorderBottom(new SolidBorder(1f)).setBorderTop(new SolidBorder(1f));

    table.addCell(aCell);

    //create row
    Cell bCell = new Cell(3, 1);
    Paragraph paragraph = new Paragraph("1.2");
    paragraph.setBold();
    bCell.add(paragraph);
    bCell.setBorder(Border.NO_BORDER).setBorderTop(new SolidBorder(1f));
    table.addCell(bCell);

    //row
    paragraph = new Paragraph("1.3").setFontSize(7).setPaddingTop(10);
    Cell someCell = createTableCellNoBorder();
    someCell.add(paragraph).setBorderTop(new SolidBorder(1f));
    table.addCell(someCell);

    someCell = createTableCellWithTopBorder();
    someCell.add("1.4");
    table.addCell(someCell);

    // row        
    paragraph = new Paragraph("2.1").setFontSize(7);
    someCell = createTableCellNoBorder();
    someCell.add(paragraph);
    table.addCell(someCell);

    someCell = createTableCellNoBorder();
    someCell.add("2.2");
    table.addCell(someCell);

    //row
    paragraph = new Paragraph("3.1").setFontSize(7);
    someCell = createTableCellNoBorder();
    someCell.add(paragraph);
    table.addCell(someCell);

    someCell = createTableCellNoBorder();
    someCell.add("3.2");
    table.addCell(someCell);

    //row
    someCell = createDoubleTableCell();
    paragraph = new Paragraph("4.1");
    paragraph.setBold();
    someCell.add(paragraph).setBorderBottom(new SolidBorder(1f)).setBorderTop(new SolidBorder(1f));
    table.addCell(someCell);

    someCell = createTableCellWithTopAndBottomBorders();
    someCell.add("4.2");
    table.addCell(someCell);

    //create row
    someCell = createDoubleTableCell();
    paragraph = new Paragraph("5.1");
    paragraph.setBold();
    someCell.add(paragraph).setBorderBottom(new SolidBorder(1f)).setBorderTop(new SolidBorder(1f));
    table.addCell(someCell);

    someCell = createTableCellWithTopAndBottomBorders();

    someCell.add("5.2");
    table.addCell(someCell);

    //create row
    someCell = createDoubleTableCell();
    paragraph = new Paragraph("6.1");
    paragraph.setBold();
    someCell.add(paragraph).setBorderBottom(new SolidBorder(1f)).setBorderTop(new SolidBorder(1f));
    table.addCell(someCell);

    someCell = createTableCellWithTopAndBottomBorders();

    someCell.add("6.2");
    table.addCell(someCell);

    //create row
    someCell = createDoubleTableCell();
    paragraph = new Paragraph("7.1");
    paragraph.setBold();
    someCell.add(paragraph).setBorderBottom(new SolidBorder(1f)).setBorderTop(new SolidBorder(1f));
    table.addCell(someCell);

    someCell = createTableCellWithTopAndBottomBorders();

    someCell.add("7.2");
    table.addCell(someCell);

    document.add(table);

    pdfDocument.close();
    writer.flush();
    writer.close();

}

    private Cell createTableCellNoBorder() {
    Cell someCell;
    someCell = new Cell()
            .setPadding(0)
            .setBorder(Border.NO_BORDER)
            .setVerticalAlignment(VerticalAlignment.TOP);
    return someCell;
}
 private Cell createTableCellWithTopBorder() {
    Cell someCell;
    someCell = new Cell()
            .setPadding(0)
            .setBorderBottom(Border.NO_BORDER).setBorderLeft(Border.NO_BORDER).setBorderRight(Border.NO_BORDER).setBorderTop(new SolidBorder(1f))
            .setVerticalAlignment(VerticalAlignment.TOP);
    return someCell;
}

private Cell createTableCellWithTopAndBottomBorders() {
    Cell someCell;
    someCell = new Cell()
            .setPadding(0)
            .setBorder(Border.NO_BORDER).setBorderBottom(new SolidBorder(1f)).setBorderTop(new SolidBorder(1f))
            .setVerticalAlignment(VerticalAlignment.TOP);
    return someCell;
}

private Cell createDoubleTableCell() {
    Cell someCell;
    someCell = new Cell(1, 2)
            .setPadding(0)
            .setBorder(Border.NO_BORDER)
            .setVerticalAlignment(VerticalAlignment.TOP);
    return someCell;
}

}

The NullPointerException issue is essentially the same bug as analyzed in this answer . In response to that, the bug has been fixed in the development code.

Thus, it didn't come as a surprise that running the test class using the current iText development code (the current 7.0.1-SNAPSHORT) did not result in an exception.

Furthermore, the result of both test methods identically looks like this:

TableBorderTest.png

Thus, the issue of itext ignoring no-border requests and just rendering all borders also seems to have been fixed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM