Add bookmark to existing PDF document using Aspose.PDF for .NET - Insert bookmark at root level


Hi,

I have an existing pdf with bookmarks and I want to add a new root bookmark e.g. the filename and add existing under that, how can I do that? any easy way?

e.g. currently:-
Example 1
--- Example 1 child
Example 2
Example 3

need:-
bookmark.pdf
---Example 1
------Example 1 child
---Example 2
---Example 3

c# - currently just add's it at the end.

Document doc = new Document("bookmark.pdf");
// Create new bookmark.
OutlineItemCollection newBookmark = new OutlineItemCollection(doc.Outlines);
newBookmark.Title = "bookmark.pdf";
newBookmark.Destination = new Aspose.Pdf.InteractiveFeatures.FitHExplicitDestination(doc.Pages[12], 792);

// Save previous bookmarks list.
IList prevBookmarks = new List();
foreach (OutlineItemCollection bookmark in doc.Outlines)
{

bookmark.Add(newBookmark); // adding new bookmark

prevBookmarks.Add(bookmark);
}
// Remove existing bookmarks.
doc.Outlines.Delete();
// Add updated bookmarks.
foreach (OutlineItemCollection prevBookmark in prevBookmarks)
{
doc.Outlines.Add(prevBookmark);
}

doc.Save("bookmark_out.pdf");

worked it out from a previous thread.


Document doc = new Document(“bookmark.pdf”);
OutlineCollection rootBookmark = doc.Outlines;
OutlineItemCollection newRootBookmark = new OutlineItemCollection(rootBookmark);
newRootBookmark.Title = “bookmark.pdf”;
newRootBookmark.Destination = new Aspose.Pdf.InteractiveFeatures.FitHExplicitDestination(1, doc.Pages[1].Rect.Height);
// or newRootBookmark.Action = …
foreach (OutlineItemCollection bookmarks in rootBookmark)
{
newRootBookmark.Add(bookmarks);
}
doc.Outlines.Delete();
doc.Outlines.Add(newRootBookmark);
doc.Save(“bookmark_out.pdf”);

Hi Lee,


Thank you very much for your feedback. It is good to know that you have managed to accomplish your requirement.

Please keep using our API and feel free to contact us for any further query or concern, we will be more than happy to extend our support.

Best Regards,