简体   繁体   English

在Java中使用itext填写pdf表格有问题

[英]Fill pdf form using itext in java having problems

I am using iText to fill pdf form, no error in console, but when i am opening the output pdf it is giving "Expected a dict object" and after it gives another message saying "This document enabled extended features in Adobe reader. The document has been changed since it was created and use of extended features is no longer available." 我正在使用iText填写pdf表单,但在控制台中没有错误,但是当我打开输出pdf时,它给出了“ Expected dict object”,并给出了另一条消息,指出“此文档已启用Adobe Reader中的扩展功能。该文档自创建以来已更改,不再使用扩展功能。”

Also the output pdf have the changes i made but it does not have features of original pdf document, as the above message shows. 输出的pdf也有我所做的更改,但不具有原始pdf文档的功能,如上述消息所示。

I have used below code... 我用下面的代码...

PdfReader reader = new PdfReader("C:\\Users\\asfs\\Downloads\\1013-Form22.pdf");
String name = "C:\\Users\\asfs\\Downloads\\Temp.pdf";

PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(name));
AcroFields form = stamp.getAcroFields();

String last = "Form22_Dtls[0].Page1[0].Country_C[0]";

form.setField(last, "Sample Country name");

HashMap map = new HashMap();
map = form.getFields();
Iterator iterator = map.keySet().iterator();
while(iterator.hasNext())
    System.out.println("Field is >>>"+iterator.next());
// close pdf stamper
stamp.setFormFlattening(true);
stamp.close();

Please help me. 请帮我。

Thanks 谢谢

Try: 尝试:

// open the pdf stamper in edit mode
stamp = new PdfStamper(reader, new FileOutputStream(name), '\0', true);

The PdfStamper needs to be in append mode. PdfStamper必须处于附加模式。

if you want a Java code, here's a snippet I got from one a blog. 如果您想要Java代码,这是我从一个博客获得的摘录。 But it uses pdf Nitro, in case this helps or let me know 但是它使用pdf Nitro,以防万一,请告诉我

private ByteArrayOutputStream editPdfDocument() throws Exception {
PdfReader reader = null;
PdfStamper stamper = null;
ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
HashMap fieldsWithValues = new HashMap();
int user_id = 1234; // unique key for the object
String dirPath = “D:/abc”;// directory path
String fileName = “def.pdf”;// name of the file
try {
reader = new PdfReader(dirPath + “/” + fileName);
stamper = new PdfStamper(reader, baosPDF);
AcroFields form = stamper.getAcroFields();
HashMap fields = form.getFields();
Set keys = fields.keySet();   // keys represents the names of all the form fields
fieldsWithValues = fetchFieldValuesForObject(user_id, keys);
// fetchFieldValuesForObject(user_id, keys) method will fetch the values of the fields from Database for object identified by user_id
Iterator itr = keys.iterator();
while (itr.hasNext()) {
String fieldName = (String) itr.next();
String fieldValue = fieldsWithValues.get(fieldName) != null ? (String)(fieldsWithValues.get(fieldName)) : “”;
form.setField(fieldName, fieldValue);
}
stamper.setFormFlattening(true);
} catch (Exception dex) {
if (stamper != null)
stamper.close();
if (reader != null)
reader.close();
throw dex;
}
return baosPDF;
}`enter code here`

It's Pdf form filling using Java that directly retrieves data from database, hope it works Visit Java Codes for Automatic form fill 这是使用Java的Pdf表单填充,可直接从数据库中检索数据,希望它能正常工作。 访问Java代码以进行自动表单填充

as far as i know, the stamp.setFormFlattening(true); 据我所知,stamp.setFormFlattening(true); you are calling, remove fields and replace that with content, that's why your new document doesn't preserve fields 您正在打电话,删除字段并用内容替换,这就是为什么您的新文档不保留字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM