You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Kevin Ternes <KT...@thegeneral.com> on 2016/04/25 23:49:37 UTC

Completely Remove a Field

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.

Any is help appreciated.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


RE: Completely Remove a Field

Posted by Kevin Ternes <KT...@thegeneral.com>.
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:THausherr@t-online.de] 
Sent: Tuesday, April 26, 2016 12:02 PM
To: users@pdfbox.apache.org
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: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Completely Remove a Field

Posted by Tilman Hausherr <TH...@t-online.de>.
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: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


RE: Completely Remove a Field

Posted by Kevin Ternes <KT...@thegeneral.com>.
The code below works as far as it goes.
Until we call PDAcroForm.flatten(), the removed PDField is still accessible.  

So the correct code is:

    COSDictionary formDict = pdAcroForm.getCOSObject();
    COSArray formFieldArray = (COSArray) formDict.getDictionaryObject(COSName.FIELDS);
    formFieldArray.removeObject(pdField.getCOSObject());
      //  other operations . . .
   pdAcroForm.flatten(); 


-----Original Message-----
From: Kevin Ternes 
Sent: Monday, April 25, 2016 4:50 PM
To: users@pdfbox.apache.org
Subject: Completely Remove a Field

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.

Any is help appreciated.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org