iText 为 PDF 封面中的占位符动态添加值

iText Add values to placeholders in PDF cover page dynamically

我有以下封面。我需要为 Licenseedate 添加值,并用给定的另一个文本填写方框。如何使用 iText 完成此操作。感谢任何帮助。

首先,您需要创建一个表单,作为所有许可文件的模板。 Chapter 6 of "iText in Action" (a chapter that can be downloaded for free) explains how to create such a form using OpenOffice, but there are many alternative ways to do this. I created such a form using Adobe Acrobat: CertificateOfExcellence.pdf

的第 5.3.5 节

我突出显示了这些字段,以便您可以看到我添加它们的位置。有四个:

  • 课程: 14pt Helvetica 中的课程名称,居中对齐
  • 姓名:Helvetica学生姓名,左对齐
  • 日期: Helvetica 课程日期,左对齐
  • 课程说明: Helvetica 学生姓名,左对齐 multi-line 标志。

现在我可以轻松填写​​表格了(参见FillForm):

public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    AcroFields form = stamper.getAcroFields();
    form.setField("course", "Copying and Pasting from Whosebug");
    form.setField("name", "Some dude on Whosebug");
    form.setField("date", "April 10, 2016");
    form.setField("description",
        "In this course, people consistently ignore the existing documentation completely. "
        + "They are encouraged to do no effort whatsoever, but instead post their questions "
        + "on Whosebug. It would be a mistake to refer to people completing this course "
        + "as developers. A better designation for them would be copy/paste artist. "
        + "Only in very rare cases do these people know what they are actually doing. "
        + "Not a single student has ever learned anything substantial during this course.");
    stamper.setFormFlattening(true);
    stamper.close();
}

生成的 PDF 如下所示:certificate.pdf

如您所见,代码非常简单。对于那些花时间阅读文档的人来说,所有这些都有很好的记录。也许你也可以看看标题为 Interactive forms on the official web site.

的部分