简体   繁体   English

使用iText7(Java)将表添加到现有PDF并在其他页面上继续

[英]Using iText7 (Java) to add a table to an existing PDF and continue on additional pages

I am attempting to complete a project with almost identical requirements as those associated with this question asked in 2015. 我正在尝试完成一个几乎与2015年提出的与此问题相关的要求完全相同的项目。

The answer provided by Bruno was perfect, but related to iText5. Bruno提供的答案是完美的,但与iText5有关。 I am relatively new to iText, and am desperately trying to get up-to-speed to complete a current project. 我对iText来说还比较陌生,并且极力地尝试着加快完成当前项目的速度。

  • I need to populate the fields of a PDF document 我需要填充PDF文档的字段
  • I need to add a table below the populated section, and the table needs to span multiple pages thereafter 我需要在填充部分下方添加一个表格,此后该表格需要跨越多个页面

Can anyone assist with the translation of Bruno's answer from iText5 to iText7? 有人可以协助将Bruno的答案从iText5转换为iText7吗?

Thanks so much in advance for any/all assistance! 提前非常感谢您提供的任何/所有帮助!

You should write something like that: 您应该这样写:

    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();

Probably the most interesting part is about extending DocumentRenderer. 可能最有趣的部分是关于扩展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. 此类的实例(与文档关联)处理其布局和覆盖的方法(updateCurrentArea),顾名思义,该实例用于布局的更新区域。

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 . 值得一提的是: 所有iText5 SO答案都已移植到iText7中,您可以在iText网站上找到它们https : //developers.itextpdf.com/content/itext-7-examples

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用iText7在现有PDF的特定页面中添加表格 - Using iText7 to add a table in a specific page of an existing PDF 如何使用iText7将SVG添加到PDF - How to add an SVG to a PDF using iText7 使用iText7和Java生成PDF - PDF Generation using iText7 with Java itext7 Java为现有pdf中的标题创建PdfExplicitDestination - itext7 Java Create PdfExplicitDestination for titles in existing pdf 如何使用 itext7 Java 将多个图像添加到 PDF? - How do you add multiple images to a PDF with itext7 Java? 如何将签名表单添加到现有的pdf(使用iText7),以便输出文件可以用作pdf(顺序签名)的输入? - How to add signature form to an existing pdf (using iText7), so that the output file can be served as a input to pdf (sequential signature)? 我尝试使用 itext7 生成带有 eclipse.but 的 pdf 文件。但是当我在上面添加页面时,它会输出错误“java.lang.NullPointerException” - I try to use itext7 to produce a pdf file with eclipse.but when I add the pages on it ,it will output a error"java.lang.NullPointerException" 使用 JAVA 和 Itext7 将 Html 转换为 PDF 时获取 NullPointerException - Getting NullPointerException while converting Html to PDF using JAVA and Itext7 iText Java-将标头添加到现有的pdf - iText Java - add header to an existing pdf 使用iText7验证/添加PDF签名 - Verify/Add PDF Signatures with iText7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM