2

Using iTextSharp I have to fill a given rectangle with a text, both in height and width. The text can wrap, I have to find the correct fontsize for it to fill the rectangle.

wrapping the text is not an issue I have a recursive function that return an array of all possible wrapping configuration.

This is not a problem with gdi+, but using iTextSharp I have issues finding the height of my text once it wraps (i.e. contains "\n")

Any help?

I have found this article very usefull, it addess my problem from another point of view:

Centered, multiline text using iTextSharp ColumnText

ADDED: EXAMPLE CODE:

        string text = "Lorem Ipsum è un testo segnaposto utilizzato nel settore della tipografia e della stampa. Lorem Ipsum è considerato il testo segnaposto standard sin dal sedicesimo secolo, quando un anonimo tipografo prese una cassetta di caratteri e li assemblò per preparare un testo campione. È sopravvissuto non solo a più di cinque secoli, ma anche al passaggio alla videoimpaginazione, pervenendoci sostanzialmente inalterato. Fu reso popolare, negli anni ’60, con la diffusione dei fogli di caratteri trasferibili “Letraset”, che contenevano passaggi del Lorem Ipsum, e più recentemente da software di impaginazione come Aldus PageMaker, che includeva versioni del Lorem Ipsum.";

        Rectangle rect3 = new Rectangle(1000f, 0f, 2000f, 1000f);

        // Determine correct font size

        Font font3 =  new Font(FontFactory.GetFont(FontFactory.HELVETICA, 1000, Font.NORMAL, Color.BLACK));
        float fontSize2 = FitText(font3, text, rect3, 1000, PdfWriter.RUN_DIRECTION_DEFAULT);

        PdfContentByte contentByte = writer.DirectContent;

        Chunk chunk = new Chunk(text, new Font(FontFactory.GetFont(FontFactory.HELVETICA, fontSize2, Font.NORMAL, Color.BLACK)));

        Phrase text33 = new Phrase(chunk);

        ColumnText columnText4 = new ColumnText(cb);
        columnText4.SetSimpleColumn(rect3.GetLeft(0), 0,rect3.GetRight(0), rect3.GetTop(0));
        columnText4.SetText(text33);




        cb.SaveState();
        cb.SetColorStroke(Color.RED);
        cb.Rectangle(rect3.GetLeft(0), rect3.GetBottom(0), rect3.GetRight(0), rect3.GetTop(0));
        cb.Stroke();
        cb.RestoreState();


        columnText4.Alignment = Element.ALIGN_LEFT;
        columnText4.Go();


        doc.Close();

Besides I have throuble even with columnText4.SetSimpleColumn, which render to a different rectangle than my rect3...

EDIT2: using this seem to fix, but I really don't understand why I have to force leadong as fontsize: columnText4.SetSimpleColumn(rect3.Left, rect3.Bottom, rect3.Right, rect3.Top, fontSize2, Element.ALIGN_LEFT);

Community
  • 1
  • 1
Luca
  • 45
  • 6

2 Answers2

3

Use ColumnText.FitText(Font font, String text, Rectangle rect, float maxFontSize, int runDirection). It returns the font size.

Paulo Soares
  • 1,896
  • 8
  • 21
  • 19
  • I forgot to mention that I use version 4 of the library, this function seem to not exist. – Luca Feb 19 '15 at 17:16
  • That's the problem of using versions with many years. Look at the code in the new version, it's only about 20 lines, and adapt it to your needs. – Paulo Soares Feb 19 '15 at 22:20
  • 1
    This function seem to work, at leat it does some font calculation, BUT, I am in throuble with leading, I think... I will update main question with some code – Luca Feb 25 '15 at 13:17
  • I still have a little issue: the function breaks my string in the middle of a word, not on spaces... It is normal? There is a solution for proper word breaks? – Luca Mar 06 '15 at 15:10
1

Downloaded the souces, not all is sound, BUT I undestand this:

paragraph use 1.5 * fontsize as leading.

setText ignore leading and use a default value (16f = 12f x 1.5)

so if I want to use setText I have to provvide leading. BUT the function FitText calculate the fontsize with 1 * fontsize leading, so it is not correct to use it with default 1.5 leading and the other function use default 16f...

I managed extending the FitText with an extra parameter: the multiplier (1, 1.5 etc). This saves my day.

Luca
  • 45
  • 6