如何将HTML页眉和页脚添加到页面? [英] How to add HTML headers and footers to a page?

查看:1509
本文介绍了如何将HTML页眉和页脚添加到页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用itext从html源添加标题到pdf?

How to add header to pdf from an html source using itext?

目前,我们已经扩展了PdfPageEventHelper并覆盖了这些方法。工作正常,但是当我到达2页以上时会抛出RuntimeWorkerException。

Currently, we have extended PdfPageEventHelper and overriden these methods. Works fine but it throws a RuntimeWorkerException when I get to 2+ pages.

 @Override
    void onStartPage(PdfWriter writer, Document document) {
        InputStream is = new ByteArrayInputStream(header?.getBytes());
        XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);
    }

    @Override
    void onEndPage(PdfWriter writer, Document document) {
        InputStream is = new ByteArrayInputStream(footer?.getBytes());
        XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);

    }


推荐答案

这是禁止一般在 onStartPage()事件中添加内容。 禁止将内容添加到 onEndPage()中的文档对象。您应该使用 PdfWriter NOT onEndPage()方法中添加页眉和页脚>文件。另外:通过一遍又一遍地解析HTML,你浪费了大量的CPU。

It is forbidden to add content in the onStartPage() event in general. It is forbidden to add content to the document object in the onEndPage(). You should add your header and your footer in the onEndPage() method using PdfWriter, NOT document. Also: you are wasting plenty of CPU by parsing the HTML over and over again.

请看一下 HtmlHeaderFooter 示例。

它有两个HTML代码段,一个用于页眉,一个用于页脚。

It has two snippets of HTML, one for the header, one for the footer.

public static final String HEADER =
    "<table width=\"100%\" border=\"0\"><tr><td>Header</td><td align=\"right\">Some title</td></tr></table>";
public static final String FOOTER =
    "<table width=\"100%\" border=\"0\"><tr><td>Footer</td><td align=\"right\">Some title</td></tr></table>";

请注意,有更好的方法来描述页眉和页脚,而不是使用HTML,但也许它是一个你的要求,所以我不会问你为什么不使用官方文档。顺便提一下:您可以在免费电子书中找到解决问题所需的所有信息。 所以你可能想要下载它...

Note that there are better ways to describe the header and footer than by using HTML, but maybe it's one of your requirements, so I won't ask you why you don't use any of the methods that is explained in the official documentation. By the way: all the information you need to solve your problem can also be found in that free ebook so you may want to download it...

我们将在页面事件中只阅读一次这些HTML片段,然后我们将渲染元素并在每一页上重复:

We will read these HTML snippets only once in our page event and then we'll render the elements over and over again on every page:

public class HeaderFooter extends PdfPageEventHelper {
    protected ElementList header;
    protected ElementList footer;
    public HeaderFooter() throws IOException {
        header = XMLWorkerHelper.parseToElementList(HEADER, null);
        footer = XMLWorkerHelper.parseToElementList(FOOTER, null);
    }
    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        try {
            ColumnText ct = new ColumnText(writer.getDirectContent());
            ct.setSimpleColumn(new Rectangle(36, 832, 559, 810));
            for (Element e : header) {
                ct.addElement(e);
            }
            ct.go();
            ct.setSimpleColumn(new Rectangle(36, 10, 559, 32));
            for (Element e : footer) {
                ct.addElement(e);
            }
            ct.go();
        } catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }
}

你看到我们的机制吗?用于添加从XML Worker获取的元素对象?我们创建一个 ColumnText 对象,该对象将写入 writer 直接内容(使用禁止文件。我们定义了一个 Rectangle ,我们使用 go()来渲染元素。

Do you see the mechanism we use to add the Element objects obtained from XML Worker? We create a ColumnText object that will write to the direct content of the writer (using the document is forbidden). We define a Rectangle and we using go() to render the elements.

结果显示在 html_header_footer.pdf 中。

这篇关于如何将HTML页眉和页脚添加到页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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