3

尝试使用 pdfbox 创建带有可由用户或计算机填写的表单字段的 pdf。到目前为止,我的代码如下所示:

PDDocument doc = new PDDDocument();
PDPage page = new PDPage();
doc.addPage(page)
PDAcroForm = new PDAcroForm(doc)
doc.documentCatalog.setAcroForm(acroForm)
COSDictionary cosDict = new COSDictionary()
PDTextbox textField = new PDTextbox(acroForm, cosDict)
PDRectangle rect = new PDRectangle()
rect.setLowerLeftX((float) 250)
rect.setLowerLeftY((float) 125)
rect.setUpperRightX((float) 500)
rect.setUpperRightY((float) 75)
textField.getWidget().setRectangle(rect)
acroForm.getFields.add(textField)
page.getAnnotations().add(textField)
page.getAnnotations().add(textField.getWidget())
4

1 回答 1

7

我遇到了几乎同样的问题。我尝试将字段添加到包含表单的现有文档中。我想出了以下解决方案:

PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDAcroForm acroForm = new PDAcroForm(doc);
doc.getDocumentCatalog().setAcroForm(acroForm);
COSDictionary cosDict = new COSDictionary();

COSArray rect = new COSArray();
rect.add(new COSFloat(250f)); // lower x boundary
rect.add(new COSFloat(75f)); // lower y boundary
rect.add(new COSFloat(500f)); // upper x boundary
rect.add(new COSFloat(125f)); // upper y boundary

cosDict.setItem(COSName.RECT, rect);
cosDict.setItem(COSName.FT, COSName.getPDFName("Tx")); // Field Type
cosDict.setItem(COSName.TYPE, COSName.ANNOT);
cosDict.setItem(COSName.SUBTYPE, COSName.getPDFName("Widget"));
cosDict.setItem(COSName.T, new COSString("yourFieldName"));

PDTextbox textField = new PDTextbox(acroForm, cosDict);

acroForm.getFields().add(textField);
page.getAnnotations().add(textField.getWidget());

我认为问题在于小部件没有写入文本字段的字典。

于 2013-11-26T21:37:54.687 回答