Using custom fonts with HtmlFragment

Hi,

I am creating a PDF with Aspose.PDF for .NET using a custom font which I am assigning to the default text state of my output document. Some example code of this is below.

var defaultTextState = new TextState();
defaultTextState.Font = FontRepository.OpenFont("~\Content\fonts\OpenSans-Regular.ttf");
defaultTextState.FontSize = 11;
defaultTextState.LineSpacing = 4;

outputDocument.PageInfo.DefaultTextState = defaultTextState;

This works fine and the font is being applied whenever I use a TextFragment.
However, when my input text has HTML tags, I have to use HtmlFragment instead to parse the HTML, but in these cases, it seems that the custom font does not apply.

I have tried to set the font directly on the text state of the HtmlFragment like the code below but this doesn’t work either.

var aboutTheMeasure = new HtmlFragment(data.Indicator.AboutThisMeasure);
aboutTheMeasure.TextState = new TextState();
aboutTheMeasure.TextState.Font = FontRepository.OpenFont("~\Content\fonts\OpenSans-Regular.ttf");
page.Paragraphs.Add(aboutTheMeasure);

The text seems to default to Times New Roman instead of using the custom font. I have noticed that if I change to a regular font e.g. FontRepository.OpenFont(“Calibri”); then this works. It only doesn’t work if it’s a custom font.

Is there any way to get this working or are custom fonts not supported with HtmlFragment?
Additionally, I have found that the PDF generation time is greatly increased when using HtmlFragments vs only using TextFragments. Is there a way to speed this up?

Also I have another question: is it possible to create unordered and ordered bullet lists with the Aspose DOM approach? I haven’t found a way to do this in the documentation.

Thanks,
David

@davidpbtran

Thanks for contacting support.

We were able to replicate the issue in our environment that API was unable to set custom font in case of HtmlFragment. Hence, we logged this issue under the ticket ID PDFNET-45225 in our issue tracking system. We will further investigate the issue and keep you informed with the status of its correction. Please be patient and spare us little time.

As a workaround, you can install custom fonts in your machine and set them in the code using following line. This way it can be set for HtmlFragment.

hf.TextState.Font = FontRepository.FindFont("Open Sans Extrabold");

The delay in PDF generation is expected as API renders HTML content inside PDF document and saves it with all resources (i.e. Fonts Embedding, Images Used, Graphics, etc.). However, if in your some cases it is taking so much time and causing performance ahead, you can please share complete scenario details with us. We will test the scenario in our environment and address it accordingly. Please also share your complete environment details along with it.

You can render ordered and un-ordered lists using HtmlFragment inside PDF document. Please check following code snippet and attached output document.

Document doc = new Document();
Page page = doc.Pages.Add();
TextFragment tf = new TextFragment("This is default font");
tf.TextState.Font = FontRepository.FindFont("Times New Roman");
page.Paragraphs.Add(tf);
HtmlFragment hf = new HtmlFragment("<ul><li>List item</li><li>List item</li></ul><ol><li>List item</li><li>List item</li></ol>");
hf.TextState = new TextState();
hf.TextState.Font = FontRepository.FindFont("Open Sans Extrabold");
page.Paragraphs.Add(hf);
doc.Save(dataDir + "HtmlFragmentwithCustomFont.pdf");

As soon as we have some definite updates regarding logged issue resolution, we will let you know.

We are sorry for the inconvenience.

Hi,

Thank you for the response.
Is there any way to create bullet lists without HtmlFragments? Currently I can get the custom font working with TextFragments but I need to be able to create bullet lists with the custom font as well.

@davidpbtran

Thanks for getting back to us.

Please use following code snippet to add bullet lists with TextFragments:

String outFile = dataDir + "bulletlist.pdf";
//Input data:
List<String> bulletList = new List<String>();
bulletList.Add("List Item");
bulletList.Add("List Item");

System.Text.Encoding Encoder = System.Text.ASCIIEncoding.Default;
Byte[] buffer = new byte[] { (byte)149 };
string bullet = Encoding.GetEncoding(1252).GetString(buffer);

// Instantiate PDF object by calling its empty constructor
Document doc = new Document();
// Create a section in the PDF object
Page page = doc.Pages.Add();

// Create dash text
for (int i = 0; i < bulletList.Count; i++)
{
  TextFragment bulletFragment = new TextFragment(bullet + " " + bulletList[i]);
   bulletFragment.TextState.Font = FontRepository.FindFont("Arial");
   page.Paragraphs.Add(bulletFragment);
}
doc.Save(outFile); 

bulletlist.pdf (33.2 KB)

Hi,

Could I please get an ETA and a status update for PDFNET-45225 ?

Thanks,
David

@davidpbtran

Thanks for your inquiry.

I am afraid that earlier logged ticket is pending for review due to other pending issues in the queue. Please note that the issue has been logged under free support model and will be investigated/resolved on first come first serve basis. As soon as we have some definite updates regarding issue resolution, we will definitely let you know. Please spare us little time.

Furthermore, you can also check our paid support option where issues are resolved on urgent basis. You can also report your issue there in case you need the ticket to get resolved on priority basis.

Hi Asad.ali
I am also seeing this same problem - HTMLFragment not recognising the embedded and assigned open type font and rendering default Times NR. The work around for this with other functions was to convert the pdf file to PDF 2A and set to not editable. This is still not working in this case; as explained by David.
Can you please escalate this request.
Regard,
Brad

@thegordon

Thanks for contacting support.

