Adding parent bookmark to PDF documents

Hi,

I am trying to add a parent bookmark to an existing pdf document and I am using following piece of code. But Adding parent bookmark does not point to first page of document. It either points to second page of document or it points to end of first page. How can I point it to the first page of document?

        Aspose.Pdf.Document p = new Aspose.Pdf.Document("DocInput.pdf");
        Aspose.Pdf.OutlineCollection rootBookmark = p.Outlines;
        Aspose.Pdf.OutlineItemCollection newRootBookmark = new Aspose.Pdf.OutlineItemCollection(rootBookmark);
        newRootBookmark.Title = "label";
        newRootBookmark.Destination = new Aspose.Pdf.Annotations.FitHExplicitDestination(p.Pages[1], p.Pages.Count);


        new FitHExplicitDestination(p.Pages[1], p.Pages.Count);
        foreach (Aspose.Pdf.OutlineItemCollection bookmarks in rootBookmark)
        {
            newRootBookmark.Add(bookmarks);
        }
        p.Outlines.Delete();
        p.Outlines.Add(newRootBookmark);

        p.Save("DocOutput.pdf"); 

DocInput.pdf (1.7 MB)DocOutput.pdf (1.7 MB)

Hello Muzna,

Thanks for contacting support.

In order to set the destination of bookmark, you need to specify page number and top position of the destination. Please check following code snippet where I have set the destination of root bookmark item to the first page. For your reference, I have also attached an output, generated by below code.

Aspose.Pdf.Document p = new Aspose.Pdf.Document(dataDir + "DocInput.pdf");
Aspose.Pdf.OutlineCollection rootBookmark = p.Outlines;
Aspose.Pdf.OutlineItemCollection newRootBookmark = new Aspose.Pdf.OutlineItemCollection(rootBookmark);
newRootBookmark.Title = "label";
newRootBookmark.Destination = new Aspose.Pdf.Annotations.FitHExplicitDestination(_1, p.Pages[1].Rect.URY_);
foreach (Aspose.Pdf.OutlineItemCollection bookmarks in rootBookmark)
{
 newRootBookmark.Add(bookmarks);
}
p.Outlines.Delete();
p.Outlines.Add(newRootBookmark);
p.Save(dataDir + "DocOutput.pdf");

DocOutput.pdf (1.7 MB)

In case of any further assistance, please feel free to contact us.


Best Regards,
Asad Ali