We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

builder.Font.ClearFormatting() behaves differently depending on table cell contents

Hi,

I’m using Aspose.Words 21.10 In our code we often need to call builder.Font.ClearFormatting() after a table row is built but before entire table is finalized. I’ve noticed that this behaves in an odd manner when the last cell has no contents in it - when there is text in the last cell, if we call .EndRow() and then Font.ClearFormatting(), the cell retains its font settings, but if the cell is empty and we call these two methods, the cell’s font settings are reset to default. I’ve replicated it with the following code:

 Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);


builder.Writeln("this is default size");

Table table1 = builder.StartTable();

builder.Font.Size = 5;
builder.InsertCell();
builder.Write("This should be size 5, and the following cell should also be size 5");
builder.InsertCell();//with no text in the cell, its font gets reset to default if the ClearFormatting is called after row is closed
builder.EndRow();

builder.Font.ClearFormatting();
builder.EndTable();

builder.Writeln("this is here just to separate the tables in the word doc, and should be default size");

Table table2 = builder.StartTable();

builder.Font.Size = 5;
builder.InsertCell();
builder.Write("This should be size 5, and the following cell should also be size 5");
builder.InsertCell();
builder.Write("This should be size 5");//with text in the cell, its font doesn't get reset to default if the ClearFormatting is called after row is closed
builder.EndRow();

builder.Font.ClearFormatting();
builder.EndTable();

builder.Writeln("this is last paragraph, and should be default size");

doc.Save(@"path to your file.docx");

I would expect the last cell in first table to have font size =5, similarly to the last cell in second table, but it seems to reset to default value. Could you advise please?

Thanks,
Tomek

@acturisaspose Thank you for reporting this issue to us. There are two issues in this case.

  1. Font size increase in the last cell of the first table
  2. Returning small font after the first table after ClearFormatting has been called

As for the first issue, this may be not entirely obvious, but nevertheless correct behavior. Since after calling builder.InsertCell() you are inside the Сell Paragraph, you then execute ClearFormatting() which clears the cell paragraph current formatting. Thus, the default formatting for the break run symbol comes into effect for this cell, it is applied if the paragraph is empty, that is, it does not contain anything except the break run symbol.
It’s more correct to call ClearFormatting() after builder.EndTable() in this case.
As for the second issue, I have logged it as WORDSNET-23389. We will keep you informed and let you know once it has been resolved.