将 rowspan 与 itext pdf 一起使用时无法显示背景颜色
Cannot display background color when using rowspan with itext pdf
我正在尝试创建一个如下所示的 table:
我想使用 rowspan 2。问题是当我使用 rowspan 时,第一行的背景颜色显示正常,但第二行显示白色。
我遇到了此处显示的相同问题:http://itext.2136553.n4.nabble.com/Rowspan-and-background-color-td4659361.html
我正在尝试在 Java 中执行此操作。我在这里错过了什么傻事吗?
请查看 SimpleTable10 示例。在这个例子中,我试图通过创建一个看起来与你的完全一样的 table 来重现你的问题,除了我为每个单元格提供了不同的背景颜色:
您声称第一行的背景颜色显示正常,但第二行显示白色。 simple_table10.pdf 示例的屏幕截图与此指控相矛盾。
请提供一些重现该问题的示例代码,或检查我的代码以了解我如何能够为行跨度或列跨度大于 1 的单元格创建彩色背景:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable table = new PdfPTable(5);
PdfPCell sn = new PdfPCell(new Phrase("S/N"));
sn.setRowspan(2);
sn.setBackgroundColor(BaseColor.YELLOW);
table.addCell(sn);
PdfPCell name = new PdfPCell(new Phrase("Name"));
name.setColspan(3);
name.setBackgroundColor(BaseColor.CYAN);
table.addCell(name);
PdfPCell age = new PdfPCell(new Phrase("Age"));
age.setRowspan(2);
age.setBackgroundColor(BaseColor.GRAY);
table.addCell(age);
PdfPCell surname = new PdfPCell(new Phrase("SURNAME"));
surname.setBackgroundColor(BaseColor.BLUE);
table.addCell(surname);
PdfPCell firstname = new PdfPCell(new Phrase("FIRST NAME"));
firstname.setBackgroundColor(BaseColor.RED);
table.addCell(firstname);
PdfPCell middlename = new PdfPCell(new Phrase("MIDDLE NAME"));
middlename.setBackgroundColor(BaseColor.GREEN);
table.addCell(middlename);
PdfPCell f1 = new PdfPCell(new Phrase("1"));
f1.setBackgroundColor(BaseColor.PINK);
table.addCell(f1);
PdfPCell f2 = new PdfPCell(new Phrase("James"));
f2.setBackgroundColor(BaseColor.MAGENTA);
table.addCell(f2);
PdfPCell f3 = new PdfPCell(new Phrase("Fish"));
f3.setBackgroundColor(BaseColor.ORANGE);
table.addCell(f3);
PdfPCell f4 = new PdfPCell(new Phrase("Stone"));
f4.setBackgroundColor(BaseColor.DARK_GRAY);
table.addCell(f4);
PdfPCell f5 = new PdfPCell(new Phrase("17"));
f5.setBackgroundColor(BaseColor.LIGHT_GRAY);
table.addCell(f5);
document.add(table);
document.close();
}
确保您使用最新的官方版本的 iText(Sharp)。我们知道有人一直在分发 iText(Sharp) 的过时(和有缺陷)版本。
我正在尝试创建一个如下所示的 table:
我想使用 rowspan 2。问题是当我使用 rowspan 时,第一行的背景颜色显示正常,但第二行显示白色。
我遇到了此处显示的相同问题:http://itext.2136553.n4.nabble.com/Rowspan-and-background-color-td4659361.html
我正在尝试在 Java 中执行此操作。我在这里错过了什么傻事吗?
请查看 SimpleTable10 示例。在这个例子中,我试图通过创建一个看起来与你的完全一样的 table 来重现你的问题,除了我为每个单元格提供了不同的背景颜色:
您声称第一行的背景颜色显示正常,但第二行显示白色。 simple_table10.pdf 示例的屏幕截图与此指控相矛盾。
请提供一些重现该问题的示例代码,或检查我的代码以了解我如何能够为行跨度或列跨度大于 1 的单元格创建彩色背景:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable table = new PdfPTable(5);
PdfPCell sn = new PdfPCell(new Phrase("S/N"));
sn.setRowspan(2);
sn.setBackgroundColor(BaseColor.YELLOW);
table.addCell(sn);
PdfPCell name = new PdfPCell(new Phrase("Name"));
name.setColspan(3);
name.setBackgroundColor(BaseColor.CYAN);
table.addCell(name);
PdfPCell age = new PdfPCell(new Phrase("Age"));
age.setRowspan(2);
age.setBackgroundColor(BaseColor.GRAY);
table.addCell(age);
PdfPCell surname = new PdfPCell(new Phrase("SURNAME"));
surname.setBackgroundColor(BaseColor.BLUE);
table.addCell(surname);
PdfPCell firstname = new PdfPCell(new Phrase("FIRST NAME"));
firstname.setBackgroundColor(BaseColor.RED);
table.addCell(firstname);
PdfPCell middlename = new PdfPCell(new Phrase("MIDDLE NAME"));
middlename.setBackgroundColor(BaseColor.GREEN);
table.addCell(middlename);
PdfPCell f1 = new PdfPCell(new Phrase("1"));
f1.setBackgroundColor(BaseColor.PINK);
table.addCell(f1);
PdfPCell f2 = new PdfPCell(new Phrase("James"));
f2.setBackgroundColor(BaseColor.MAGENTA);
table.addCell(f2);
PdfPCell f3 = new PdfPCell(new Phrase("Fish"));
f3.setBackgroundColor(BaseColor.ORANGE);
table.addCell(f3);
PdfPCell f4 = new PdfPCell(new Phrase("Stone"));
f4.setBackgroundColor(BaseColor.DARK_GRAY);
table.addCell(f4);
PdfPCell f5 = new PdfPCell(new Phrase("17"));
f5.setBackgroundColor(BaseColor.LIGHT_GRAY);
table.addCell(f5);
document.add(table);
document.close();
}
确保您使用最新的官方版本的 iText(Sharp)。我们知道有人一直在分发 iText(Sharp) 的过时(和有缺陷)版本。