[英]How to set field name in pdf- java
I want to set a field name in a pdf (not in existing pdf) so that I can get the coordinates of that field when required.我想在 pdf 中设置一个字段名称(不在现有的 pdf 中),以便我可以在需要时获取该字段的坐标。 Can we achieve this without pdfstamper?我们可以在没有 pdfstamper 的情况下实现这一目标吗?
Thanks in advance提前致谢
You say you want to create a PDF from scratch (not an existing PDF) and you want this PDF to have a field.你说你想从头开始创建一个 PDF(不是现有的 PDF),并且你希望这个 PDF 有一个字段。
Creating a PDF from scratch doesn't involve PdfStamper
, so the answer to the question "Can we achieve this without PdfStamper
" is "Yes, you can."从头开始创建 PDF 不涉及PdfStamper
,因此“我们可以在没有PdfStamper
情况下实现这一PdfStamper
”问题的答案是“是的,你可以”。
If you are thinking about using iText 5, you should take a look at the following examples:如果您正在考虑使用 iText 5,您应该查看以下示例:
One of those examples was written in answer to the question Add PdfPCell to Paragraph其中一个示例是为了回答将PdfPCell 添加到段落这一问题而编写的
In this example, we create a Paragraph
in which some Chunk
objects are fields:在这个例子中,我们创建了一个Paragraph
,其中一些Chunk
对象是字段:
You can get the coordinates of those fields using the getFieldPositions()
method.您可以使用getFieldPositions()
方法获取这些字段的坐标。 That is explained in the FAQ: How to find the absolute position and dimension of a field?常见问题解答中对此进行了解释: 如何找到字段的绝对位置和维度?
If you are thinking of using iText 7.0.1, you will discover that the classes are much easier to understand because the same classes are used regardless whether you are creating a form from scratch or filling out an existing form, see chapter 4 of the iText 7 jump-start tutorial .如果您正在考虑使用 iText 7.0.1,您会发现这些类更容易理解,因为无论您是从头开始创建表单还是填写现有表单,都使用相同的类,请参阅iText 的第 4 章7 快速入门教程。
public class GenericFields extends GenericTest {
public static final String DEST = "./target/test/resources/sandbox/events/generic_fields.pdf";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new GenericFields().manipulatePdf(DEST);
}
@Override
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
Paragraph p = new Paragraph();
p.add("The Effective Date is ");
Text day = new Text(" ");
day.setNextRenderer(new FieldTextRenderer(day, "day"));
p.add(day);
p.add(" day of ");
Text month = new Text(" ");
month.setNextRenderer(new FieldTextRenderer(month, "month"));
p.add(month);
p.add(", ");
Text year = new Text(" ");
year.setNextRenderer(new FieldTextRenderer(year, "year"));
p.add(year);
p.add(" that this will begin.");
doc.add(p);
doc.close();
}
protected class FieldTextRenderer extends TextRenderer {
protected String fieldName;
public FieldTextRenderer(Text textElement, String fieldName) {
super(textElement);
this.fieldName = fieldName;
}
@Override
public void draw(DrawContext drawContext) {
PdfTextFormField field = PdfTextFormField.createText(drawContext.getDocument(), getOccupiedAreaBBox(), fieldName);
PdfAcroForm.getAcroForm(drawContext.getDocument(), true).addField(field);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.