Section break after table cell not working as expected

I have a word document that looks like the following

Some text

Table cell 1 Table cell 2

More Text

In my code, if I do the following (where db is a DocumentBuilder, and table index is the index of the table shown above)

db.MoveToCell(tableIndex, 0, 1, -1)
db.InsertBreak(BreakType.SectionBreakContinous)

The section break comes at the end of the document rather than between the table and the text. Any ideas?

Hi,

Thank you for considering Aspose.

You will not achieve proper results in this way because you are trying to insert a section break into a table cell. If you need to insert the break to a paragraph right after the table, the following approach could probably be better:

Table table = doc.Sections[sectionIndex].Body.Tables[tableIndex]; 
Paragraph paraAfterTable = (Paragraph)table.NextSibling;
// Try this one too.
// Paragraph paraAfterTable = (Paragraph)table.NextSibling.NextSibling;
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveTo(paraAfterTable);
builder.InsertBreak(BreakType.SectionBreakContinuous);

Note that the section break is invisible, but if you put the cursor in the paragraph below the table you will see in the status bar in MS Word it shows the cursor is in the 2nd section.
This seem to be just some strange MS Word behaviour to treat a section break that is immediately after a table in this way. If you want to insert the section break in a more “normal” MS Word-like way, then uncomment the line of code that inserts the break after the paragraph that is after the table.

Neither of the suggestions work (I had already tried the first, not the second). I’ve attached a sample document with the problem. Any other suggestions would be greatly appreciated. By the way, the reason I’m trying to insert a section break is that I need all the (formated) text after the table cell, so my normal approach for this in Aspose has been to insert a section break, then I can access all the sections on either side.

You just have no an empty paragraph after the table to insert the section break in your test document - so simply add it. Here’s the working code:

Table table = doc.Sections[sectionIndex].Body.Tables[tableIndex];

Paragraph para = new Paragraph(doc);
doc.Sections[sectionIndex].Body.InsertAfter(para, table);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveTo(para);
builder.InsertBreak(BreakType.SectionBreakContinous);

Note: for your document, both sectionIndex and tableIndex are equal to 0.