1T3XT info
Wed, 28 May 2008 08:26:30 -0700
[EMAIL PROTECTED] wrote: > Hi, > I have some PDF form fields which iText is populating. (snip) > The ideal solution would compact text that is overflowing, > but respect the original font size if extra space is available. > To that end, I'm trying to get the Font object in order to > calculate how much space the text will occupy, but I can't > find anything in the API that allows me to retrieve that object. That's too much work. Why not try this solution: http://www.1t3xt.info/examples/browse/?page=example&id=347 The part that is important for you can be found here: PdfContentByte canvas = stamper.getOverContent(1); float size = 12; float[] f = form.getFieldPositions(TEXT); while (addText(s, canvas, f, size, true) && size > 6) { size -= 0.2; } addText(s, canvas, f, size, false); What happens? The initial font size = 12. addText tries to add the text, but if it doesn't fit, the font is made smaller by 0.2 pt (but the size has too remain bigger than 6pt otherwise the text might not be legible). As soon as the text fits the field, the text is added for real. You'll have to adapt (read: simplify) the addText method, as it assumes that the String s is an HTML snippet: public static boolean addText(String s, PdfContentByte canvas, float[] f, float size, boolean simulate) throws DocumentException, IOException { StyleSheet styles = new StyleSheet(); styles.loadTagStyle("p", "size", size + "px"); styles.loadTagStyle("p", "align", "justify"); styles.loadTagStyle("p", "hyphenation", "en_us"); ArrayList<Element> objects = HTMLWorker.parseToList(new StringReader(s), styles, null); ColumnText ct = new ColumnText(canvas); ct.setAlignment(Element.ALIGN_JUSTIFIED); ct.setLeading(size * 1.2f); ct.setSimpleColumn(f[1] + 2, f[2] + 2, f[3] - 2, f[4]); for (Element element : objects) { ct.addElement(element); } return ColumnText.hasMoreText(ct.go(simulate)); } The result looks like this: http://www.1t3xt.info/examples/results/classroom/filmfestival/movies25.pdf -- This answer is provided by 1T3XT BVBA ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ iText-questions mailing list iText-questions@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/itext-questions Do you like iText? Buy the iText book: http://www.1t3xt.com/docs/book.php Or leave a tip: https://tipit.to/itexttipjar