0

I'm starting with itextsharp and wondering if there is any reason why if I set the font of a phrase after the construction it 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;
 Document document = new Document();
 try
 {
    PdfWriter.GetInstance(document, new System.IO.FileStream("PhraseTest.pdf",   FileMode.Create));
    document.SetPageSize(PageSize.A4);
    document.Open();
    Phrase titreFormules = new Phrase("Nos formules",f); //THIS WORKS
    // titreFormules.Font = f; // THIS DOESN'T WORK!
    document.Add(titreFormules);
    document.Close();
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
Alain BUFERNE
  • 2,016
  • 3
  • 26
  • 37

1 Answers1

2

This is documented in my book.

  • 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.

This is also what I meant when I answered the question iText - PdfPTable doesn't show Cyrillic(Russian) symbols:

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

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • That such questions come so often, perhaps indicates that people want a functionality to change the fonts already used in a phrase instead of thinking of proper font-use beforehand. (And admittedly, a property or attribute of a `Phrase` class called `Font` *feels* like operating on the existing content, too.) – mkl Sep 12 '14 at 09:56
  • I understand better how it's working. But as mkl said the property push me to understand as there are constructor for Phrase and Paragraph without font parameter (even default font) I was thinking ok let get a phrase or paragraph with such text and set now it's property. Anyway it's working like that so no problem for me. Thanks for your clear answer – Alain BUFERNE Sep 13 '14 at 09:48