Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iTextSharp - calculating phrase/paragraph height before adding to column

I am creating PDF work orders that have a varying amount of items and notes. I am using ColumnText.SetSimpleColumn(phrase, x, y, x2, y2, height, alignment) to add text to a single column.

I can get the ColumnText.YLine value, but only after applying it to the ColumnText object with the Go() method.

What I want to do is to either pre-calculate the final YLine value or undo the adding of a phrase if the YLine is past mjy bottom threshold so I can manually add another page and continue. Is this possible?

like image 341
sreimer Avatar asked Sep 10 '13 16:09

sreimer


1 Answers

As indicated in the comment I posted earlier, the answer to your question is yes. MovieColumns3 shows you how to do it:

Instead of using a static ColumnText method, you need to create a ColumnText object and use the setSimpleColumn() method on that object. You can work in text mode adding Phrase objects with the addText() method, or in composite mode adding any type of Element using the addElement() method.

Normally, you'd then invoke the go() method to render those elements, but that's the whole point of your question: before you render any element, you want to know of the element fits. That can be done to use the go() method in simulation mode: go(true). This will pretend to add the content, but won't render anything. You can get the Y position using the getYLine() method and check if all the content was added using the hasMoreText() method.

It's important to understand that the go() method (partially) consumes the content of the ColumnText object. Once you decide to add the content for real (that is: not in simulation mode) be it on the current page or the next page, you need to discard all remaining content from the ColumnText object and add it anew.

You'll find a more detailed explanation in my book and if you don't recognize the methods in the example because it's written in Java, please compare with the C# port of the example.

like image 75
Bruno Lowagie Avatar answered Oct 22 '22 08:10

Bruno Lowagie