iText动态地将值添加到PDF封面中的占位符 [英] iText Add values to placeholders in PDF cover page dynamically

查看:721
本文介绍了iText动态地将值添加到PDF封面中的占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

首先,您需要创建一个表格,将作为所有许可证文件的模板。



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




  • 当然: 14pt Helvetica的课程名称,中心对齐

  • 姓名: Helvetica学生姓名,左对齐

  • 日期:日期Helvetica的课程,左对齐

  • 课程描述: Helvetica学生的姓名,左边是多行标志。



现在我可以轻松填写​​表格(参见



如您所见,代码非常简单。对于那些花时间阅读文档的人来说,所有这些都有很好的文档记录。也许您还可以查看交互式表单的部分。 href =http://developers.itextpdf.com/ =noreferrer>官方网站。


I have the following cover page. I need to add values for Licensee, date and fill in the box with another text given. How can this be done using iText. Any help is appreciated.

解决方案

First you need to create a form that will act as a template for all your license document. Section 5.3.5 of 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

I highlighted the fields so that you can see where I've added them. There are four:

  • course: the name of the course in 14pt Helvetica, with centered alignment
  • name: the name of the student in Helvetica, left alignment
  • date: the date of the course in Helvetica, left alignment
  • description of the course: the name of the student in Helvetica, left alignment with the multi-line flag on.

Now I can easily fill out the form (see 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 StackOverflow");
    form.setField("name", "Some dude on StackOverflow");
    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 StackOverflow. 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();
}

The resulting PDF looks like this: certificate.pdf

As you can see, the code is very simple. All of this is very well documented for those who take the time to read the documentation. Maybe you can also take a look at the section entitled Interactive forms on the official web site.

这篇关于iText动态地将值添加到PDF封面中的占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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