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
-
I’ve verified that the font file exists at the specified path.
-
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.