Java Itext 7,can';不要使用引用相同表单值的两个小部件
我正在尝试创建一个pdf,其中多个小部件引用相同的表单值,这样当一个小部件被填充时,所有其他小部件都显示相同的值。当它们引用不同的表单字段时,我可以添加多个小部件,但是当我将它们更改为引用同一表单字段时,只显示一个小部件。以下是我的示例代码:Java Itext 7,can';不要使用引用相同表单值的两个小部件,java,pdf,pdf-generation,itext7,Java,Pdf,Pdf Generation,Itext7,我正在尝试创建一个pdf,其中多个小部件引用相同的表单值,这样当一个小部件被填充时,所有其他小部件都显示相同的值。当它们引用不同的表单字段时,我可以添加多个小部件,但是当我将它们更改为引用同一表单字段时,只显示一个小部件。以下是我的示例代码: public class HelloWorld { public static final String DEST = "sampleOutput.pdf"; public static void main(String args[])
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;
}
}
根据我对您问题的理解,您希望在PDF文件中添加一个字段,例如,
name
,并且希望在每个页面中添加此字段的小部件注释
我对您的代码进行了如下修改:
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;
}
这是在PDF文件中创建不同组件的一种非常简洁的方法。我理解您的问题的方式是,您希望在PDF文件中添加一个字段,例如,
name
,并且希望在每个页面中添加此字段的小部件注释
我对您的代码进行了如下修改:
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;
}
这是一种在PDF文件中创建不同组件的非常干净的方法。这非常好。使用两个PdfTextFormFields引用同一个字段似乎是问题所在?我这样做只是因为我们可能希望在第一页的不同位置显示小部件。你知道怎么做吗?我会更新我的答案,这样当你需要在同一个页面上添加两个相同的字段时,它也能工作。这很好。使用两个PdfTextFormFields引用同一个字段似乎是问题所在?我这样做只是因为我们可能希望在第一页的不同位置显示小部件。你知道怎么做吗?我会更新我的答案,这样当你需要在同一个页面上添加两个相同的字段时,它也能工作。