加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Re:pdfbox库;有没有人发现如何将文本字段添加到PDF?

发布时间:2020-12-15 04:11:55 所属栏目:Java 来源:网络整理
导读:尝试使用pdfbox创建带有可由用户或计算机填写的表单字段的pdf. 到目前为止我的代码看起来像这样: PDDocument doc = new PDDDocument();PDPage page = new PDPage();doc.addPage(page)PDAcroForm = new PDAcroForm(doc)doc.documentCatalog.setAcroForm(acro
尝试使用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())

解决方法

我有几乎相同的问题.我尝试将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的字典.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读