Default font on PDF

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF CoreSuite for .NET (v11)  /  Default font on PDF

DynamicPDF CoreSuite for .NET (v11) Forum

 Nov 03 2021 3:50 AM
Hi Team,

I have requirement to change default font on PDF. As I am using some place HTML area and not providing any font style on that.
So is it possible to provide default setting for font.

Thanks ,
Chandan Singh
 Nov 03 2021 3:21 PM
Posted by a ceTe Software moderator
Hi,

Currently, there is no way to set a default font for the document along with HtmlArea. HtmlArea will have its own default font. You will have to specify the font in the HTML/CSS. If no font is specified then Arial will be used by default in HtmlArea. We may have an option to set the default fonts in HtmlArea in a future build, however, we do not have any specific timeline on this.

Thanks,
ceTe Software Support Team.
 Dec 10 2021 3:19 PM
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
 Dec 13 2021 8:16 AM
Hi Team,

I tried your code with my sample application, but its not working.

Expected Font : TimeNewRoman
Actual Font: LiberationSans

-------------Sample Code-----------------------
using ceTe.DynamicPDF;
using ceTe.DynamicPDF.PageElements.Html;
using System;

namespace ConsolePDF
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            PageDimensions dimensions = new PageDimensions(PageSize.Letter, PageOrientation.Portrait, 72f);
            ceTe.DynamicPDF.Page page = new ceTe.DynamicPDF.Page(dimensions);

            var testString = "<HTML><BODY><P align = center>This is my test sample</BODY></HTML>";
            Document document = new Document();
            HtmlArea htmlArea = new HtmlArea(testString, 0, 0, 530, 580);
            htmlArea.FontSelecting += new HtmlAreaFontSelecting(htmlArea_FontSelecting);
            do
            {
                Page page1 = new Page();
                page1.Elements.Add(htmlArea);
                document.Pages.Add(page1);
                htmlArea = htmlArea.GetOverflowHtmlArea();
            } while (htmlArea != null);
            string output = @"C:\Temp\CustomFontForHtmlArea.pdf";
            document.Draw(output);
        }

        static void htmlArea_FontSelecting(object sender, FontSelectingEventArgs e)
        {
            if (e.FontStyle == CssFontStyle.Normal && e.FontWeight < 700)
            {
                e.Font = Font.TimesRoman;
            }
        }
    }
}

Thanks,
Chandan Singh
 Dec 22 2021 10:18 AM
Posted by a ceTe Software moderator
Hi,

We have an update from our development team and this issue is fixed in DynamicPDF Core Suite for .NET (v11.23). You can download latest version from NuGet(Package Id: ceTe.DynamicPDF.CoreSuite.NET) or from our website  here

Thanks,
ceTe Software Support Team

All times are US Eastern Standard time. The time now is 10:50 PM.