We have recorded your concerns and will definitely consider them while investigating the issue. We will let you know as soon as additional updates are available regarding issue fix. Please spare us little time.

We are sorry for the inconvenience.

Thanks.

If it helps, my investigation seems to show that the TextState property of the HtmlFragment does behave differently than, say, TextFragment.

For example:

  • Direct assignment of the “.TextState” results in an object instance error - this is not the case with TextFragment where direct assignment of values is valid.
  • With HTMLFragment, you must first instantiate an instance of the TextState class, populate it’s parameters, then assign this instance to the TextState of the HTMLFragment object.
  • The “.fontSize” attribute also behaves differently. A font size of 8 in TextFragment appears is equivalent to a font size of 2 in HTMLFragment
  • The “.foregroundcolor” attribute does not appear to work at all. All text is still rendered as black regardless of RGB equivalent used.
  • And of course, the above font assignment only accepts FontRepository.FindFont of standard core fonts, not FontRepository.OpenFont for OT font files.

I am aware that issues are addressed on a case by case basis, but this is an important requirement and appears to be a bug and runs counter to the product claim of support for OT fonts, regardless of method used. Also It’s now December and this was logged by David in August.

I have found it necessary to work around the issues of support for OT fonts for some time now by literally disassembling the HTML into text fragments - an error prone process that shouldn’t be necessary. (I first raised issue with Aspose.PDF OT font support late 2016 and it appears that this is still an ongoing problem. PS: I am using version 18.12.0.0.)

Your prompt assistance is appreciated.

Cheers,
Brad

@thegordon

Thanks for providing these details. This information would definitely be helpful during investigation process. As soon as some significant progress is made towards issue resolution, we will let you know.

Could you please share the issue ID or Thread URL where you had reported similar issue so that we link that with this thread as well and proceed accordingly.

The earliest post I could find was this one; it being a thread I created after giving up on using HTML import, then beginning the process of disassembling HTML into TexrFragments. I do not know the previous ID as it’s not on my list… Open Type font embedding problem

@thegordon

Thanks for providing further details.

We have checked the thread shared by you and observed that some of findings against previous investigation were shared there. However, in case you still have different concerns in that reference, please share with us. We will continue the investigation process where it was left.

Any further progress on this one?

@thegordon

I regret to share that there is no significant updates are available regarding issues resolution due to large number of pending issues in the queue. Please note that the issues were logged under free support model thus, have low priority and will be investigated/resolved on first come first serve basis. We will surely let you know as soon as some significant progress is made towards issues rectification. Please spare us little time.

We are sorry for the inconvenience.

This issue was logged originally in August - I’m a bit concerned about a commercial product being fit for purpose or use if there are so many issues that an obvious bug cannot be addressed within 5 months regardless of how it was logged. I will discuss our refund options with our local AU reseller - this ongoing issue with OT font support has been a constant annoyance so we will be looking at alternative solutions going forwards.

@thegordon Good luck getting this resolved mate! We never ended up completing our requirement but fortunately it was low priority for us. I agree with you though that this probably shouldn’t be taking 5 months. Best of luck to you.

@asad.ali Is there any reason that this post has been set to private? I think it would be useful for anyone having similar issues to know that this is an actual bug within the product, that it has already been reported, and is being worked on.

Cheers,
David

@thegordon

We really apologize for the inconvenience and delay in resolution of the issue. Please note that we do value your concerns and for that we have raised the priority of the issue to escalate things. We will definitely take your issue in account for attention and as soon as we have some investigation results and news for ETA, we will share with you. Please spare us little time

@davidpbtran

We have made this thread public and yes, the investigation of earlier logged issue is under process and we will be sharing updates within this thread for the issue resolution as soon as we have some.

We are sorry for the inconvenience caused.

The issues you have found earlier (filed as PDFNET-45225) have been fixed in Aspose.PDF for .NET 19.3.

Hello, i’ve download your examples from github (GitHub - aspose-pdf/Aspose.PDF-for-.NET: Aspose.PDF for .NET examples, plugins and showcase projects) and seems that this bug is still not fixed in version Aspose.PDF 19.6.0.

I’ve edited the SetHTMLStringFormatting example as below:

            var html = new HtmlFragment("HTML")
            {
                TextState = new TextState
                {
                    Font = FontRepository.OpenFont(@"C:\OpenSans-Regular.ttf"),
                    ForegroundColor = Color.Red,
                    BackgroundColor = Color.Blue,
                    FontStyle = FontStyles.Bold,
                    FontSize = 20
                }
            };

            var text = new TextFragment("TEXT")
            {
                TextState =
                {
                    Font = FontRepository.OpenFont(@"C:\OpenSans-Regular.ttf"),
                    ForegroundColor = Color.Red,
                    BackgroundColor = Color.Blue,
                    FontStyle = FontStyles.Bold,
                    FontSize = 20
                }
            };

            var doc = new Document();
            var page = doc.Pages.Add();
            page.Paragraphs.Add(html);
            page.Paragraphs.Add(text);

            doc.Save(@"C:\test.pdf");

The result is:

test.pdf (87.6 KB)

Any suggestion?

@boardlab

It seems like this is a regression as the issue was resolved in 19.3 version of the API and is still occurring in 19.6. We have created a new regression issue as PDFNET-46643 in our issue tracking system for the sake of correction. We will surely look into details of the issue and keep you posted with the status of its correction. Please be patient and spare us little time.

We are sorry for the inconvenience.