New Solution:
List<PDField> pdFieldList = pdAcroForm.getFields();
removeField(pdFieldList, fieldName);
// other . . .
pdAcroForm.flatten();
public static boolean removeField(List<PDField> pdFieldList, String
fieldName) {
boolean removed = false;
Iterator<PDField> it = pdFieldList.iterator();
while (it.hasNext() && !removed) {
PDField pdField = it.next();
if (pdField instanceof PDNonTerminalField) {
List<PDField> pdChildFieldList = ((PDNonTerminalField)
pdField).getChildren();
removed = removeField(pdChildFieldList, fieldName);
} else if (fieldName.equals(pdField.getPartialName())) {
pdFieldList.remove(pdField);
removed = true;
}
}
return removed;
}
-----Original Message-----
From: Tilman Hausherr [mailto:[email protected]]
Sent: Tuesday, April 26, 2016 12:02 PM
To: [email protected]
Subject: Re: Completely Remove a Field
Am 25.04.2016 um 23:49 schrieb Kevin Ternes:
> It seems like removing a PDField from a loaded PDDocument would be trivial.
> The goal is to remove the field from the form and anything related to that
> field from the displayed PDF.
>
> I have tried just removing the PDField from the List of
> PDAcroForm.getFields() which does not seem to work.
>
> And I have tried digging into the PDAcroForm's dictionary:
> PDField pdField = pdAcroForm.getField("boxAPPL");
> COSDictionary formDict = pdAcroForm.getCOSObject();
> COSArray fieldz = (COSArray)
> formDict.getDictionaryObject(COSName.FIELDS);
> fieldz.removeObject(pdField.getCOSObject());
>
> which does not appear to work either.
Have a look at PDAcroForm.getFields(). You'll have to work with that, and if
you don't find your field, call PDNonTerminalField.getChildren() on non
terminal fields. When you find it, remove your field and reassign the list.
To find out whether you got your field, check the name or compare not the
PDField object, but its getCOSObject() result.
Your solution in the recent mail might still work with a specific PDF, but I
doubt it will work for all PDFs.
Tilman
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]