我为我的iText表设置了一个标题,该标题有6列,后来我想对另一个表使用相同的标题,并将colspans设置为更通用,但是一个rowpan不再起作用。

这是我原始的(有效的)代码,共有6列:

public static PdfPTable createHeaderContent() {
    PdfPTable table = new PdfPTable(6);
    table.setWidthPercentage(100);

    PdfPCell dobicell = new PdfPCell();
    dobicell.setColspan(2);
    dobicell.addElement(new Phrase(docType, DOBIFONTADR));
    dobicell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.TOP);
    table.addCell(dobicell);

    dobicell = new PdfPCell();
    dobicell.setColspan(2);
    dobicell.addElement(new Phrase("Ing. Mario J. Schwaiger", DOBIFONTADR));
    dobicell.setBorder(Rectangle.TOP);
    table.addCell(dobicell);

    dobicell = Dobilogo.getPiccell(92, 104);
    dobicell.setBorder(Rectangle.TOP | Rectangle.RIGHT);
    dobicell.setColspan(3);
    dobicell.setRowspan(2);
    table.addCell(dobicell);

    dobicell = getKundenCol(kunde);
    dobicell.setColspan(2);
    dobicell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
    table.addCell(dobicell);

    dobicell = getUserCell(user);
    dobicell.setColspan(2);
    table.addCell(dobicell);

    table.setHeaderRows(1);
    return table;
}


结果看起来应该是这样(我使用了一些漂亮的颜色来表示跨度:
java - 奇怪的setRowspan错误/不起作用-LMLPHP

用于“通用”的修改后的代码几乎相同:

public static PdfPTable createHeaderContent(int[] coldist) {
    PdfPTable table = new PdfPTable(coldist[0] + coldist[1] + coldist[2]); //createHeaderContent(new int[]{4, 7, 4, 4, 7});
    table.setWidthPercentage(100);

    PdfPCell dobicell = new PdfPCell();
    dobicell.setColspan(coldist[0]); //used to be 2, now 4
    dobicell.addElement(new Phrase(doctype, DOBIFONTADR));
    dobicell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.TOP);
    table.addCell(dobicell);

    dobicell = new PdfPCell();
    dobicell.setColspan(coldist[1]); //used to be 2, now 7
    dobicell.addElement(new Phrase("Ing. Mario J. Schwaiger", DOBIFONTTITEL));
    dobicell.setBorder(Rectangle.TOP);
    table.addCell(dobicell);

    dobicell = Dobilogo.getPiccell(92, 104);
    dobicell.setBorder(Rectangle.TOP | Rectangle.RIGHT);
    dobicell.setColspan(coldist[2]); //used to be 3, now 4
    dobicell.setRowspan(2);  // <--- This is fishy, but why?
    table.addCell(dobicell);

    dobicell = getKundenCol(kunde);
    dobicell.setColspan(coldist[3]); //used to be 2, now 4
    dobicell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
    table.addCell(dobicell);

    dobicell = getUserCell(user);
    dobicell.setColspan(coldist[4]); //used to be 2, now 7
    table.addCell(dobicell);

    table.setHeaderRows(1);
    return table;
}


但是最后一栏有问题:
java - 奇怪的setRowspan错误/不起作用-LMLPHP

起初,我假设隐藏了另一行并使用了dobicell.setRowspan(3);,但这已经更改了第一个数据行。尝试添加另一个单元格,将其放在标题之后的第一行。

奇怪的是,当我将用户单元的最后一部分扩大到整个行跨单元时,它们消失了。

这个问题有解决方案或原因吗?

最佳答案

这里有两个问题,一个是iText似乎忽略的问题,另一个是导致问题的原因:


在原始代码中,单元格需要7列,因为它们连续有两次setColspan(2)和一次setColspan(3)。但是该表仅用于6列:new PdfPTable(6)

iText似乎忽略了此处缺少的列...
该代码将第一行声明为表的标题:table.setHeaderRows(1)。这与第一行中最后一个单元格的声明跨越2行setRowspan(2)相冲突。

在此,iText会忽略标题行中的行跨度,从而导致出现不希望的外观。

要解决此问题,请不要声明任何行跨度,也不要使用至少两个行跨度(如果要跟随足够的行)。


OP在评论中确认


  在修改后的版本中,我仍然有setHeaderRows(1)。用setHeaderRows(2)替换它可以解决此问题

10-02 10:20
查看更多