Aspose Words using/saving document with Times New Roman Font behind the scenes

Hi,

I am not understanding why Aspose Words using Times New Roman font behind the scenes even when I specify a font style in code, i.e., Calibri. I have checked my machine and all fonts are available → Windows.Fonts…
Sample code:

builder.Font.Name = "Calibri";
// builder.Font.Style.Name = "Calibri"; --> also tried
builder.InsertCell();
builder.Write(cell_value);

Have also tried builder.Font.ClearFormatting();

Also tried the below among many other things…

Document doc = new Document("path");
FontSettings.DefaultInstance.SubstitutionSettings.DefaultFontSubstitution.DefaultFontName = "Calibri";

This was not an issue with older version of Words, now we’re using version 24.8.0.
Could you help resolve this issue?

@johnAxe

It seems that you are experiencing issues with Aspose.Words defaulting to the Times New Roman font even when you specify Calibri in your code. This can happen due to several reasons related to font availability and substitution settings.

Here are some steps you can take to ensure that your specified font is used correctly:

  1. Font Settings Configuration: Make sure you are configuring the font settings properly. You can create an instance of FontSettings and specify the default font. If the specified font is not found, Aspose.Words will substitute it with the closest available font on your system.

    Here’s an example of how to set the default font:

    FontSettings.DefaultInstance.SubstitutionSettings.DefaultFontSubstitution.DefaultFontName = "Calibri";
    
  2. Check Font Availability: Ensure that the Calibri font is installed and accessible on your machine. Sometimes, even if the font appears to be installed, there may be issues with its accessibility.

  3. Font Substitution Mechanism: Aspose.Words uses a font substitution mechanism when the specified font cannot be found. You can check for font substitution warnings by implementing the IWarningCallback interface. This will help you identify if and why a substitution is occurring.

  4. Clear Formatting: You mentioned trying builder.Font.ClearFormatting(). This is a good approach to ensure that no previous formatting is affecting the font settings. Make sure to call this method before setting the font name.

  5. Version Changes: Since you mentioned that this issue did not occur in older versions of Aspose.Words, it might be worth checking the release notes for version 24.8.0 to see if there are any changes related to font handling that could affect your implementation.

By following these steps, you should be able to resolve the issue of Aspose.Words defaulting to Times New Roman instead of Calibri. If the problem persists, consider reaching out to the Aspose community for further assistance.

Sources:
[1]: Specify Default Font When Rendering - Aspose
[2]: Set Font Fallback Settings | Aspose.Words Document Processing API

I can try this FallBack…
But the main problem here is that I can’t specify fonts and change/use them where needed…
What would be the rule and range for Calibri, using font size 10 in an xml file?

<FontFallbackSettings xmlns="Aspose.Words">
    <FallbackTable>
        <Rule Ranges="0B80-0BFF" FallbackFonts="Vijaya"/>
        <Rule Ranges="1F300-1F64F" FallbackFonts="Segoe UI Emoji, Segoe UI Symbol"/>
        <Rule Ranges="2000-206F, 2070-209F, 20B9" FallbackFonts="Arial" />
        <Rule Ranges="3040-309F" FallbackFonts="MS Gothic" BaseFonts="Times New Roman"/>
        <Rule Ranges="3040-309F" FallbackFonts="MS Mincho"/>
        <Rule FallbackFonts="Arial Unicode MS"/>
    </FallbackTable>
</FontFallbackSettings>

@johnAxe Unfortunately, I cannot reproduce the problem on my side. Here is a simple code I used for testing:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Font.Name = "Calibri";
builder.Writeln("Some calibri text");
builder.StartTable();
builder.InsertCell();
builder.Write("cell_value");
builder.EndRow();
builder.EndTable();
doc.Save(@"C:\Temp\out.docx");

Here is the produced output: out.docx (7.2 KB)

Could you please provide your output document and code you use to produce it? We will check them and provide you more information.

I tried the above and the fallback setting suggestion (separately), it still didn’t work. Document does have Calibri font displaying everywhere else correctly (so this is not font is inaccessible issue), except for this table.

@johnAxe Could you please attach your output document and provide runnable code that will allow us to reproduce the problem? We will check your scenario on our side and provide you more information.

I can’t include the document, documents generated can’t be shared with unauthorized parties. The code base is very large, and includes parts that can’t be shared with unauthorized parties, such as DB connections , licenses etc. I clearly see Calibri 10 everywhere in the document except for texts inserted in this table with code piece you see.

@johnAxe Could you please create a code example and the document using dummy data, just to demonstrate the problem?

So you want me to rewrite the entire code base to get to this table?

@johnAxe We need the code that will allow us to reproduce the problem on our side. Unfortunately, using the provided information we cannot reproduce the problem on our side. As shown above the font is properly set in the generated document.

Can we setup a brief zoom meeting to go over the code as I cannot share the entire code base, nor have the time to rewrite the entire code base…?

@johnAxe Unfortunately, we do not provide technical support via phone or video calls. The main place for getting support is this forum.

Also, why was this not a problem with older version of Aspose (20.3.0) with exact the same code base, but suddenly is with 24.8.0?

@johnAxe I am afraid, it is difficult to say what might cause the problem on your side without ability to reproduce the problem on our side.

It turns out this is happening at builder.InsertHtml(cell_value) line of the code, rather than at builder.InsertCell() and builder.Write(cell_value)

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Font.Name = "Calibri";
builder.StartTable();
cell_value = "<b>Some text</b>";
builder.InsertHtml(cell_value);
builder.EndRow();
builder.EndTable();
doc.Save(@"C:\Temp\out.docx");

At the time of builder.InsertHtml(cell_value), builder object is showing that builder.Font is Calibri.

Is there some changes with InsertHtml(...) that we’re not aware of?

I resolved this issue: builder.InsertHtml(cell_value, true);
Here, useBuilderFormatting needed to be set to true.
Please refer to this when your clients are having similar issue.

@johnAxe You are right, you should use another overload of InsertHtml method and pass useBuilderFormatting parameter as ture:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Font.Name = "Calibri";
builder.StartTable();
builder.InsertCell();
string cell_value = "<b>Some text</b>";
builder.InsertHtml(cell_value, true);
builder.EndRow();
builder.EndTable();
doc.Save(@"C:\Temp\out.docx");