iText Flying Saucer pdf标题并忽略html

发布于 2024-11-19 14:44:19 字数 542 浏览 4 评论 0 原文

我们使用 xhtml 转 pdf 非常成功,但是出现了一个新的要求,即每页上都有标题和页数。我们正在使用 Flying Saucer 的新版本。

我在这里遵循示例: http://today.java.net/pub/a/today/2007/06/26/generate-pdfs-with-flying-saucer-and-itext.html#page-specific-features< /a>

...但这行不通。标题将位于第一页的左上角。

如果我使用 r7 版本,标题和页码工作正常,但没有渲染传入的 html,而在 r8 中,标题\页码被忽略,但 html 渲染完美。用于测试的 xHTML 是从上面的 url 复制的。

我知道我一定错过了一些非常简单的东西,如果有人有任何想法\评论,我将非常感激听到。

We use xhtml to pdf with good success, but a new requirement came up to have headers and page count on every page. We are using newset release of Flying Saucer.

I followed example here: http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html#page-specific-features

...but this would not work. The header would be top left on first page.

If I use the r7 version, headers and page numbering works perfectly, but none of the passed in html is rendered, whilst in r8 the headers\ page numbers are ignored, but the html is rendered perfectly. xHTML used for tests is copied from url above.

I know I must be missing something very simple, if anyone has any ideas\ comments, I would be very grateful to hear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老街孤人 2024-11-26 14:44:19

我们使用相同的方法,一切都很完美,但是我决定不使用飞碟的内置页眉/页脚,而是使用 PdfStamper 在生成 PDF 后添加它们,它工作得很好,这是一个例子。

    public void modifyPdf(PdfStamper stamper) {
        this.reader = stamper.getReader();

        PdfContentByte under = null;

        PdfPTable header = null;
        PdfPTable footer = null;

        final int total = this.reader.getNumberOfPages();
        for (int page = 1; page <= total; page++) {
            under = stamper.getUnderContent(page);

            final PdfDocument doc = under.getPdfDocument();
            final Rectangle rect = this.reader.getPageSizeWithRotation(page);

            header = ... //build your header
            footer = ... // build your footer

            final float x = 0;

            //write header to PDF
            if (header != null) {
                float y = (rect.getTop() - 0);
                header.writeSelectedRows(0, -1, x, y, under);
            }

            //write footer to PDF
            if (footer != null) {
                float y = (rect.getBottom() + 20);
                footer.writeSelectedRows(0, -1, x, y, under);
            }
        }
    }

你可以像这样构建你的压模:

        final PdfReader reader = new PdfReader(/*your pdf file*/);
        final PdfStamper stamper = new PdfStamper(reader, /* output */);

希望你发现这有帮助。

We use the same method and everything works perfectly, I have however decided not to use flying-saucer's built in headers/footers and use a PdfStamper to add them after the PDF is generated, it works quite well, here is an example.

    public void modifyPdf(PdfStamper stamper) {
        this.reader = stamper.getReader();

        PdfContentByte under = null;

        PdfPTable header = null;
        PdfPTable footer = null;

        final int total = this.reader.getNumberOfPages();
        for (int page = 1; page <= total; page++) {
            under = stamper.getUnderContent(page);

            final PdfDocument doc = under.getPdfDocument();
            final Rectangle rect = this.reader.getPageSizeWithRotation(page);

            header = ... //build your header
            footer = ... // build your footer

            final float x = 0;

            //write header to PDF
            if (header != null) {
                float y = (rect.getTop() - 0);
                header.writeSelectedRows(0, -1, x, y, under);
            }

            //write footer to PDF
            if (footer != null) {
                float y = (rect.getBottom() + 20);
                footer.writeSelectedRows(0, -1, x, y, under);
            }
        }
    }

you can build your stamper like this:

        final PdfReader reader = new PdfReader(/*your pdf file*/);
        final PdfStamper stamper = new PdfStamper(reader, /* output */);

Hope you find this helpful.

命硬 2024-11-26 14:44:19

我认为他们在 r8 中更改了此功能....请尝试使用此方法:

https://gist.github.com/ 626264

I think they changed this functionality in r8.... try this method instead:

https://gist.github.com/626264

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文