Posted by a ceTe Software moderator
Hello,
We have an update from our development team, and they have implemented a feature to set a custom fonts in an HtmlArea object in latest release, DynamicPDF Core Suite for .NET version 11.22. You can download latest version from NuGet (Package ID: ceTe.DynamicPDF.CoreSuite.NET) or from our website
here.
Please note that, if you don't specify a font for the HtmlArea and if the the HTML source does not have any font specified, then it will add content with the LiberationSans-Regular font (default font for HtmlArea).
You can set a custom font using the FontSelecting event of HtmlArea.
Here is a code sample:
Document doc = new Document();
PageDimensions dimensions = new PageDimensions(PageSize.Letter, PageOrientation.Portrait, 72f);
ceTe.DynamicPDF.Page page = new ceTe.DynamicPDF.Page(dimensions);
string filePath = @"C:\Temp\Test.html";
Uri url = new Uri(filePath);
Document document = new Document();
HtmlArea htmlArea = new HtmlArea(url, 0, 0, 530, 580);
htmlArea.FontSelecting+=new HtmlAreaFontSelecting(htmlArea_FontSelecting);
do
{
Page page = new Page();
page.Elements.Add(htmlArea);
document.Pages.Add(page);
htmlArea = htmlArea.GetOverflowHtmlArea();
} while (htmlArea != null);
string output = @"C:\Temp\CustomFontForHtmlArea.pdf";
document.Draw(output);
System.Diagnostics.Process.Start(output);
static void htmlArea_FontSelecting(object sender, FontSelectingEventArgs e)
{
if (e.FontFamily == "serif" || e.FontFamily.Contains("Times"))
{
if (e.FontStyle == CssFontStyle.Italic && e.FontWeight >= 700)
{
e.Font = Font.TimesBoldItalic;
}
else if (e.FontStyle == CssFontStyle.Italic)
{
e.Font = Font.TimesItalic;
}
else if (e.FontWeight >= 700)
{
e.Font = Font.TimesBold;
}
else
{
e.Font = Font.TimesRoman;
}
}
else if (e.FontFamily.Contains("Courier"))
{
if (e.FontStyle == CssFontStyle.Oblique && e.FontWeight >= 700)
{
e.Font = Font.CourierBoldOblique;
}
else if (e.FontStyle == CssFontStyle.Oblique)
{
e.Font = Font.CourierOblique;
}
else if (e.FontWeight >= 700)
{
e.Font = Font.CourierBold;
}
else
{
e.Font = Font.Courier;
}
}
}
Thanks,
ceTe Software Support Team