Delete a Section

Hi,
I have a document that conatins bookmark, and I want to insert a section in a specif bookmark.
So I do:

Builder.MoveToBookmark("MyBookmark");
Builder.InsertBreak(BreakType.SectionBreakNewPage);

Then I save the document.
How can I do to delete this Section in a second time ?

Hi

Thanks for your inquiry. In your case you should move DocumentBuilder cursor before bookmark. You should just use another overload of MoveToBookmark method:
https://reference.aspose.com/words/net/aspose.words/documentbuilder/movetobookmark/
If you insert SectionBreak before bookmark, you will be able to identify section, which you need to remove by this bookmark. Here is sample code to insert section break right before bookmark:

Document doc = new Document(@"Test001\in.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
// Move builder cursor before bookmark.
builder.MoveToBookmark("test", true, false);
// Insert section break.
builder.InsertBreak(BreakType.SectionBreakNewPage);
// Save output document
doc.Save(@"Test001\out.doc");

And here is code to remove section with bookmark.

Document doc = new Document(@"Test001\out.doc");
// Get bookmark
Bookmark bk = doc.Range.Bookmarks["test"];
if (bk != null)
{
    // remove section where this bookmark is located.
    bk.BookmarkStart.GetAncestor(NodeType.Section).Remove();
}
// Save output document.
doc.Save(@"Test001\out1.doc");

Hope this helps.
Best regards.

Hi, thank you for your help,
but I don’t want to delete the bookmark beause I want to re-use it.
So I need to insert the section after the bookmark and then retrieve it and delete it.
Best regards

Hi

Thank you for additional information. In this case, you should insert section break after the bookmark. And then remove Section which follows the Section where bookmark is located.
Best regards.

Hi, How can I insert a new bookmark into the section create before ?

builder.insertbreak(BreakType.SectionBreakNewPage);
// Here insert a new bookmark
// And then I want to insert nodes at this location
Builber.insertNode(node);

Best regard

Hi

Thanks for your inquiry. Of course, you can. You can use the following code to insert bookmark:

string bkName = "myBk";
builder.StartBookmark(bkName);
builder.EndBookmark(bkName);

But you should note, MS Word document cannot contain few bookmarks with the same name.
Best regards.