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

Tilman Hausherr edited comment on PDFBOX-6179 at 3/21/26 5:37 AM:
------------------------------------------------------------------

Here's some code that assumes the equals is there (I added it locally, similar 
to PDAnnotation) and also does a recursion. I haven't tested it on a file with 
a multilevel real field tree, I'll do that when I have more time.
{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(); // returns only top 
level
            boolean removed = fields.remove(field);
            if (!removed)
            {
                removed = removeRecursive(fields, field);
            }
            if (removed)
            {
                // find page(s)
                List<PDAnnotationWidget> widgets = field.getWidgets();
                for (PDAnnotationWidget widget : widgets)
                {
                    PDPage page = widget.getPage(); // not always set - we may 
need to go through all pages
                    if (page != null)
                    {
                        List<PDAnnotation> annotations = page.getAnnotations();
                        removed = annotations.remove(widget);
                        System.out.println("page widget removed? " + removed);
                    }
                }
            }
            if (removed)
            {
                System.out.println("field removed? " + removed);
                doc.save(new File(dir, "PDFBOX-6179-form-saved.pdf"));
            }
        }
    }

    private static boolean removeRecursive(List<PDField> fields, PDField field)
    {
        // search the tree
        for (PDField fieldItem : fields)
        {
            if (fieldItem instanceof PDNonTerminalField)
            {
                PDNonTerminalField ntField = (PDNonTerminalField) fieldItem;
                List<PDField> children = ntField.getChildren();
                if (children.remove(field))
                {
                    ntField.setChildren(children);
                    return true;
                }
                if (removeRecursive(children, field))
                {
                    return true;                    
                }
            }
        }
        return false;
    }
}
{code}



was (Author: tilman):
Here's some code that assumes the equals is there (I added it locally, similar 
to PDAnnotations) and also does a recursion. I haven't tested it on a file with 
a multilevel real field tree, I'll do that when I have more time.
{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(); // returns only top 
level
            boolean removed = fields.remove(field);
            if (!removed)
            {
                removed = removeRecursive(fields, field);
            }
            if (removed)
            {
                // find page(s)
                List<PDAnnotationWidget> widgets = field.getWidgets();
                for (PDAnnotationWidget widget : widgets)
                {
                    PDPage page = widget.getPage(); // not always set - we may 
need to go through all pages
                    if (page != null)
                    {
                        List<PDAnnotation> annotations = page.getAnnotations();
                        removed = annotations.remove(widget);
                        System.out.println("page widget removed? " + removed);
                    }
                }
            }
            if (removed)
            {
                System.out.println("field removed? " + removed);
                doc.save(new File(dir, "PDFBOX-6179-form-saved.pdf"));
            }
        }
    }

    private static boolean removeRecursive(List<PDField> fields, PDField field)
    {
        // search the tree
        for (PDField fieldItem : fields)
        {
            if (fieldItem instanceof PDNonTerminalField)
            {
                PDNonTerminalField ntField = (PDNonTerminalField) fieldItem;
                List<PDField> children = ntField.getChildren();
                if (children.remove(field))
                {
                    ntField.setChildren(children);
                    return true;
                }
                if (removeRecursive(children, field))
                {
                    return true;                    
                }
            }
        }
        return false;
    }
}
{code}


> 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