首页 > 解决方案 > 使用 OpenPDF 在 PDF 的每一页上的页眉和页脚块

问题描述

我想使用 OpenPDF 创建发票 PDF。它需要在每一页上都有页眉和页脚。我正在使用 Thymeleaf 生成 pdf 的内容。然后我的问题是如何为每个页面添加页眉和/或页脚。

我找到了一个相关的帖子(使用 OpenPDF 在 PDF 的每一页上的矩形页眉和页脚块),但是我希望能够使用 html 模板字符串定义页眉/页脚。那可能吗?或者您会推荐其他方法吗?

// create and register page event to add header and footer to each page
writer.setPageEvent(new PdfPageEventHelper() {
    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        PdfContentByte cb = writer.getDirectContent();
        cb.rectangle(header); // <- header should be a html template
        cb.rectangle(footer); // <- footer should be a html template
    }
});

标签: pdfpdf-generationopenpdf

解决方案


推荐阅读