iText中的PdfPageEventHelper [英] PdfPageEventHelper in iText

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

问题描述

我正在创建一个pdf,其中每个pdf页面的标题将根据当前页码自定义。例如,在第一页中标题是第一页,在第二页中标题是第二页,依此类推......

I am creating a pdf where the title of each pdf page would be customized based on the current page number. For example, in the 1st page the title is "First Page", and in the 2nd page the title is "Second Page", and so on...

什么我们现在要做的是将标题添加到PdfPTable中,然后我们向PdfPTable添加了很多其他内容,因此这个PdfPTable包含几页数据。最后,我们将这个大的PdfPTable对象添加到文档中。现在我们要使用PdfPageEventHelper中的onStartPage()方法来获取当前页码,以便我们可以自定义每个页面的标题。

What we do now is we add the title to a PdfPTable, then we add a lot of other stuff to the PdfPTable as well, so this PdfPTable contains several pages of data. Finally we add this large PdfPTable object to document. Now we want to use the onStartPage() method in PdfPageEventHelper to get the current page number so that we can customize the title for each page.

问题是onStartPage()在我们将大的PdfPTable对象添加到文档之前不会触发,这意味着我们无法在资源包之前加载不同的键值。 PdfPTable对象被添加到文档中,对吗?有什么建议可以实现这个吗?

The problem is onStartPage() does not trigger until we add that large PdfPTable object to the document, which means we cannot make the resource bundle to load different key values before the PdfPTable object is added to the document, right? Any suggestion to realize this?

--------------------我们有如下代码---- ---------------------------------

--------------------we have codes like below-------------------------------------

  Phrase title = new Phrase();
  title.add(new Chunk(bundle.getString(pdfNewPageEventHandler.getKey()), headerFont));
  PdfPCell cell = new PdfPCell(new Paragraph(
            new Phrase(title)));
  .........
  PdfPTable table = new PdfPTable(tableSize);
  table.addCell(cell);
  .........
  document.add(table);




private class PdfNewPageEventHandler extends PdfPageEventHelper {

    private int currentPageNum = 0;
    private String key;

    @Override
    public void onStartPage(PdfWriter writer, Document document) {

        currentPageNum = currentPageNum + 1;

        if (currentPageNum == 1) {
            key = "firstPage";
        } 
        else if (currentPageNum == 2) {
            key = "secondPage";
        }

    }

    public String getKey() {
        return key;
    }

}


推荐答案

我有不止一个答案。我不知道哪一个适用于您的具体情况:

I have more than one answer. I don't know which one applies to your specific situation:


  1. 不要在<$ c $中添加内容c> onStartPage()方法。如文档所述,所有内容都应添加到 onEndPage()方法中。

  1. Don't ever add content in the onStartPage() method. As documented, all content should be added in the onEndPage() method.

这并不总是明智的创建一个大表(它在内存中构建),然后将表添加到文档中(仅在此时,可以释放内存)。也许您想从文档中尝试一些大表策略

It's not always wise to create one large table (it builds up in memory) and then add the table to the document (only at this moment, memory can be freed). Maybe you want to try out some of the large table strategies from the documentation.

在某些情况下,在内存中构建一个表然后将其添加到文档是您可以使用的唯一策略。然后,iText将在不同页面上分发表格内容,从而触发页面事件。但是:如果要触发特定于表的事件,还可以在表级别定义事件。有一个 PdfPTableEventSplit 和一个 PdfPTableEventAfterSplit 类用于此目的。

In some cases, building a table in memory and then adding it to the document is the only strategy you can use. iText will then distribute the content of the table over different pages, triggering page events. However: if you want to trigger events that are specific to the table, you can also define events at the level of the table. There's a PdfPTableEventSplit and a PdfPTableEventAfterSplit class for this exact purpose.

您提供的代码示例并没有真正说明问题。你能不能重新解释一下这个问题,因为我不确定我的答案能不能解决问题的核心。

The code sample you provided, didn't really illustrate the problem. Can you please rephrase the problem, as I'm not sure if any of my answers go to the core of the problem.

这篇关于iText中的PdfPageEventHelper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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