Fill pdf form using itext in java having problems

9.8k views Asked by At

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."

Also the output pdf have the changes i made but it does not have features of original pdf document, as the above message shows.

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

3

There are 3 answers

11
Jasper Duizendstra On BEST ANSWER

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.

1
Not Important On

as far as i know, the stamp.setFormFlattening(true); you are calling, remove fields and replace that with content, that's why your new document doesn't preserve fields

0
user2853941 On

if you want a Java code, here's a snippet I got from one a blog. But it uses pdf Nitro, in case this helps or let me know

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