Non-existing- but unused fonts when converting to PDF

Hi!

I am developing an application where PDF documents are created on the server; the templates for the documents (Word files) are created and edited on the client side.

I want to prevent the user to use fonts in the Word document that are not installed on the server. To achieve that, I examine the Word file using Aspose.Words by going through all Runs in the document and collecting the used fonts. Afterwards I compare the found fonts to a list of fonts installed on the server. This generally works just fine.

Now I have a Word document (see attachment) that contains just one image. That image is contained in a paragraph that is formatted to use the font “Ubuntu”. But since there are no Runs in the document, it seems I am not able to find the font “Ubuntu” as being used.

Now it gets really weird. When converting the document to PDF on the server, the IWarningCallback is called, since the font “Ubuntu” is not installed on the server. But the converted PDF document does not even use any font.

As a workaround I thought about checking all Runs for fonts before conversion (just like on the client side) and ignoring all IWarningCallback calls for fonts that I have not found in Runs, assuming that these won’t have any effect on the generated PDF file.

My questions:

  1. How could I detect the font “Ubuntu” in the Word file? Or more generally: how can I really detect all used fonts? It seems there are more “places” in a Word document that have font settings…
  2. Why is the IWarningCallback being called for a font that is not even used in the resulting PDF?
  3. In the WarningInfo object there is no way to get to the name of the missing font other than parsing the Description string, which does not seem to be a clean solution. Could you implement something better in a future version?
  4. What do you think about my workaround idea? Will only fonts used in Runs be actually used in the generated PDF?

Thank you very much for your help!

David

Hi David,
As your document contains image(s) only and there are no runs you can loop through each shape (not only runs) to check which fonts are used in your document e.g.

int sectionIndex = 0;
foreach (Section sec in doc.Sections)
{
    NodeCollection shapes = sec.GetChildNodes(NodeType.Shape, true);
    foreach (Shape shape in shapes)
    {
        string fontName = shape.Font.Name;
    }
    sectionIndex++;
}

You can also use FontSettings.SetFontSubstitues to set replacements for missing fonts and also use default font as described here https://docs.aspose.com/words/net/manipulating-and-substitution-truetype-fonts/ set default font which will be used for missing fonts if no substitutes are specified.
Best Regards,