StyleSeparator in table cells

Does Aspose.Words support style separators in table cells?

Word supports it partially: you cannot enter a style separator by pressing CTRL+ALT+ENTER. But you can copy existing text with a style separator to a table cell.

Attached sample tries to create a cell content with style separator, but the resulting cell has only one style for the full cell.
AsposeWordsStyleSeparator.zip (13.3 KB)
Is there an error in my sample or is it a missing feature?

Best regards
Wolfgang

@wknauf Inserting style separator is a bit tricky, because InsertStyleSeparator() method does not insert anything, but it modifies current Paragraph’s properties and convert it into style separator Paragraph. This is the way similar to what MS Word does. For Table content, we cannot convert end of Cell Paragraph, for that reason we need to insert a new Paragraph first.
You can use the following code as a workaround for this problem:

...
var cell11 = docBuilder.InsertCell();
docBuilder.InsertParagraph();
docBuilder.MoveTo(cell11.FirstParagraph);
docBuilder.ParagraphFormat.StyleName = "CustomStyle1";
docBuilder.Write("Cell 1/1 ");
docBuilder.InsertStyleSeparator();
docBuilder.ParagraphFormat.StyleName = "CustomStyle2";
docBuilder.Write("with style separator ");
...

Thanks, this worked for my simple sample. But I made some changes, and two more problems arose:
AsposeWordsStyleSeparator_2023-01-24.zip (13.5 KB)

a) If I call “InsertParagraph”, but don’t use a StyleSeparator, an additional newline is added to the cell (cell labeled “Cell 2/0”). This does not happen if the cell contains a style separator. I can work around this by checking whether the cell actually needs a style separator.

b) if the cell text has multiple lines, I could not manage to add a style separator in the second line (see code for cell with content “Cell 1/1 line 2”). Probably I have to jump to another paragraph? But which one?

Probably style separators in table cells are much more complex than I first thought.

@wknauf answering your questions in order:
a) This issue is caused by how a table cell works, when you create a cell it contains by default a special kind of “Paragraph” the end of that paragraph is also the end of the cell. So, if you insert a new paragraph in the cell then you will have 2 ends of paragraph in that cell and that cause the line break, cause by default if you have an element after the end of a paragraph it will by displayed in a new line. The method InsertStyleSeparator() modifies the end of line of a regular paragraph preventing that line break. So, solution for this issue could be as you mention (the best option) only create a paragraph when a change of style is required or always call to the method InsertStyleSeparator(), as I mentioned that method not insert anything new in the document, just modify the end of line of the paragraph, so it’s safe to use.

b) The issue here is the same that the original, after the call to the Writeln() method the end of paragraph on target is the end of paragraph of the cell (which is also the end of cell) and the InsertStyleSeparator() method can’t modify it. So your thoughts are correct you need to insert a new paragraph and move the builder to it (the last paragraph in a cell is always the default paragraph of the cell), see the bellow code:

...
//Workaround to make the style separator work:
docBuilder.InsertParagraph();
docBuilder.MoveTo(cell11.FirstParagraph);
//It makes no change whether the style name is set before or after writing the content.
docBuilder.Write("Cell 1/1 line 1");
docBuilder.ParagraphFormat.StyleName = "CustomStyle1";

docBuilder.InsertStyleSeparator();

docBuilder.Write("with style separator");
docBuilder.ParagraphFormat.StyleName = "CustomStyle2";

docBuilder.Writeln();
docBuilder.InsertParagraph();

// Move the builder to the last inserted paragraph
docBuilder.MoveTo(cell11.Paragraphs[cell11.Paragraphs.Count - 2]);
docBuilder.ParagraphFormat.StyleName = "CustomStyle1";
docBuilder.Write("Cell 1/1 line 2");
docBuilder.InsertStyleSeparator();
docBuilder.ParagraphFormat.StyleName = "CustomStyle2";
docBuilder.Write("with style separator ");
...

Whow, it is getting more and more difficult.

Will “last inserted paragraph” always be “cell11.Paragraphs[cell11.Paragraphs.Count - 2]” if more than two rows are added, or will this index change for each paragraph?

@wknauf for the cell the last inserted paragraph will be always the paragraph with the index:
index = lastIndex - 1 (indexes starts counting from 0) or
index = itemsCount - 2 (item counter starts counting from 1)
so, you can use always the same expression to get the last inserted paragraph, keep in mind that the last paragraph in a cell is the default “paragraph” that comes with the cell

Thanks for the clarification!

1 Like