DEV Community

Discussion on: Add and Delete Bookmarks in PDF in Java

Collapse
 
eiceblue profile image
E-iceblue Product Family • Edited

Hi,

Thanks for commenting. You can refer to the below code to delete the bookmarks that have no child bookmarks:

PdfDocument doc = new PdfDocument();
        doc.loadFromFile("D:/BookmarkSample.pdf");

        for (int i = 0; i < doc.getBookmarks().getCount(); i++) {
            PdfBookmark pb = doc.getBookmarks().get(i);
            //Remove the bookmark that has no child bookmark
            if (pb.getCount() == 0) {
                doc.getBookmarks().removeAt(i);
                i--;
            }
        }
        doc.saveToFile("D:/out.pdf");
Enter fullscreen mode Exit fullscreen mode
Collapse
 
chandrashekharahg profile image
CHANDRASHEKHARA-HG

Thank you so much... for your reply

Thread Thread
 
chandrashekharahg profile image
CHANDRASHEKHARA-HG

I have one more doubt on this, im using Apache PDFBox libs so im not able use this code