Font cannot be set in Words table

Hi,

Aspose.Word doesn’t keep the formatting of a table cell when it is preceded by a specific paragraph (see the attached Part1 - BadGeneration.docm). Here is our scenario:

  1. Create a Word document with Aspose.
  2. Add to the new document a paragraph from Part1 - BadGeneration.docm.
  3. Create a table and add exactly the content of A1 cell in AffectedTable.xlsm and KEEP THE SAME STYLE.
  4. Generate the end result.

Problem:
Even if we set the cell font to Time New Roman, the end result document contains the table with Calibri font.
Notice this is not occurring if we use Part1_GoodGeneration.docm.

I have attached a sample application demonstrating the issue. Can you provide a fix or workaround?

Thank you,

Hi,

Aspose.Word doesn’t keep the formatting of a table cell when it is preceded by a specific paragraph (see the attached Part1 - BadGeneration.docm). Here is our scenario:

  1. Create a Word document with Aspose.
  2. Add to the new document a paragraph from Part1 - BadGeneration.docm.
  3. Create a table and add exactly the content of A1 cell in AffectedTable.xlsm and KEEP THE SAME STYLE.
  4. Generate the end result.

Problem:
Even if we set the cell font to Time New Roman, the end result document contains the table with Calibri font.
Notice this is not accuring if we use Part1_GoodGeneration.docm.

I have attached a sample application demonstrating the issue. Can you provide a fix or workaround?

Thank you,

Hi Jawad,

Thanks for your inquiry. Could you please attach your input/output documents here for testing? I will investigate the issue on my side and provide you more information.

Here attached the sample application demonstrating the issue.

Thanks,

Hi Jawad,

Thanks for sharing the detail. Please call Font.ClearFormatting method before creating the table in MS Word document. Please see the highlighted line in following code snippet. This will solve your problem. Please let us know if you have any more queries.

var doc = new Document();
var wordDocBuilder = new DocumentBuilder(doc);
string wordFile = @"Part1 - BadGeneration.docm";
Section sec = doc.LastSection;
Paragraph afterNode = sec.Body.LastParagraph;
var sourceDocument = new Document(wordFile);
InsertDocument(afterNode, sourceDocument);
wordDocBuilder.MoveToDocumentEnd();
wordDocBuilder.Font.ClearFormatting();
Table wordTable = wordDocBuilder.StartTable();
string excelFile = @"AffectedTable.xlsm";
var wb = new Workbook(excelFile);
var ws = wb.Worksheets[0];
Cell excelCell = ws.Cells["A1"];
Aspose.Cells.Style excelStyle = excelCell.GetStyle();
wordDocBuilder.InsertCell();
wordDocBuilder.Font.Name = excelStyle.Font.Name;
wordDocBuilder.Font.Size = excelStyle.Font.Size;
wordDocBuilder.Write(excelCell.StringValue);
wordDocBuilder.EndRow();
wordDocBuilder.EndTable();
wordTable.PreferredWidth = PreferredWidth.Auto;
string wordResultFile = excelFile.Replace("xlsm", "docm");
doc.Save(wordResultFile);