java - iText setRowspan 不适用于标题中的表格?

标签 java itext

完全相同的代码用于创建 PdfPTable用于文档的主体和文档的页眉。该表格在主体中正确呈现,但似乎没有考虑标题中的 setRowspan 值。

下面我粘贴了生成的 pdf 的屏幕截图,然后是用于生成 pdf 的最小独立程序。

PDF输出

enter image description here

代码

import java.io.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

class HeaderAndFooter extends PdfPageEventHelper{

    private PdfPTable header;

    public HeaderAndFooter() throws Exception {
        header = FooMain.createTable();
        header.setTotalWidth(530);
    }

    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        PdfContentByte cb = writer.getDirectContent();
        float x   = document.leftMargin();
        float hei = header.getTotalHeight();
        float y   = document.top()+hei;
        header.writeSelectedRows(0, -1, x , y, cb);
    }
}

public class FooMain {

    public static void main(String[] args) throws Exception {
        Document doc = new Document(PageSize.A4, 30, 30, 150, 40);
        PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("./brokenTableInHeader.pdf"));
        writer.setPageEvent(new HeaderAndFooter());
        doc.open();
        doc.add(createTable());
        doc.close();
    }

    public static PdfPTable createTable() throws Exception {
        PdfPTable table = new PdfPTable(2);
        PdfPCell cell = new PdfPCell(new Phrase("cell spanning two rows")); 
        cell.setRowspan(2);
        table.addCell(cell);
        table.addCell( new Phrase ("a"));
        table.addCell( new Phrase ("b"));
        return table;
    }
}

最佳答案

this SO question 中找到答案(使用 ColumnText::go 而不是 PdfPTable::writeSelectedRows)

这是我的工作代码的摘录:

ColumnText column = new ColumnText(writer.getDirectContent());
column.addElement(footer);
column.setSimpleColumn (0, 0, 630, 80);
column.go();

关于java - iText setRowspan 不适用于标题中的表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16087542/

相关文章:

java - Android 旋转器与对象

Java - 如何制作 JButton "Deselected"

java - 带有 Spring JMS 的 ActiveMQ - 如何发送 NONPERSISTENT 消息?

java - JsonMappingException 无单字符串构造函数/工厂方法 Jackson

java - 如何扩展现有 PDF 的页面大小以在 iText 中添加页脚

关闭后立即从 Web 服务器删除文件的 C# 技术

java - 在 iText 中将图像添加到 acrofield?

java - 返回两个或多个值

itext - HTML 到 iTextSharp 行高转换

java - iText - 可点击的图像应该打开 MS Word 附件