Java 奇怪的setRowspan错误/不工作

Java 奇怪的setRowspan错误/不工作,java,pdf,itext,Java,Pdf,Itext,我为有6列的iText表设置了一个标题,后来我想对另一个表使用相同的标题,并将colspans设置为更通用的,但是一个rowspan不再起作用 这是我的原始(工作)代码,共有6列: public static PdfPTable createHeaderContent() { PdfPTable table = new PdfPTable(6); table.setWidthPercentage(100); PdfPCell dobicell = new PdfPCel

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

这是我的原始(工作)代码,共有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;
}
结果看起来应该是这样的(我用了一些漂亮的颜色来表示跨度:

“通用”的修改代码基本相同:

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;
}
公共静态PdfPTable createHeaderContent(int[]coldist){
PdfPTable=newpdfptable(coldist[0]+coldist[1]+coldist[2]);//createHeaderContent(newint[]{4,7,4,4,7});
表1.设定宽度百分比(100);
PdfPCell-dobicell=新的PdfPCell();
setColspan(coldist[0]);//以前是2,现在是4
添加元素(新短语(doctype,DOBIFONTADR));
dobicell.setboorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.TOP);
表2.addCell(dobicell);
dobicell=新的PdfPCell();
setColspan(coldist[1]);//以前是2,现在是7
dobicell.addElement(新短语(“Ing.Mario J.Schwaiger”,Dobifonttite));
dobicell.setboorder(矩形,顶部);
表2.addCell(dobicell);
dobicell=Dobilogo.getPiccell(92104);
dobicell.setboorder(Rectangle.TOP | Rectangle.RIGHT);
setColspan(coldist[2]);//以前是3,现在是4

dobicell.setRowspan(2);//这里有两个问题,一个是iText似乎忽略的,另一个是导致问题的原因:

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

    iText似乎忽略了此处缺少的列

  • 代码将第一行声明为表的标题:
    table.setHeaderRows(1)
    。这与将第一行中的最后一个单元格声明为跨2行相冲突
    setRowspan(2)

    iText在此忽略标题行中的行间距,从而导致不希望出现的外观

    要解决这个问题,根本不要声明行范围,或者使用至少两行的行范围(如果后面有足够的行)

在评论中,OP证实了这一点

在修改后的版本中,我仍然有
setHeaderRows(1)
。将其替换为
setHeaderRows(2)
解决了这个问题


您的“具有6列的原始(工作)代码”看起来很奇怪,因为严格来说它需要7列:您有两个
setColspan(2)
和一个
setColspan(3)
,所以总结起来您有7列。此外,您有
setHeaderRows(1)
-您希望如何将其与
setRowspan(2)结合使用
在单标题行中?您已经删除了“修改代码”中的第一个问题但是保留了第二个问题。似乎两个问题碰巧结合在一起,相互抵消,但第二个问题本身就导致了你的观察。我刚刚检查了整个代码,这很奇怪:是的,你完全正确,我有一个
setColspan(3)
。但是把它放回
setColspan(2)
没有任何效果。当我写这个问题时,我做了一些测试,可能它漏掉了并保留了下来。与
setHeaderRows(1)相关
-您也注意到了。不,这是原始代码。当我在这部分编程时,我首先从可用的在线资源复制了一些工作代码,但忘了调整它…因为它工作。
setHeaderRows(2)
什么也没做。显然是因为行跨度,但这只是猜测。你是对的。在修改后的版本中,我仍然有
setHeaderRows(1)
。将其替换为
setHeaderRows(2)
解决了问题OK,我会让我的评论成为实际的答案。。。