Print Details of What Fonts are Present in a Word DOCX Document using C# .NET Code | FontInfos

Hello,
I work with Aspose Words .NET in version 20.10
When I use the “FontInfos” function to retrieve the fonts used on the document, I end up with other fonts.
On this example, I only use the “Arial” “Font” and yet with “fontInfos”, I get 4 “Calibri, Times New Roman, Arial, Calibri Ligth” fonts. Is there a solution to only have the “Font” used?

Here is my code
var docInfo = new Document(Path.Combine(folder, fileName));
var listeFont = docInfo.FontInfos;

and the file
TestFont.zip (8.6 KB)

Thanks for your help
Sylvain

@SylvainAxonCable,

You can use the following C# code of Aspose.Words for .NET API to print the details of what fonts are present in a Word document.

Document doc = new Document("C:\\Temp\\TestFont\\TestFont.docx");

FontInfoCollection fonts = doc.FontInfos;
// The fonts info extracted from this document does not necessarily mean that the fonts themselves are
// used in the document. If a font is present but not used then most likely they were referenced at some time
// and then removed from the Document
for (int i = 0; i < fonts.Count; i++)
{
    Console.WriteLine($"Font index #{i}");
    Console.WriteLine($"\tName: {fonts[i].Name}");
    Console.WriteLine($"\tIs {(fonts[i].IsTrueType ? "" : "not ")}a trueType font");
}

For the sake of any correction in Aspose.Words API, we have logged this problem in our issue tracking system with ID WORDSNET-21442. We will further look into the details of this problem and will keep you updated on the status of correction. We apologize for any inconvenience.

Hello,
Thank you for your reply.
The problem is normal for most of the documents, but I created the document. And I didn’t remove any font, I just put “Arial”.

So I will be able to understand for “Arial” and “Calibri Ligth”.
But I’m looking for a function to just find the fonts used in the document, not find all the fonts that are passed.

Thank you
Sylvain

@SylvainAxonCable,

We have logged these details in our issue tracking system and will keep you posted here on any further updates.

Thank you so much
Sylvain

Hello,
Is there a solution to get that “Fonts” used on the words document?
With Excel, I can check the “Fonts” cell by cell.
It may be long, but fair.
Is there an almost identical system on Word?
Thank you
Sylvain

@SylvainAxonCable,

Text in Word document is represented by Run nodes. You can determine Font names of Run nodes by using the following code.

Document doc = new Document("C:\\Temp\\input.docx");

ArrayList listOfFonts = new ArrayList();
foreach (Run run in doc.GetChildNodes(NodeType.Run, true))
{
    string fontName = run.Font.Name;
    if (!listOfFonts.Contains(fontName))
        listOfFonts.Add(fontName);
}

foreach (string fontName in listOfFonts)
    Console.WriteLine(fontName);

Thank you
Sylvain

@SylvainAxonCable,

Regarding WORDSNET-21442, we have completed the analysis of this issue and concluded to close this issue with “not a bug” status. The analysis reveals the following:

DocumentBase.FontInfos contains fonts not necessary explicitly set in a document. It can be implicitly referenced inside document, for example, in styles. For this particular document we can check, that corresponding fontTable.xml contains the same fonts as FontInfos:

<w:fonts>
  <w:font w:name="Calibri">
  <w:font w:name="Times New Roman">
  <w:font w:name="Arial">
  <w:font w:name="Calibri Light">
</w:fonts>

So, you can continue using the previous code snippet to achieve desired result.