Combing Table Style and custom format in single cells

Hi all,

I try to understand the capabilities of the Table Style handling in Aspose.Words. My question is: is it possible to apply a Table Style to a table, and also format single cells in the table differently (e.g bold or different back color)?

Attached is as sample where the cell 2/3 has a different back color, bold font and horizontal alignment. Start it and click the button. It will load the template “TableStyleTest.docx” and create a “bin\Debug\result.docx” file.

  • The BackColor (“CellFormat.Shading.BackgroundPatternColor”) is not reset afterwards. If I set it to “Color.Empty” for the next cell, the table style color will not be applied to those cells. How could one reset the color to the style default?
  • The ForeColor (“cell.CellFormat.Shading.ForegroundPatternColor”) seems to be ignored?!
  • The font: after the bold cell, I reset it to “bold = false”, and all other cells are non-bold, though the table style declares all odd rows bold. How could one reset the font to the style default?
  • Horizontal alignment: setting “docBuilder.ParagraphFormat.Alignment” for cell 2/3 and resetting it for all other cells works fine.


Are those limitations of Aspose.Words, or is my code wrong?

Best regards

Wolfgang

It gets even worse if you add a “simple” table without table style but with some custom formatted cells to the document, then add a table styled table to the doc. All custom formatting of the document builder is applied to the styled table.

Hi,


I am afraid, creation of Table Styles is not supported at the moment.

Please attach your expected document here for our reference. We will investigate the structure of your expected document as to how you want your final output be generated like. You can create expected document using Microsoft Word.

We will then start investigation into your issue and provide you code to achieve the same by using Aspose.Words. Thanks for your cooperation.

Best regards,

Hi,

I don’t have the requirement to use Table styles, so I cannot provide you with a report sample.

I recently added to one of our configurable word reports the option to predefine a table style in a word template and apply this style to a table generated by our report. While doing this, I wonderes whether it would be possible to combine a table style with custom formatted cells (e.g. different back color or bold font).

If your reply is that this is not possible in the moment, this is OK for me as it would not break any customer requirement. Maybe we will need this in the far future, but as we know that it is not possible at the moment, we will try to avoid it ;-).

Probably this would be a major change to the DocumentBuilder class, maybe a method like “ResetFormattingToTableStyle” is needed.

Best regards

Wolfgang

Please not that my initial question was not about creation of Table Styles - I picked them from a word template. I just asked about a way to combine Table Style and custom formatting for single cells.

Best regards

Wolfgang

Hi,


Before applying new direct-formatting (i.e. in addition to formatting specified via Style), you may try clearing old DocumentBuilder settings by using following methods:

ParagraphFormat.ClearFormatting Method
Font.ClearFormatting Method

RowFormat.ClearFormatting Method
CellFormat.ClearFormatting Method

Border.ClearFormatting Method
BorderCollection.ClearFormatting Method

Shading.ClearFormatting Method

Hope, these methods help.

Best regards,

Hi,

amazing, with a call to “cell.CellFormat.ClearFormatting” and “documentBuilder.Font.ClearFormatting” it worked. Many thanks for your help!

Just for the records and for all who find this thread when researching similar questions: attached is a reworked sample.
I noticed that the cell fore color cannot be set by using “cell.CellFormat.Shading.ForegroundPatternColor” (which works when not using a table style), but by using “docBuilder.Font.Color”.

I was very near to achieving this goal, but “cell.CellFormat.Shading.ClearFormatting” was probably not enough in my previous attempt :wink:

Best regards

Wolfgang

Hi,


It is great you were able to find what you were looking for. Please let us know any time you have any further queries.

Best regards,