Help Deleting a Section


#1

I need some help. I'm using Aspose.Word to construct a contract.

I have a set of paragraphs onto which I have placed a named bookmark. Also, in my experimentation, I have placed a Continuous Section Break before and directly after the paragraphs.

From my code, I would like to conditionally delete these paragraphs. I am having trouble figuring out the right combination of commands to do this.

I guess what I'd like to do is move to the bookmark, then delete the text and quit using the section breaks. But you tell me.

These lines are effective at deleting the first node in the area, but not the whole thing.

if (builder.MoveToBookmark("Security_Clearence"))
builder.CurrentNode.ParentNode.Range.Delete();

This doesn't seem to work either.

builder.Document.Range.Bookmarks["Security_Clearence"].BookmarkStart.ParentNode.RemoveAllChildren();


#2

If you want to remove the section containing the specified bookmark then the following code will do the trick:

Node node = doc.Range.Bookmarks["SectionX"].BookmarkStart;

while (node.NodeType != NodeType.Section) node = node.ParentNode;

node.Remove();


#3

Thanks! That did the trick. But it left the last section break. How do I remove the section break? It's a continuous break. I know I can do SectionsIdea [I].Remove(), but I don't know the index. And even if I did, calling that Remove seems to remove everything below that break. I just want the break to disappear.

As a reminder, what I have is a blob of text between two continuous section breaks. There is a bookmark in it. So I ran your code, which finds the bookmark, then finds the top of the section and deletes the section. But it leaves the bottom section break.

I tried removing the bottom break, but then everything in the document below the last section break is removed as well. Any ideas? It almost seems like I need to get at all the children of the bottom break and move them up a generation.


#4

Please send the document with the section break that you want to remove. It will help to make exact recommendations on how to remove it.


#5

In your example you have put the beginning of the bookmark in the 'begin' section of the document, which effectively marks this section for removal. After it is removed, there are two sections remaining - 'middle' and 'end' with a section break between them. So, this remaining section break is not related to the section being deleted.

To recommend something I need to understand what are you trying to achieve. Do you want to get rid of all continuous section breaks after removing bookmarked sections? Or you just want to merge sections before and after removed section? For the latter I have composed a code snippet that does the thing:

//delete the middle section

//We are deleting backwards

Node node = builder.Document.Range.Bookmarks["My_Bookmark"].BookmarkStart;

while (node.NodeType != NodeType.Section)

node = node.ParentNode;

Section sectionToRemove = (Section)node;

Section nextSection = (Section)sectionToRemove.NextSibling;

Section previousSection = (Section)sectionToRemove.PreviousSibling;

sectionToRemove.Remove();

if(nextSection!=null && previousSection!=null)

{

foreach(Node n in nextSection.Body)

{

previousSection.Body.ChildNodes.Add(n.Clone(true));

}

nextSection.Remove();

}