Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 226
Ravi Sharma Avatar asked Jan 21 '26 11:01

Ravi Sharma


1 Answers

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.

like image 182
Jasper Duizendstra Avatar answered Jan 23 '26 00:01

Jasper Duizendstra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!