Itext 7,不能使用两个引用相同表单值的小部件
Itext 7, can't use two widgets referencing same form value
我想要一个 pdf,其中多个小部件引用相同的表单值,这样当一个小部件被填充时,所有其他小部件都显示相同的值。当它们引用不同的表单域时,我可以添加多个小部件,但是当我将它们更改为引用相同的表单域时,只有一个小部件显示。这是我的示例代码:
public class HelloWorld {
public static final String DEST = "sampleOutput.pdf";
public static void main(String args[]) throws IOException {
File file = new File(DEST);
new HelloWorld().createPdf(DEST);
}
public void createPdf(String dest) throws IOException {
//Initialize PDF writer
PdfWriter writer = new PdfWriter(dest);
//Initialize PDF document
PdfDocument pdf = new PdfDocument(writer);
// Initialize document
Document document = new Document(pdf);
HelloWorld.addAcroForm(pdf, document);
//Close document
document.close();
}
public static PdfAcroForm addAcroForm(PdfDocument pdf, Document doc) throws IOException {
Paragraph title = new Paragraph("Test Modify")
.setTextAlignment(TextAlignment.CENTER)
.setFontSize(16);
doc.add(title);
doc.add(new Paragraph("Confirmation:").setFontSize(12));
//Add acroform
PdfAcroForm form = PdfAcroForm.getAcroForm(doc.getPdfDocument(), true);
//Create text field
PdfTextFormField nameField = PdfFormField.createText(doc.getPdfDocument(),
new Rectangle(99, 525, 425, 15), "name", "");
PdfTextFormField nameField2 = PdfFormField.createText(doc.getPdfDocument(),
new Rectangle(99, 425, 425, 15), "name2", "");//"name2" works, if I use "name" only nameField is shown (not nameField2)
form.addField(nameField, pdf.getFirstPage());
form.addField(nameField2, pdf.getFirstPage());
return form;
}
}
根据我对你问题的理解,你想添加一个字段,例如name
,到 PDF 文件,并且您希望将此字段的小部件注释添加到每个页面。
我已经将你的代码改编成这样:
public static PdfAcroForm addAcroForm(PdfDocument pdf, Document doc) throws IOException {
Paragraph title = new Paragraph("Test Form")
.setTextAlignment(TextAlignment.CENTER)
.setFontSize(16);
doc.add(title);
doc.add(new Paragraph("Full name:").setFontSize(12));
PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
PdfTextFormField nameField = PdfFormField.createText(pdf,
new Rectangle(99, 525, 425, 15), "name", "");
for (int i = 1; i < 8; i++) {
form.addField(nameField, pdf.getPage(i));
}
return form;
}
现在您有一个字段,名为 name
,已添加到每一页。如果您在一页上更改字段的值,则它会在每一页上更改:
更新:
作为替代方案,您可以创建 PdfTextFormField
而无需先定义 Rectangle
。您可以在页面中添加不同的小部件注释,然后将这些小部件注释作为孩子添加到字段中:
public static PdfAcroForm addAcroForm(PdfDocument pdf, Document doc) throws IOException {
PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
PdfTextFormField nameField = PdfFormField.createText(pdf);
nameField.setFieldName("name");
PdfWidgetAnnotation widget1 = new PdfWidgetAnnotation(new Rectangle(99, 525, 425, 15));
pdf.getPage(1).addAnnotation(widget1);
PdfWidgetAnnotation widget2 = new PdfWidgetAnnotation(new Rectangle(99, 425, 425, 15));
pdf.getPage(1).addAnnotation(widget2);
form.addField(nameField, pdf.getPage(1));
nameField.addKid(widget1);
nameField.addKid(widget2);
return form;
}
这是一种在 PDF 文件中创建不同组件的非常简洁的方法。
我想要一个 pdf,其中多个小部件引用相同的表单值,这样当一个小部件被填充时,所有其他小部件都显示相同的值。当它们引用不同的表单域时,我可以添加多个小部件,但是当我将它们更改为引用相同的表单域时,只有一个小部件显示。这是我的示例代码:
public class HelloWorld {
public static final String DEST = "sampleOutput.pdf";
public static void main(String args[]) throws IOException {
File file = new File(DEST);
new HelloWorld().createPdf(DEST);
}
public void createPdf(String dest) throws IOException {
//Initialize PDF writer
PdfWriter writer = new PdfWriter(dest);
//Initialize PDF document
PdfDocument pdf = new PdfDocument(writer);
// Initialize document
Document document = new Document(pdf);
HelloWorld.addAcroForm(pdf, document);
//Close document
document.close();
}
public static PdfAcroForm addAcroForm(PdfDocument pdf, Document doc) throws IOException {
Paragraph title = new Paragraph("Test Modify")
.setTextAlignment(TextAlignment.CENTER)
.setFontSize(16);
doc.add(title);
doc.add(new Paragraph("Confirmation:").setFontSize(12));
//Add acroform
PdfAcroForm form = PdfAcroForm.getAcroForm(doc.getPdfDocument(), true);
//Create text field
PdfTextFormField nameField = PdfFormField.createText(doc.getPdfDocument(),
new Rectangle(99, 525, 425, 15), "name", "");
PdfTextFormField nameField2 = PdfFormField.createText(doc.getPdfDocument(),
new Rectangle(99, 425, 425, 15), "name2", "");//"name2" works, if I use "name" only nameField is shown (not nameField2)
form.addField(nameField, pdf.getFirstPage());
form.addField(nameField2, pdf.getFirstPage());
return form;
}
}
根据我对你问题的理解,你想添加一个字段,例如name
,到 PDF 文件,并且您希望将此字段的小部件注释添加到每个页面。
我已经将你的代码改编成这样:
public static PdfAcroForm addAcroForm(PdfDocument pdf, Document doc) throws IOException {
Paragraph title = new Paragraph("Test Form")
.setTextAlignment(TextAlignment.CENTER)
.setFontSize(16);
doc.add(title);
doc.add(new Paragraph("Full name:").setFontSize(12));
PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
PdfTextFormField nameField = PdfFormField.createText(pdf,
new Rectangle(99, 525, 425, 15), "name", "");
for (int i = 1; i < 8; i++) {
form.addField(nameField, pdf.getPage(i));
}
return form;
}
现在您有一个字段,名为 name
,已添加到每一页。如果您在一页上更改字段的值,则它会在每一页上更改:
更新:
作为替代方案,您可以创建 PdfTextFormField
而无需先定义 Rectangle
。您可以在页面中添加不同的小部件注释,然后将这些小部件注释作为孩子添加到字段中:
public static PdfAcroForm addAcroForm(PdfDocument pdf, Document doc) throws IOException {
PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
PdfTextFormField nameField = PdfFormField.createText(pdf);
nameField.setFieldName("name");
PdfWidgetAnnotation widget1 = new PdfWidgetAnnotation(new Rectangle(99, 525, 425, 15));
pdf.getPage(1).addAnnotation(widget1);
PdfWidgetAnnotation widget2 = new PdfWidgetAnnotation(new Rectangle(99, 425, 425, 15));
pdf.getPage(1).addAnnotation(widget2);
form.addField(nameField, pdf.getPage(1));
nameField.addKid(widget1);
nameField.addKid(widget2);
return form;
}
这是一种在 PDF 文件中创建不同组件的非常简洁的方法。