user1651973
user1651973

Reputation: 11

Get text fitting into box using ColumnText

I am using iText library in a ColdFusion application for creating PDFs and the goal is to add various texts into box of certain size at certain position.

It can be perfectly done using ColumnText (details skipped):

<cfset directContent = writer.getDirectContent()>
<cfset ct = CreateObject("java", "com.lowagie.text.pdf.ColumnText").init(directContent)>
<cfset ct.setSimpleColumn(100, 50, 400, 400, 18, Element.ALIGN_JUSTIFIED)>
<cfset ct.setText( createObject("java", "com.lowagie.text.Phrase").init("#text_var#") ) >
<cfset ct.go()>

The problem is: long texts are cut, as expected (go() returns ColumnText.NO_MORE_COLUMN). But I need in this case to show that text is not finished by putting for example '...' in the end (adding dots or if needed replacing one or two last symbols with '...').

Is there a simple way to achieve this? For example, after I call go(true) - I can see number of lines used to output the text without actually outputting it. But I need somehow to get actual text fitting into box.

Upvotes: 1

Views: 1309

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

You're using a mighty old version of iText, and I don't know if what I'm about to answer is supported in the packages that were named after me.

Q1: I want to add ... in the end. A1: if go() returns NO_MORE_COLUMN, you can get the X position of the last X position of the last line that has been written. See http://api.itextpdf.com/itext/com/itextpdf/text/pdf/ColumnText.html#getLastX%28%29 You can get the Y position with getYLine. See http://api.itextpdf.com/itext/com/itextpdf/text/pdf/ColumnText.html#getYLine%28%29 You could use this X and Y to add '...' at the appropriate place.

Q2: I don't want the text to be cut; I want to fit the text inside a Rectangle. A2: that's exactly what is done in this example: http://itextpdf.com/examples/iia.php?id=163 In the method addParagraph(), text is added in simulation mode to see if it fits. If it does, the text is added for real; if it doesn't the text is added again, but using a smaller font.

Upvotes: 2

Related Questions