Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.
Tue May 21, 2019 1:05 pm
Could you provide sample c# code on deleting content between two bookmarks in a word document. Our client wants to create "beginning" and "ending" bookmarks around document content. Then have us remove the content in between the two bookmarks for certain scenarios.
-
tholland
-
- Posts: 23
- Joined: Fri Dec 01, 2017 6:38 pm
Wed May 22, 2019 6:10 am
Hi,
Thank you for your inquiry.
Please refer to the following code to delete content between two bookmarks. If there is any question, please feel free to write back.
- Code: Select all
Document doc = new Document();
doc.LoadFromFile("bookmarks.docx");
//get the index of BookmarkEnd of bookmark named 'start'
BookmarksNavigator navigator = new BookmarksNavigator(doc);
navigator.MoveToBookmark("start");
BookmarkEnd end = navigator.CurrentBookmark.BookmarkEnd;
int endIndex=end.OwnerParagraph.ChildObjects.IndexOf(end);
//get the index of BookmarkStart of bookmark named 'end'
BookmarksNavigator navigator1 = new BookmarksNavigator(doc);
navigator1.MoveToBookmark("end");
BookmarkStart start = navigator1.CurrentBookmark.BookmarkStart;
int startIndex=start.OwnerParagraph.ChildObjects.IndexOf(start);
//create a new bookmark
BookmarkStart st = new BookmarkStart(doc, "myBookmark");
BookmarkEnd ed = new BookmarkEnd(doc, "myBookmark");
end.OwnerParagraph.ChildObjects.Insert(endIndex, st);
start.OwnerParagraph.ChildObjects.Insert(startIndex, ed);
//delete content of the bookmark
BookmarksNavigator navigator3 = new BookmarksNavigator(doc);
navigator3.MoveToBookmark("myBookmark", true, true);
navigator3.DeleteBookmarkContent(true);
//delete the 'myBookmark'
BookmarkCollection bookmarks = doc.Bookmarks;
Bookmark bookmark = bookmarks["myBookmark"];
doc.Bookmarks.Remove(bookmark);
doc.SaveToFile("checkbox.docx", FileFormat.Docx2013);
Sincerely,
Nancy
E-iceblue support team
-
nancy.yang
-
- Posts: 184
- Joined: Wed Apr 03, 2019 2:33 am
Wed May 22, 2019 7:56 pm
Thank you, I will try this logic.
-
tholland
-
- Posts: 23
- Joined: Fri Dec 01, 2017 6:38 pm
Thu May 23, 2019 7:47 am
Hi,
Thanks for your feedback.
Have you tested my code? Did it meet your requirement?
Sincerely,
Nancy
E-iceblue support team
-
nancy.yang
-
- Posts: 184
- Joined: Wed Apr 03, 2019 2:33 am