1

I am upgrading com.lowagie:itext:2.1.7 libraries to com.itextpdf libraries.

It is decided not to use com.itextpdf version 5 series (Example, com.itextpdf:itextpdf:5.5.13.2)as it has reached EOL(End Of Life). So the latest iText version 7.x is used.

Since it is not possible to use com.itextpdf.text.* packages anymore as this package is not present in latest library(beyond iText 5), we are using Acroform from iText7 to handle PDF forms.

Now we are facing issues while handling forms. (using Acroform in iText 7).

The issue is that, we have a set of radio buttons in the template(source) PDF.

  1. We read the source PDF using Acroform
  2. Update the radio button value using formField.setValue(value);
  3. Save the PDF to destination file.
  4. Open the destination PDF in Adobe Acrobat Reader
  5. It is seen that the radio button value is not set.
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfButtonFormField;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
...
ByteArrayOutputStream out = new ByteArrayOutputStream();
PdfWriter writer = new PdfWriter(out);
PdfDocument pdfDocument = new PdfDocument(new PdfReader(new ByteArrayInputStream(byteArrPdfContent)), writer);
PdfAcroForm destForm = PdfAcroForm.getAcroForm(pdfDocument, true);

Map<String, PdfFormField> formFields = destForm.getFormFields();
PdfFormField destForm.getField(currentFieldName);
formField.setValue(value);

Surprisingly, this was working fine with com.lowagie:itext:2.1.7 (with com.itextpdf.text.pdf.AcroFields;)

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Rcee
  • 11
  • 1
  • What kind of value do you set? For Radiobuttons not every value works...And why are you surprised? It was not a bad version. The successor of the version is openPDF. If you post your PDF it is easier to help you... – Lonzak May 21 '21 at 12:24
  • the code looks right (depending on what you're using as a "value"). Are you using something that is available on the getAppearanceStates() [from the PdfFormField]? – André Lemos May 21 '21 at 14:43
  • 1
    I use some text values. I can see all the 3 expected values in the getAppearanceStates(), but a few more values 0,1,2, and Off as well. Code used is [link](https://kb.itextpdf.com/home/it7kb/faq/how-to-get-specific-types-from-acrofields-like-pushbuttonfield-radiocheckfield-etc). When I use 0,1 or 2 the pdf is reflecting the value, but I am supposed to use the text values. – Rcee May 22 '21 at 17:46
  • @Rcee any chance you can share your PDF file, or a simplified version of it? – André Lemos May 25 '21 at 06:53
  • How to share the PDF? I could not find a way to upload it here..! – Rcee May 26 '21 at 19:42
  • @Rcee you can use https://www.dropbox.com or any other file hosting service – Alexey Subach May 31 '21 at 21:04

0 Answers0