Section break after table cell not working as expected


#1

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?

#2

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];<?xml:namespace prefix = o ns = “urn:schemas-microsoft-com:office:office” /><o:p></o:p>

Paragraph paraAfterTable = (Paragraph)table.NextSibling;<o:p></o:p>

//Try this one too.
//Paragraph paraAfterTable = (Paragraph)table.NextSibling.NextSibling;<o:p></o:p>

DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveTo(paraAfterTable);
builder.InsertBreak(BreakType.SectionBreakContinuous);<o:p></o:p>

<o:p>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.<o:p></o:p>

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.<o:p></o:p>

</o:p>

#3

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.


#4

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.