HTMLFormatProvider -> use custom font for imported html string

1 Answer 322 Views
PdfProcessing
Fabian
Top achievements
Rank 1
Fabian asked on 18 Jan 2023, 09:50 AM

Hi!

How can I make a custom font work, when importing a HTML string and printing it to pdf? Im loosing the "ÄÖÜ" characters currently and know that this is a font related issue.

 

var htmlDocument = new Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider();

// Regular Font
byte[] fontData = System.IO.File.ReadAllBytes("fonts/verdana.ttf");
Telerik.Documents.Core.Fonts.FontFamily fontFamily = new Telerik.Documents.Core.Fonts.FontFamily("CustomVerdana");
FontsRepository.RegisterFont(fontFamily, FontStyles.Normal, FontWeights.Normal, fontData);

var doc = htmlDocument.Import("<html><body style=\"font-family:CustomVerdana;\">Das ist ein test. <br> <strong>ÖOÜUÄA</strong></body></html>");

var fileName = "output.pdf";
var pdf = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();
using (Stream output = System.IO.File.OpenWrite(fileName))
{
      pdf.Export(doc, output);
 }

1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 18 Jan 2023, 11:59 AM

Hello Fabian,

Your approach is correct. Please make sure that the font family name matches the name from the font file. And the font actually contains the desired characters. 

I hope this helps. Should you have any other questions do not hesitate to ask.

Regards,
Dimitar
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Fabian
Top achievements
Rank 1
commented on 19 Jan 2023, 11:31 AM

Hey Dimitar,

thank you for the quick response. I changed the name to be equal to the font file name. Still does not work, the letters "äöü" get lost. The font same font works when i .insertText() and provide the font as an argument. I have isolated the necessary code and hope you can find the issue

 

Dimitar
Telerik team
commented on 19 Jan 2023, 01:25 PM

Hi Fabian, 

Thank you for the project.

Please note that the font family is case-sensitive and it must start with capital "V" in this case. 

In addition, the text in the HTML is bold, and this means that you need to register a bold font as well. Usually, separate font files are used for the bold characters. 

I hope this helps. Should you have any other questions do not hesitate to ask.

Regards,

Dimitar

Fabian
Top achievements
Rank 1
commented on 20 Jan 2023, 06:10 AM | edited

Thanks a lot Dimitar, it works now!

Just one more question. How can i insert the html string which is currently exported to .pdf into a RadFixedDocument table cell block? For better understanding i have attached a screenshot of the layout. The whole document is a table with two columns and i want to insert the imported html into the main content table cell, while keeping the html formatting

Dimitar
Telerik team
commented on 20 Jan 2023, 02:03 PM

Hi Fabian, 

The only way to achieve this would be to create a RadFlowDocument first and then convert it to PDF. This way you can import the HTML and insert it into the document. Detailed information is available in the following articles:

Let me know if I can assist you further.

Regards,

Dimitar

 

Tags
PdfProcessing
Asked by
Fabian
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or