Java itext 7表格边界问题
当前版本的itext 7在呈现表格中某些单元格跨越几行的单元格边框时遇到问题 呈现边框的正确方式(通过文档,如下所示)会导致空指针异常Java itext 7表格边界问题,java,itext7,Java,Itext7,当前版本的itext 7在呈现表格中某些单元格跨越几行的单元格边框时遇到问题 呈现边框的正确方式(通过文档,如下所示)会导致空指针异常 .setBorder(Border.NO_BORDER) .setBorderTop(new SolidBorder(1f)) 直观的方式(如下所示)不忽略任何边界请求,只渲染所有边界 这只会发生在第一行,最后一个右边的单元格(在我的例子中标记为“1.4”)。 除此之外,我发现iText7是一个令人惊叹的产品,也是伟大软件工程的一个例子。谢谢 下面是说明这两
.setBorder(Border.NO_BORDER)
.setBorderTop(new SolidBorder(1f))
直观的方式(如下所示)不忽略任何边界请求,只渲染所有边界
这只会发生在第一行,最后一个右边的单元格(在我的例子中标记为“1.4”)。
除此之外,我发现iText7是一个令人惊叹的产品,也是伟大软件工程的一个例子。谢谢
下面是说明这两种情况的完整源代码:
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;
}
}NullPointerException问题本质上与本文分析的错误相同。作为回应,开发代码中的bug已经修复 因此,使用当前的iText开发代码(当前的7.0.1-SNAPSHORT)运行测试类并没有导致异常并不令人惊讶
.setBorder(Border.NO_BORDER)
.setBorderTop(new SolidBorder(1f))
此外,两种试验方法的结果相同,如下所示:
因此,itext忽略无边框请求而只呈现所有边框的问题似乎也已得到解决。NullPointerException问题本质上与本文分析的问题相同。作为回应,开发代码中的bug已经修复 因此,使用当前的iText开发代码(当前的7.0.1-SNAPSHORT)运行测试类并没有导致异常并不令人惊讶
.setBorder(Border.NO_BORDER)
.setBorderTop(new SolidBorder(1f))
此外,两种试验方法的结果相同,如下所示:
因此,itext忽略无边框请求而只呈现所有边框的问题似乎也已得到解决。NullPointerException问题本质上与分析的问题相同。如果我没记错的话,它已经在开发代码中修复了。我刚刚使用iText 7.0.1-SNAPSHOT运行了您的测试代码,肯定没有例外。关于另一个问题,我不能确切地确定预期输出和您观察到的输出是什么,但确实没有iText只呈现所有边界的单元格:根本没有垂直边界。顺便说一下,很高兴你提供了一个可运行的示例。我更新了这个问题,以包括我得到的输出,其中单元格1.4呈现了所有四个边框。我还没有尝试过7.0.1-SNAPSHOT,所以希望它能解决这两个问题。谢谢。我想你有理由抱有希望。据我回忆,我在office的测试运行结果在这两种情况下都只有1.4的上边框。
NullPointerException
问题本质上与分析的错误相同。如果我没记错的话,它已经在开发代码中修复了。我刚刚使用iText 7.0.1-SNAPSHOT运行了您的测试代码,肯定没有例外。关于另一个问题,我不能确切地确定预期输出和您观察到的输出是什么,但确实没有iText只呈现所有边界的单元格:根本没有垂直边界。顺便说一下,很高兴你提供了一个可运行的示例。我更新了这个问题,以包括我得到的输出,其中单元格1.4呈现了所有四个边框。我还没有尝试过7.0.1-SNAPSHOT,所以希望它能解决这两个问题。谢谢。我想你有理由抱有希望。据我回忆,我在office中对这两种情况的测试结果都只有1.4的顶部边界。谢谢,我刚刚运行并验证了这两个问题将在7.0.1版本中得到修复。谢谢,我刚刚运行并验证了这两个问题将在7.0.1版本中得到修复。