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);