页眉与表重叠 [英] Page Header overlaps with Table

查看:254
本文介绍了页眉与表重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在itext中,我的pdf中有一个表格。当表格填满页面时,条目会在下一页继续,但它与我的pdf页面标题重叠。我该如何避免?

解决方案

请查看



这证明你的指控它不起作用是错误的。请理解,在人们详细解释如何做某事后,说它不起作用是不礼貌的。通过显示它确实起作用,阅读此内容的人可以将你的指控解释为谎言(这对你的业力有害)。


In itext, I have a table in my pdf. When the table gets full for a page the entries continues on the next page but it overlaps with my pdf page header. How do I avoid that?

解决方案

Please take a look at the TableHeader example.

In this example, I create a document with some "Hello World" content:

public void createPdf(String filename) throws IOException, DocumentException {
    TableHeader.HeaderTable event = new TableHeader.HeaderTable();
    // step 1
    Document document = new Document(PageSize.A4, 36, 36, 20 + event.getTableHeight(), 36);
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
    writer.setPageEvent(event);
    // step 3
    document.open();
    // step 4
    for (int i = 0; i < 50; i++)
        document.add(new Paragraph("Hello World!"));
    document.newPage();
    document.add(new Paragraph("Hello World!"));
    document.newPage();
    document.add(new Paragraph("Hello World!"));
    // step 5
    document.close();
}

As you can see, I also define a TableHeader event. I use this event as a page event, but I also need this event when I define the Document. I use the following value as top margin:

20 + event.getTableHeight()

What does this mean? Let's take a look at the implementation of this event:

public class HeaderTable extends PdfPageEventHelper {
    protected PdfPTable table;
    protected float tableHeight;
    public HeaderTable() {
        table = new PdfPTable(1);
        table.setTotalWidth(523);
        table.setLockedWidth(true);
        table.addCell("Header row 1");
        table.addCell("Header row 2");
        table.addCell("Header row 3");
        tableHeight = table.getTotalHeight();
    }

    public float getTableHeight() {
        return tableHeight;
    }

    public void onEndPage(PdfWriter writer, Document document) {
        table.writeSelectedRows(0, -1,
                document.left(),
                document.top() + ((document.topMargin() + tableHeight) / 2),
                writer.getDirectContent());
    }
}

When I create the event, a PdfPTable is constructed. I store this table as a member variable, along with the height of this table: tableHeight.

I use this tableHeight when I define the top margin, so that I am 100% sure that the table will fit the margin. I add an additional 20 user units because I don't want the table to stick to the top border of the page:

20 + event.getTableHeight()

When I add the table in the onEndPage() method, I use the following coordinates:

x = document.left()
y = document.top() + ((document.topMargin() + tableHeight) / 2),

The value of document.top() is the value of the top of the page minus the top margin. I add some extra space, more specifically the difference of the top margin and the table height divided by two, added to the table height:

tableHeight + ((document.topMargin() - tableHeight) / 2)

This formula can be simplified to:

((document.topMargin() + tableHeight) / 2)

As you can see, all of this is simple Math, the kind of Math you are taught in primary school.

The resulting PDF looks like this:

This proves that your allegation that "it doesn't work" is wrong. Please understand that it is not polite to say "it doesn't work" after people have explained in great detail how to do something. By showing that it does work, people reading this could interpret your allegation as a lie (and that is bad for your karma).

这篇关于页眉与表重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