Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Aug 30, 2018 10:48 am

Hello,

Is there a way to get all child bookmarks of a particular bookmark in a pdf?

nilarya
 
Posts: 20
Joined: Mon Mar 19, 2018 7:23 am

Fri Aug 31, 2018 4:01 am

Hello,

Thanks for your inquiry.
Below code can fulfill your requirement. Please have a try. If there is any question, welcome to write back.
Code: Select all
List<PdfBookmark> lists = new List<PdfBookmark>();
private void button1_Click(object sender, EventArgs e)
{
    PdfDocument doc = new PdfDocument();
    doc.LoadFromFile("Bookmark.pdf");

    //Get the specific bookmark
    PdfBookmarkCollection bookmark = doc.Bookmarks[0];

    //Get all bookmarks         
    GetBookmark(bookmark);

    PdfBookmark[] allBookmarks = lists.ToArray();
}
private void GetBookmark(PdfBookmarkCollection bookmarks)
{
    if (bookmarks.Count > 0)
    {
        foreach (PdfBookmark parentBookmark in bookmarks)
        {
            lists.Add(parentBookmark);
            GetBookmark(parentBookmark);
        }
    }
}

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Fri Aug 31, 2018 2:28 pm

Hello,
Thank you, now I am getting bokmarklist but I don't get the page number of bookmarks. I am using the following code:


PdfDestination des = parentBookmark.Destination;//get the second bookmark.
int pageNum = doc.Pages.IndexOf(des.Page) + 1;//get the page number.

Please suggest me how I get the page number of a bookmarks.

and another question is that I am not getting the parent details of the traversed bookmark.


Can you please suggest how i can get parent title if I need any kind of casting, please suggest.

nilarya
 
Posts: 20
Joined: Mon Mar 19, 2018 7:23 am

Mon Sep 03, 2018 7:28 am

Dear nilarya,

Thanks for your feedback and sorry for late reply as weekend.
The code you used to get the page number is correct, and below code shows how to get title of the bookmarks. If there is any question, welcome to write back.
Code: Select all
Dictionary<string, int> lists = new Dictionary<string, int>();
private void button1_Click(object sender, EventArgs e)
{
    PdfDocument doc = new PdfDocument();
    doc.LoadFromFile("Bookmark.pdf");

    //Get the specific bookmark
    PdfBookmarkWidget bookmark = doc.Bookmarks[1] as PdfBookmarkWidget;

    //Get parent bookmark title and page number
    string title = bookmark.Title;
    int pageNum=doc.Pages.IndexOf(bookmark.Destination.Page) + 1;
    lists.Add(title,pageNum);

    //Get all bookmarks         
    GetBookmark(bookmark, doc);
}
private void GetBookmark(PdfBookmarkCollection bookmarks,PdfDocument doc)
{
    if (bookmarks.Count > 0)
    {
        foreach (PdfBookmark parentBookmark in bookmarks)
        {
            //Get bookmark title and page number
            string title = parentBookmark.Title;
            int pageNum = doc.Pages.IndexOf(parentBookmark.Destination.Page) + 1;
            lists.Add(title, pageNum);
            GetBookmark(parentBookmark,doc);
        }
    }
}

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Mon Sep 03, 2018 1:20 pm

Hello,

I have used the code you have suggested for the page number of a bookmarks, but for one file I can't get the page number. I am getting the following error:
[img]

[/img]


I have attached the .pdf file.

Thank you

nilarya
 
Posts: 20
Joined: Mon Mar 19, 2018 7:23 am

Tue Sep 04, 2018 2:14 am

Dear nilarya,

Thanks for your feedback.
I have noticed the issue that the destination of the bookmark is null and posted it to our Dev team for further analysis and investigation, if there is any good news, we will let you know immediately. Sorry for the inconvenience caused.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Mon Sep 10, 2018 2:27 pm

Hello ,


Can you find any solution for getting page number of bookmarks for the previous attached file, please let us know.


Thank you,

nilarya
 
Posts: 20
Joined: Mon Mar 19, 2018 7:23 am

Tue Sep 11, 2018 7:28 am

Dear nilarya,

Sorry we haven't found a solution for your issue, our Dev team is still working on it. Please spare us more time. If it is resolved, we will inform you immediately.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Sun Sep 30, 2018 3:45 am

Dear nilarya,

Thanks for your patient waiting.
The issue of getting page number has been fixed. You could download the hotfix from this link: Spire.PDF Pack(Hot Fix) Version:4.9.7. However, you will see that the exception still occurs when getting the page number of some bookmarks (named titles and images). The issue is caused by your Pdf file itself. Some bookmarks in the file don't have bookmark action so that they have no page destination. You need to modify your original document to avoid the exception. If there is any question, welcome to write back.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.PDF