How do I delete content between two different bookmarks in word?
How to delete content outside a bookmark in word? Bookmark a contains other bookmarks?
How do I delete content between two different bookmarks in word?
How to delete content outside a bookmark in word? Bookmark a contains other bookmarks?
Please see these sample input/output Word documents (Docs.zip (21.0 KB)). The following Aspose.Words’ code should delete all content between two bookmarks in Word document:
Document doc = new Document("E:\\temp\\in.docx");
Node startNode = doc.Range.Bookmarks["bm1"].BookmarkEnd;
Node endNode = doc.Range.Bookmarks["bm2"].BookmarkStart;
Node curNode = startNode.NextPreOrder(doc);
while (curNode != null)
{
if (curNode.Equals(endNode))
break;
Node nextNode = curNode.NextPreOrder(doc);
if (curNode.IsComposite)
{
if (!(curNode as CompositeNode).ChildNodes.Contains(endNode) &&
!(curNode as CompositeNode).ChildNodes.Contains(startNode))
{
if (curNode.NextSibling == null)
{
Section curSect = (Section)curNode.GetAncestor(NodeType.Section);
if (curSect.NextSibling != null)
{
curSect.AppendContent((Section)curSect.NextSibling);
curSect.NextSibling.Remove();
endNode = doc.Range.Bookmarks["bm2" + "_0"].BookmarkStart;
doc.Range.Bookmarks["bm2" + "_0"].Name = "bm2";
}
}
nextNode = curNode.NextSibling;
curNode.Remove();
}
}
else
{
curNode.Remove();
}
curNode = nextNode;
}
doc.Save("E:\\Temp\\19.9.docx");
Hope, this helps.