使用iText7(Java)将表添加到现有PDF并在其他页面上继续 [英] Using iText7 (Java) to add a table to an existing PDF and continue on additional pages

查看:1515
本文介绍了使用iText7(Java)将表添加到现有PDF并在其他页面上继续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试完成一个项目,该项目的要求几乎与与

I am attempting to complete a project with almost identical requirements as those associated with this question asked in 2015.

Bruno提供的答案是完美的,但与iText5有关.我对iText来说还比较陌生,并且极力想赶上最新的进度来完成当前项目.

The answer provided by Bruno was perfect, but related to iText5. I am relatively new to iText, and am desperately trying to get up-to-speed to complete a current project.

  • 我需要填充PDF文档的字段
  • 我需要在填充部分下方添加一个表格,此后该表格需要跨越多个页面

有人可以协助将Bruno的答案从iText5转换为iText7吗?

Can anyone assist with the translation of Bruno's answer from iText5 to iText7?

非常感谢您提供任何/所有帮助!

Thanks so much in advance for any/all assistance!

推荐答案

您应该这样写:

    PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
    Document doc = new Document(pdfDoc);
    PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
    Map<String, PdfFormField> fields = form.getFormFields();
    fields.get("Name").setValue("Jeniffer");
    fields.get("Company").setValue("iText's next customer");
    fields.get("Country").setValue("No Man's Land");
    form.flattenFields();

    Table table = new Table(UnitValue.createPercentArray(new float[]{1, 15}));
    table.addHeaderCell("#");
    table.addHeaderCell("description");
    for (int i = 1; i <= 150; i++) {
        table.addCell(String.valueOf(i));
        table.addCell("test " + i);
    }

    doc.setRenderer(new DocumentRenderer(doc) {
        @Override
        protected LayoutArea updateCurrentArea(LayoutResult overflowResult) {
            LayoutArea area = super.updateCurrentArea(overflowResult);
            if (area.getPageNumber() == 1) {
                area.getBBox().decreaseHeight(266);
            }
            return area;
        }
    });

    doc.add(table);

    doc.close();

最有趣的部分可能是关于扩展DocumentRenderer.此类的实例与文档相关联,可处理其布局和覆盖的方法(updateCurrentArea),顾名思义,该方法用于更新布局区域.

Probably the most interesting part is about extending DocumentRenderer. The instance of this class associated with document handles its layout and overrided method (updateCurrentArea), as the name stands for, updates area for layout.

重要的是要提到的:所有iText5 SO答案都已移植到iText7中,您可以在iText网站上找到它们:

What is important to mention: All iText5 SO answers are ported in iText7 and you can find them on iText's website: https://developers.itextpdf.com/content/itext-7-examples .

这篇关于使用iText7(Java)将表添加到现有PDF并在其他页面上继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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