[ 
https://issues.apache.org/jira/browse/PDFBOX-6179?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18067217#comment-18067217
 ] 

Tilman Hausherr commented on PDFBOX-6179:
-----------------------------------------

I didn't read the last comment this morning and so while writing the following 
code I was surprised that PDAnnotation has an equals method. Anyway, the 
following code would work on your file:
{code:java}

public class RemoveField
{

    public static void main(String[] args) throws IOException
    {
        String name = "XXXX";
        String dir = .......

        try (PDDocument doc = Loader.loadPDF(new File(dir, 
"PDFBOX-6179-form.pdf")))
        {
            PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();
            PDField field = acroForm.getField(name);
            if (field == null)
            {
                System.out.println("field not found");
                doc.close();
                return;
            }
            List<PDField> fields = acroForm.getFields(); // not the best method 
because it returns only top level
            boolean removed = false;
            for (int i = 0; i < fields.size(); ++i)
            {
                if (fields.get(i).getCOSObject().equals(field.getCOSObject()))
                {
                    fields.remove(i);
                    removed = true;
                    break;
                }
            }
            System.out.println("field removed? " + removed);
            if (removed)
            {
                // find page(s)
                List<PDAnnotationWidget> widgets = field.getWidgets();
                for (PDAnnotationWidget widget : widgets)
                {
                    PDPage page = widget.getPage(); // not always set
                    List<PDAnnotation> annotations = page.getAnnotations();
                    removed = annotations.remove(widget);
                    System.out.println("page widget removed? " + removed);
                }
            }
            doc.save(new File(dir, "PDFBOX-6179-form-saved.pdf"));
        }
    }
}
{code}
A generally working code would need to call getFieldTree() and check on the 
kids. However what is the use case on removing a single field?

> Removing fields not possible
> ----------------------------
>
>                 Key: PDFBOX-6179
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-6179
>             Project: PDFBox
>          Issue Type: Bug
>          Components: AcroForm
>    Affects Versions: 3.0.7 PDFBox
>            Reporter: Stefan Ziegler
>            Priority: Major
>         Attachments: simple_form.pdf
>
>
> Seems that fields can no longer be removed like this:
> PDField field = acroForm.getField("XXXXX");
> List<PDField> fields = acroForm.getFields();
> fields.remove(field);
> Internally, the field is never found. PDField has no equals method. getField 
> and getFields create new PDField instances and so equals fails in this case.
> Shouldn't there be an equals method in PDField, that compares the underlying 
> COSObject?



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to