logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

New Topic Post Reply
Options
Go to last post Go to first unread
ophthalmos  
#1 Posted : Saturday, May 8, 2021 1:27:53 PM(UTC)
Quote
ophthalmos

Rank: Newbie

Groups: Registered
Joined: 4/6/2020(UTC)
Posts: 4
Germany
Location: Kiel

Code:
private PdfBookmark currBookmark = null;

private void TraverseBookmark(PdfBookmarkCollections bookmarks, int iLevel, string nTitle)
{
    foreach (PdfBookmark item in bookmarks)
    {
        TraverseBookmark(item.Childs, iLevel + 1, nTitle);
        if (item.Title.Equals(nTitle)) { currBookmark = item; }
    }
}

private void DeleteThisBookmarkToolStripMenuItem_Click(object sender, EventArgs e)
{
    currBookmark = null;
    TraverseBookmark(pdfViewer.Document.Bookmarks, 0, bookmarksViewer.SelectedNode.Text);
    if (currBookmark != null)
    { //MessageBox.Show(currBookmark.Title);
        if (pdfViewer.Document.Bookmarks.Remove(currBookmark))
        {
            bookmarksViewer.RebuildTree();
            bookmarksViewer.ExpandAll();
        }
        else { Console.Beep(); }
    }
}


The PdfBookmarkCollections.Remove Method doesn't delete any element which is a child element!?
Paul Rayman  
#2 Posted : Saturday, May 8, 2021 7:03:28 PM(UTC)
Quote
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,138

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
Bookmarks are grouped into a tree. And you are trying to remove a bookmark from the flat list that is the root of this tree. Instead, you must remove the bookmark from the flat list of children of the parent of the bookmark. Something like that:
Code:
currBookmark.Parent.Childs.Remove(currBookmark);
ophthalmos  
#3 Posted : Saturday, May 8, 2021 10:14:36 PM(UTC)
Quote
ophthalmos

Rank: Newbie

Groups: Registered
Joined: 4/6/2020(UTC)
Posts: 4
Germany
Location: Kiel

Many Thanks! Thats how it works:

Code:
if (pdfViewer.Document.Bookmarks.Remove(currBookmark) || currBookmark.Parent.Childs.Remove(currBookmark))
Paul Rayman  
#4 Posted : Sunday, May 9, 2021 12:04:29 AM(UTC)
Quote
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,138

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
In this case, one unnecessary search is performed in the collection of root elements.
Here is another option :)
Code:
if((currBookmark.Parent?.Childs ?? pdfViewer1.Document.Bookmarks).Remove(currBookmark))

Edited by user Sunday, May 9, 2021 12:07:01 AM(UTC)  | Reason: Not specified

ophthalmos  
#5 Posted : Sunday, May 9, 2021 11:48:41 AM(UTC)
Quote
ophthalmos

Rank: Newbie

Groups: Registered
Joined: 4/6/2020(UTC)
Posts: 4
Germany
Location: Kiel

Nice code! Learned something again!
Quick Reply Show Quick Reply
Users browsing this topic
Guest (3)
New Topic Post Reply
Forum Jump  
You can post new topics in this forum.
You can reply to topics in this forum.
You can delete your posts in this forum.
You can edit your posts in this forum.
You cannot create polls in this forum.
You can vote in polls in this forum.