Apply Custom Font in HtmlFragment in PDF

I am adding custom font family with HtmlFragment in Aspose.pdf for .NET using below Code. It loads the font from a file using the FontRepository.OpenFont function, but the font doesn’t seem to be applied to the text in the HtmlFragment. The code creates a new document, adds a new page, sets the font file path and creates an HtmlFragment with a text “this is text”. It then sets the TextState of the HtmlFragment with the font obtained using the FontRepository.OpenFont method, and finally adds the HtmlFragment to the page and saves the document as a PDF file. Please let me know if I missing to add any property to apply font in document.

var _dataDir = @"C:\Users\akash.choubey\";
Document doc = new Document();
Page page = doc.Pages.Add();
var fontPath = _dataDir + "astmensh.otf";
HtmlFragment hf = new HtmlFragment("<p>this is text</p>");
hf.TextState = new TextState();
hf.TextState.Font = FontRepository.OpenFont(fontPath);
page.Paragraphs.Add(hf);
doc.Save(_dataDir + "output.pdf");

Things that I have tried

  1. I’ve verified that the font file exists at the specified path.

  2. I’ve also tried using a built-in font such as Times New Roman instead of a custom font using FontRepository.FindFont and that works fine.

@Jitesh.jain,

Since I do not have the font is hard for me to replicate the problem. But can you try by also setting the font name?

hf.TextState.Font.FontName = “your font name here”;

I am using Nuget Aspose.pdf latest stable 23.3.0.

Please see attached font fileAlkatra-VariableFont_wght.zip (347.0 KB)

@Jitesh.jain,

I am going to create a ticket, because I am not able to select the font even though I have it installed correctly.

@Jitesh.jain
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-54097

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.