HTML Fragment Font Family Issues

Hello,
I’m currently having an issue inserting an html fragment into a pdf with a desired font family.
Below is my code and under that is the error I get.

        // Create PDF file
        Document doc = new Document();

        // Add page
        Page page = doc.Pages.Insert(1);

        HtmlFragment title = new HtmlFragment("Document Name 0");
        TextState textState = new TextState();
        textState.Font = FontRepository.FindFont("Helvetica");
        title.TextState = textState;
        page.Paragraphs.Add(title);

        // Save the updated document
        doc.Save("generatedtest.pdf");

System.InvalidCastException: ‘Unable to cast object of type ‘#=zj3u_Unz$MOQSdLv_sGX2lGd1bD12’ to type ‘#=zVe4SU6do6sXUhKleV4jybHqr08Jb’.’

@cmacgregor

We tested the scenario at our end using Aspose.PDF for .NET 21.4 and did not notice any issue. The code executed fine and PDF was generated successfully. For your kind reference, generated file is also attached.
generatedtest.pdf (11.8 KB)

Would you please confirm if you are using the latest version of the API? Also, please make sure that target font is present/installed in the system. In case issue still persists, please share your complete environment details i.e. OS Name and Version, Application Type, .NET Framework Version, etc. with us. We will again test the scenario at our end and share our feedback with you accordingly.

I did not have helvetica installed on my machine. Now that I do I’m not getting the error on my local test case, but In our product we use docker linux containers and I am not seeing the correct font when using HTML fragments. Currently, I am running .NET 21.4.0 and .NET 21.1.0 of your product. Are there known issues with this functionality on these versions?

Using text fragments in the below code the correct font is displayed, but the HTMLFragment does not display the correct font.

var tocDescriptionhtml = new HtmlFragment("<div>Document Name 0</div>")
{
	TextState = new TextState
	{
		Font = FontRepository.FindFont("Bell MT"),
	}
};
var tocDescription = new TextFragment("Document Name 0")
{
	TextState =
	{
		Font = FontRepository.FindFont("Bell MT"),
	}
};
tocPage.Paragraphs.Add(tocDescriptionhtml);
tocPage.Paragraphs.Add(tocDescription);

@cmacgregor

Could you please make sure that all necessary fonts are present in the Linux container? Please make sure to do the following and let us know if you still face any issue by sharing the sample output file generated at your end:

  • Install package with Microsoft compatible fonts: ttf-mscorefonts-installer. (e.g. sudo apt-get install ttf-mscorefonts-installer) These fonts should be placed in “/usr/share/fonts/truetype/msttcorefonts” directory as Aspose.PDF for .NET scans this folder on Linux like operating systems. In case operating system has other default folder/directory for fonts, you should use following line of code before performing any operation using Aspose.PDF.
Aspose.Pdf.Text.FontRepository.Sources.Add(new FolderFontSource("<user's path to ms fonts>"));