Remove all content after bookmark

Hi,

I need help. I am trying to remove all the content after a bookmark. Also, at some point I will need to remove all content before a bookmark. I can’t figure out how to do this.

Thanks in advance

Hi
Thanks for your request. Please try using the following code:

/// 
/// Method removes content after specified bookmark
/// 
/// Document that will be processed
/// Name of bookmark
private void RemoveContentAfterBookmark(Document doc, string bookmarkName)
{
    Node curNode = doc.Range.Bookmarks[bookmarkName].BookmarkEnd.NextPreOrder(doc);
    while (curNode != null)
    {
        // Remove all children of current node
        if (curNode.IsComposite)
            ((CompositeNode)curNode).RemoveAllChildren();
        // Move to next node
        Node nextNode = curNode.NextSibling;
        if (nextNode == null)
            nextNode = curNode.NextPreOrder(doc);
        // Remove current node
        curNode.Remove();
        curNode = nextNode;
    }
}
/// 
/// Method removes content before specified bookmark
/// 
/// Document that will be processed
/// Name of bookmark
private void RemoveContentBeforeBookmark(Document doc, string bookmarkName)
{
    Node startFrom = doc.Range.Bookmarks[bookmarkName].BookmarkStart;
    Node curNode = doc.Range.Bookmarks[bookmarkName].BookmarkStart.PreviousPreOrder(doc);
    while (curNode != null)
    {
        CompositeNode currComposite = curNode as CompositeNode;
        // Remove all children of current node if current node does not contain specified bookmark
        if (currComposite != null && !currComposite.ChildNodes.Contains(startFrom))
            currComposite.RemoveAllChildren();
        // Move to next node
        Node nextNode = curNode.PreviousSibling;
        if (nextNode == null)
            nextNode = curNode.PreviousPreOrder(doc);
        // Remove current node if current node does not contain specified bookmark
        if (currComposite == null || !currComposite.ChildNodes.Contains(startFrom))
            curNode.Remove();
        else if (currComposite != null)
            startFrom = currComposite;
        curNode = nextNode;
    }
}

Hope this helps.
Best regards.

Thanks for the code and the quick reply. I am using vb.net, so I had to convert the code. The first function works great, but I am having trouble with one line in the second.

CompositeNode currComposite = curNode as CompositeNode;

produces the error “Unable to cast object of type ‘Aspose.Words.Run’ to type ‘Aspose.Words.CompositeNode’.”

Also, I am posting my converted vb.net code just in case anyone reading this forum may need it.

Thanks

Private Sub RemoveContentAfterBookmark(ByVal doc As Aspose.Words.Document, ByVal bookmarkName As String)

Dim curNode As Aspose.Words.Node = doc.Range.Bookmarks(bookmarkName).BookmarkEnd.NextPreOrder(doc)

While curNode IsNot Nothing
'Remove all children of current node
If curNode.IsComposite Then
Dim currcomposite As Aspose.Words.CompositeNode = curNode
currcomposite.RemoveAllChildren()
End If

'Move to next node
Dim nextNode As Aspose.Words.Node = curNode.NextSibling
If nextNode Is Nothing Then
nextNode = curNode.NextPreOrder(doc)
End If

'Remove current node
curNode.Remove()

curNode = nextNode
End While

End Sub

Private Sub RemoveContentBeforeBookmark(ByVal doc As Aspose.Words.Document, ByVal bookmarkName As String)

Dim startFrom As Aspose.Words.Node = doc.Range.Bookmarks(bookmarkName).BookmarkStart
Dim curNode As Aspose.Words.Node = doc.Range.Bookmarks(bookmarkName).BookmarkStart.PreviousPreOrder(doc)

While curNode IsNot Nothing

Dim currcomposite As Aspose.Words.CompositeNode = curNode
'Remove all children of current node if current node does not contain specified bookmark
If currcomposite IsNot Nothing And currcomposite.ChildNodes.Contains(startFrom) = False Then
currcomposite.RemoveAllChildren()
End If

'Move to next node
Dim nextNode As Aspose.Words.Node = curNode.PreviousSibling
If nextNode Is Nothing Then
nextNode = curNode.PreviousPreOrder(doc)
End If

'Remove current node if current node does not contain specified bookmark
If currcomposite Is Nothing Or currcomposite.ChildNodes.Contains(startFrom) = False Then
curNode.Remove()
ElseIf currcomposite IsNot Nothing Then
startFrom = currcomposite
End If

curNode = nextNode

End While

End Sub

Hi
Thanks for your request. You should use TryCast.

Dim currcomposite As Aspose.Words.CompositeNode = TryCast(curNode, Aspose.Words.CompositeNode)

Best regards.