尝试使用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())
答案 0 :(得分:7)
我遇到了几乎相同的问题。我尝试将Fields添加到包含表单的现有文档中。我提出了以下解决方案:
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());
我认为问题是小部件不会写入textField的字典。