Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strike>Is this an AcroForm form or a LiveCycle Designer (xfa) form? </p> <p>If it's XFA (which is likely given the field names), iText can't help you. It can only get/set field values when working with XFA.</strike></p> <p>Okay, an AcroForm. Rather than go the route used in your source, I suggest you directly manipulate the existing field dictionaries and the acroForm field list.</p> <p>I'm a Java native when it comes to iText, so you'll have to do some translation, but here goes:</p> <p>A) Delete the AcroForm's field array. Leave the calculation order alone if present (/CO). I think.</p> <pre><code>PdfDictionary acroDict = reader.getCatalog().getAsDictionary(PdfName.ACROFORM); acroDict.remove(PdfName.FIELDS); </code></pre> <p>B) Attach all the 'top level' fields to a new FIELDS array.</p> <pre><code>PdfArray newFldArray = new PdfArray(); acroDict.put(newFldArray, PdfName.FIELDS); // you could wipe this between pages to speed things up a bit Set&lt;PdfIndirectReference&gt; radioFieldsAdded = new HashSet&lt;PdfIndirectReference&gt;(); int numPages = reader.getNumberOfPages(); for (int curPg = 1; curPg &lt;= numPages; ++curPg) { PdfDictionary curPageDict = reader.getPageN(curPg); PdfArray annotArray = curPageDict.getAsArray(PdfName.ANNOTS); if (annotArray == null) continue; for (int annotIdx = 0; annotIdx &lt; annotArray.size(); ++annotIdx) { PdfIndirectReference fieldReference = (PdfIndirectReference) annotArray.getAsIndirect(annotIdx); PdfDictionary field = (PdfDictionary)PdfReader.getObject(fieldReference); // if it's a radio button if ((PdfFormField.FF_RADIO &amp; field.getAsNumber(PdfName.FF).intValue()) != 0) { fieldReference = field.get(pdfName.PARENT); field = field.getAsDict(PdfName.PARENT); // looks up indirect reference for you. // only add each radio field once. if (radioFieldsAdded.contains(fieldReference)) { continue; } else { radioFieldsAdded.add(fieldReference); } } field.remove(PdfName.PARENT); // you'll need to assemble the original field name manually and replace the bits // you don't like. Parent.T + '.' child.T + '.' + ... String newFieldName = SomeFunction(field); field.put(PdfName.T, new PdfString( newFieldName ) ); // add the reference, not the dictionary newFldArray.add(fieldReference) } } </code></pre> <p>C) Clean up</p> <pre><code>reader.removeUnusedObjects(); </code></pre> <p>Disadvantage:<br> More Work.</p> <p>Advantages:<br> Maintains all field types, attributes, appearances, and doesn't change the file as a whole all that much. Less CPU &amp; memory.</p> <p>Your existing code ignores field script, all the field flags (read only, hidden, required, multiline text, etc), lists/combos, radio buttons, and quite a few other odds and ends.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload