我想使用iText将共享相同fieldName的文本字段添加到文档中。目的是能够设置一个值,并且值应出现在所有页面上(自动填充)。
我的问题是,使用以下代码时,只有第一个TextField出现在Adobe Reader中。研究之后看起来我必须使用“一个词典条目”和多个视觉表示“小部件”,但我不知道如何用iText实现它。
有什么建议吗? 谢谢 齐格弗里德
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.TextField;
public class ITextAddTextfieldDemo {
private static final String ORIGINAL = "original.pdf";
private static final String RESULT = "original_textfield.pdf";
public static void main(String[] args) throws IOException,
DocumentException {
ITextAddTextfieldDemo.createPdf(ORIGINAL, 5);
ITextAddTextfieldDemo.stamp(ORIGINAL, RESULT);
System.out.println("done");
}
public static void stamp(String src, String dest) throws IOException,
DocumentException {
PdfReader reader = new PdfReader(new File(src).getAbsolutePath());
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
int pages = reader.getNumberOfPages();
// test problem by switching true/false
boolean uniqueName = false;
String fieldName = "demoField";
if (uniqueName) {
TextField textField = new TextField(stamper.getWriter(),
new Rectangle(560, 600, 590, 800), fieldName);
textField.setText(""); // add text to see misrendered output
textField.setOptions(TextField.MULTILINE | TextField.DO_NOT_SCROLL);
textField.setRotation(90);
for (int page = 1; page <= pages; page++) {
stamper.addAnnotation(textField.getTextField(), page);
}
} else {
TextField[] textFields = new TextField[pages];
for (int page = 1; page <= pages; page++) {
int idx = page - 1;
fieldName += "_" + String.valueOf(page);
textFields[idx] = new TextField(stamper.getWriter(),
new Rectangle(560, 600, 590, 800), fieldName);
textFields[idx].setText(""); // add text to see misrendered output
textFields[idx].setOptions(TextField.MULTILINE
| TextField.DO_NOT_SCROLL);
textFields[idx].setRotation(90);
stamper.addAnnotation(textFields[idx].getTextField(), page);
}
}
stamper.close();
reader.close();
}
public static void createPdf(String filename, int pages)
throws IOException, DocumentException {
Document document = new Document(PageSize.A4);
PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();
for (int i = 0; i < pages; i++) {
Paragraph p = new Paragraph("Hello page " + String.valueOf(i + 1));
document.add(p);
document.newPage();
}
document.close();
}
}
@EDIT:我找到了一个解决方案,但我还有一个问题。这是建议的方式,为什么我必须在每页上标记父母? (仅在第一页上标记它不起作用)。
if (uniqueName) {
PdfFormField parent = PdfFormField.createTextField(
stamper.getWriter(), true, false, 0);
parent.setFieldName(fieldName);
parent.setUserName("Please enter the common demo data.");
for (int page = 1; page <= pages; page++) {
TextField textField = new TextField(stamper.getWriter(),
new Rectangle(560, 600, 590, 800), null);
textField.setOptions(TextField.MULTILINE
| TextField.DO_NOT_SCROLL);
textField.setRotation(90);
PdfFormField pff = textField.getTextField();
parent.addKid(pff);
stamper.addAnnotation(pff, page);
// why is it necessary to add it on every page?
stamper.addAnnotation(parent, page);
}
} else {
答案 0 :(得分:2)
您不必将父级添加到每个页面。您可以使用PdfAnnotation.setPlaceInPage(pagenumber)为每个窗口小部件设置页面,并添加父窗口一次。我已经调整了您在问题中添加的解决方案:
if (uniqueName) {
PdfFormField parent = PdfFormField.createTextField(
stamper.getWriter(), true, false, 0);
parent.setFieldName(fieldName);
parent.setUserName("Please enter the common demo data.");
for (int page = 1; page <= pages; page++) {
TextField textField = new TextField(stamper.getWriter(),
new Rectangle(560, 600, 590, 800), null);
textField.setOptions(TextField.MULTILINE
| TextField.DO_NOT_SCROLL);
textField.setRotation(90);
PdfFormField pff = textField.getTextField();
parent.addKid(pff);
// add widget to each page
pff.setPlaceInPage(page);
}
// add parent
stamper.addAnnotation(parent, 1);
} else {