Why can't I set the font of a Phrase?

Why doesn't setting the font of a Phrase after the construction of the object work?

25th October 2015
admin-marketing

I'm starting with iTextSharp and wondering if there is any reason why setting the font of a Phrase after the construction of the object doesn't work. Is there any reason, do I miss something?

 

iTextSharp.text.Font f = PdfFontFactory.GetComic();
f.SetStyle(PdfFontStyle.BOLD);
Color c = Color.DarkRed;
f.SetColor(c.R,c.G,c.B);
f.Size = 20;
Phrase titreFormules = new Phrase("Nos formules",f); //THIS WORKS
// titreFormules.Font = f; // THIS DOESN'T WORK!
document.Add(titreFormules);
document.Close();

 

Posted on StackOverflow on Sep 11, 2014 by Alain BUFERNE

When you use setFont(), you change the font of the Phrase for all the content that is added after the font was set.

  • A Chunk is an atomic part of text in the sense that all the text in a Chunk has the same font family, font size, font color,...
  • A Phrase is a collection of Chunk objects and as such a Phrase can contain different 'atoms' of text using different fonts.

In your example "Nos formules" will be written in Helvetica. You change the font after the Helvetica Chunk with the text "Nos formules" was added to the Phrase. As you didn't add anything else to titreFormules, the font "Comic" is never used.



Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now