get_FontInfos is falling back to the default fonts of a blank document for a document generated with Aspose

I am generating a simple document with Aspose.Words with few different fonts and I am trying to get the fonts information for the existing fonts in the document. Here is a sample code:

  auto doc = MakeObject<Document>();
  auto builder = MakeObject<DocumentBuilder>(doc);
  auto font = builder->get_Font();
  font->set_Name(u"Liberation Mono");
  builder->Writeln(u"This is a Liberation Mono paragraph");
  font->set_Name(u"Courier New");
  builder->Writeln(u"This is a Courier New paragraph");

  auto font_collection_enumerator = doc->get_FontInfos()->GetEnumerator();
  while (font_collection_enumerator->MoveNext()) {
    auto font_info = font_collection_enumerator->get_Current();
    std::cout << font_info->get_Name() << std::endl;
  }
}

The issue is that get_FontInfos is only returning the default fonts of a blank document (Arial, Symbol ,Times New Roman) and not detecting any of the fonts in the document. Please advise

@Mike1992 Aspose.Words does not update FontInfos collection. This collection of font definitions is loaded as is from the document. Font definitions might be optional, missing or incomplete in some documents.

Do not rely on this collection to ascertain that a particular font is used in the document. You should only use this collection to get information about fonts that might be used in the document.

Thanks @alexey.noskov for the quick response.

  1. Is there any way or workaround to set the FontInfos collection for a document that is generated with Aspose?
  2. Do you have any recommendations for a better way to assert that a particular font is used in the document?

@Mike1992 As a simple solution, you can use IWarningCallback and UpdatePageLayout method. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Font.Name = "Arial";
builder.Write("Arial");
builder.Font.Name = "Calibri";
builder.Write("Calibri");
builder.Font.Name = "Times New Roman";
builder.Write("Times New Roman");
builder.Font.Name = "Liberation Mono";
builder.Write("Liberation Mono");
builder.Font.Name = "Courier New";
builder.Write("Courier New");

// Set no font sources, so all fonts are substituted to make Aspose.Words to warn
doc.FontSettings = new FontSettings();
doc.FontSettings.SetFontsSources(new FontSourceBase[] { });
doc.WarningCallback = new WarningCallback();
doc.UpdatePageLayout();
private class WarningCallback : IWarningCallback
{
    public void Warning(WarningInfo info)
    {
        if (info.WarningType == WarningType.FontSubstitution)
            Console.WriteLine(info.Description);
    }
}

This code returns the following output:

Font 'Arial' has not been found. Using 'Fanwood' font instead. Reason: first available font.
Font 'Calibri' has not been found. Using 'Fanwood' font instead. Reason: first available font.
Font 'Times New Roman' has not been found. Using 'Fanwood' font instead. Reason: first available font.
Font 'Liberation Mono' has not been found. Using 'Fanwood' font instead. Reason: first available font.
Font 'Courier New' has not been found. Using 'Fanwood' font instead. Reason: first available font.

As another option is using DocumentVisitor to go through all nodes in the document and get font used for each node.