【问题标题】:Cannot display background color when using rowspan with itext pdf将rowspan与itext pdf一起使用时无法显示背景颜色
【发布时间】:2015-05-15 19:16:25
【问题描述】:

我正在尝试创建一个如下所示的表格:

我想使用 2 的行跨度。问题是当我使用行跨度时,顶行的背景颜色显示正常,但第二行显示为白色。

我遇到了同样的问题:http://itext.2136553.n4.nabble.com/Rowspan-and-background-color-td4659361.html

我正在尝试在 Java 中执行此操作。我在这里错过了什么愚蠢的东西吗?

【问题讨论】:

    标签: java pdf pdf-generation itextsharp itext


    【解决方案1】:

    请查看SimpleTable10 示例。在此示例中,我尝试通过创建一个与您的表格完全相同的表格来重现您的问题,只是我为每个单元格赋予了不同的背景颜色:

    您声称第一行的背景颜色显示正常,但第二行显示为白色。 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)。

    【讨论】:

    • 谢谢。我的问题解决了。问题是我使用的是早期版本,itext 5.4.4。更新到最新的可用 maven 版本 5.5.2 让它工作。感谢图书馆创建者的回应:)
    猜你喜欢
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多