Using html bold tag in FormattedTextAre

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF Generator for .NET (v4)  /  Re: Using html bold tag in FormattedTextAre

DynamicPDF Generator for .NET (v4) Forum

I need to create a pdf where some part of the string is bold and some is regular. For this I use the <b> and </b> tags in the text for a FormattedTextArea but this only works if I use ceTe.DynamicPDF.FontFamily.Times or any of the other fonts in ceTe.DynamicPDF.FontFamily.

If I create an OpenTypeFont, the tags are ignored. The code I am using is given below -

FormattedTextAreaStyle style = new FormattedTextAreaStyle(ceTe.DynamicPDF.FontFamily.Times, 10, true);
                        FormattedTextArea textArea = new FormattedTextArea("<b><i>Bold: </i></b> Regular", 0, 0, 100, 100, style);
                        textArea.Height = textArea.GetRequiredHeight();

                        System.Drawing.Font tempFont = new System.Drawing.Font("Microsoft Sans Serif", 10);
                        ceTe.DynamicPDF.Font ceTeFont = OpenTypeFont.LoadSystemFont(tempFont) as OpenTypeFont;
                        if (ceTeFont == null)
                                return;

                        ceTe.DynamicPDF.FontFamily fontFamily = new ceTe.DynamicPDF.FontFamily(ceTeFont.Name,
                                ceTeFont);
                        if (fontFamily == null)
                                return;
                        FormattedTextAreaStyle style1 = new FormattedTextAreaStyle(fontFamily, 10, true);

                        FormattedTextArea textArea1 = new FormattedTextArea("<b><i>Bold: </i></b> Regular", 0,
                                textArea.Height + 10, 100, 100, style1);
                        textArea1.Height = textArea1.GetRequiredHeight();

                        Document document = new Document();
                        Page page = new Page();
                        page.Elements.Add(textArea);
                        page.Elements.Add(textArea1);

                        document.Pages.Add(page);
                        string pdfFileName = @"c:\temp\test.pdf";
                        document.Draw(pdfFileName);
Posted by a ceTe Software moderator
Hello,

You can add text where some part of it is bold using FormattedTextArea without any problem. You will need to use a font which supports bold style and create TrueTypeFont object and then add this font to FontFamily. You can use this font family to set the style for FormattedTextArea. Below is the sample code for it.

            //MiCrosoft Sans Serif regular font.
            TrueTypeFont regularFont = new TrueTypeFont(@"C:\WINDOWS\Fonts\REFSAN.TTF");
            //MiCrosoft Sans Serif Bold font.
            TrueTypeFont boldFont = new TrueTypeFont(@"C:\WINDOWS\Fonts\REFSANB.TTF");
            //MiCrosoft Sans Serif speciality font.Similarly you can use font which supports italic style.
            TrueTypeFont specialityFont = new TrueTypeFont(@"C:\WINDOWS\Fonts\REFSPCL.TTF");
            FontFamily fontFamily = new FontFamily("MSSansSerif", regularFont, boldFont, specialityFont, specialityFont);
           
            FormattedTextAreaStyle style1 = new FormattedTextAreaStyle(fontFamily, 10, true);
            FormattedTextArea textArea1 = new FormattedTextArea("<b>Bold: </b> Regular", 0,100, 200, 100, style1);
            textArea1.Height = textArea1.GetRequiredHeight();

            Document document = new Document();
            Page page = new Page();
            page.Elements.Add(textArea1);
            document.Pages.Add(page);
            string pdfFileName = @"c:\temp\test.pdf";
            document.Draw(pdfFileName);

Thanks,
ceTe Software Support Team.

          

All times are US Eastern Standard time. The time now is 2:15 AM.