Move bookmarks between levels

Hi,

I have pdf with following (sample) bookmarks:

Bookmark1.1
Bookmark1.2
Bookmark1.3
Bookmark2.1
Bookmark2.2

I’d like to process those bookmakrs to have following structure:

TopLevelBookmark1
Bookmark1.1
Bookmark1.2
Bookmark1.3
TopLevelBookmark2
Bookmark2.1
Bookmark2.2

I was trying with such a code:

var doc = new Aspose.Pdf.Document(memoryStream);
var previousBookmarks = doc.Outlines.Cast().ToList();
//get bookmarks for each level
var sub = previousBookmarks.Where(b => b.Title.StartsWith(“Bookmark1.”)).ToList();
var sub2 = previousBookmarks.Where(b => b.Title.StartsWith(“Bookmark2.”)).ToList();

//delete previous bookmarks
doc.Outlines.Delete();

//new top level bookmark
var newTopLevel = new OutlineItemCollection(doc.Outlines);
newTopLevel.Title=“TopLevelBookmark1”;
newTopLevel.Action = sub.First().Action; //set top level bookmark destination to be the same as dest of first child
sub.ForEach(newTopLevel.Add);
doc.Outlines.Add(newTopLevel);

//second top level bookmark
var newTopLevel2 = new OutlineItemCollection(doc.Outlines);
newTopLevel2.Title=“TopLevelBookmark2”;
newTopLevel2.Action = sub.First().Action;
sub.ForEach(newTopLevel2.Add);
doc.Outlines.Add(newTopLevel2);

doc.Save(<some_path_to_save>);

Unfortunatelly final outcome looks like:
TopLevelBookmark1
Bookmark1.1
Bookmark1.2
Bookmark1.3
Bookmark2.1
Bookmark2.2
TopLevelBookmark2
Bookmark2.1
Bookmark2.2

I’ve tested also with different subsets of previousBookmarks collection, and it’s weird for me that when I even add only sub.Take(1) to the newMain collection, it adds all bookmarks as children to the TopLevelBookmark1.

Hi Robert,


Thanks for your inquiry. We are looking into the issue and will get back to you soon.

Best Regards,