mardi 24 mai 2011

ASP: FPDF MultiCell with multiple lines and positioning

As described in another post (ASP: Create PDF with FPDF), FPDF allows to create PDF files dynamically. I described how to create text boxes using MultiCell. I assume you already know how to do it, and I believe you might have encountered a little problem with the cells positioning. Let me explain it.

The positioning
When we position a multicell, we use a code like:
pdf.SetXY 8,156
pdf.setfillcolor 197,198,200
pdf.MultiCell 40,4,"Hello!",0,0,1
The above snippet is taken from the aforementioned article. Basically we set the X and Y position of the multicell, the background colour and then we place the multicell using different parameters (width, height, text, border etc.)

Now imagine that the text is not "Hello!" like in the example, but it is a long text, or better a text which lenght we do not know (because it is dynamic). When you are going to place another multicell, just after the first one, you do not know its exact position, because you don't know its height. So you can't set the X and Y position like:
pdf.SetXY 8,160
assuming that the multicell has an height set to 4. How to correctly place the new multicell?

The GetY()
We can use the pdf.GetY() function to get the Y position just after the first multicell, and then use that position to place the second multicell, the third multicell and so on.
Let's see a complete example:
pdf.SetXY 8,156
pdf.setfillcolor 197,198,200
pdf.MultiCell 40,4,"This is a very long text that won't fit inside the multicell. Because of this the multicell will expand vertically to accomodate all the text",0,0,1
y = pdf.GetY()
pdf.SetXY 8,y
pdf.Multicell 40,4,"This text will stay just after the end of the previous cell",0,0,1
You can use something like that just to be sure to place objects in the right place. Easy isn't it?

Let me know what you think about it.

0 Responses to “ASP: FPDF MultiCell with multiple lines and positioning”

Enregistrer un commentaire

All Rights Reserved Blogging Tips | Blogger Template by Bloggermint