Deleting all bookmarks in a corrupt PDF document.

Q:

I use the following code to delete all bookmarks in a PDF document, but I get an exception:

“The dictionary does not contain required key: Parent”

Could you please let us know what is wrong in the document?

Bookmark root = doc.GetFirstBookmark();
while(root.IsValid()) {
root.Delete();
root = doc.GetFirstBookmark();
}

A:

The problem is that the given bookmark in the file is corrupt. According to PDF spec all bookmarks need to have a Parent key - and this bookmark is missing the Parent key.

As a work around you can clear all bookmarks in a document using the following line:

pdfdoc.GetRoot().Erase(“Outlines”);

This is also a more efficient way of deleting all bookmarks.