[reportlab-users] Absolute position of a paragraph in a page

Robin Becker robin at reportlab.com
Mon Nov 30 08:26:22 EST 2009


Solid Snake wrote:

> Hello,

>

> I'm new to ReportLab and I'd like to use it to generate a PDF. My

> starting setup is pretty simple: a BaseDocTemplate which has a

> PageTemplate in it with two Frames(2 columns). Here is the code on

> pastebin[1]

>

> I'd like to put some Paragraphs in it but it is very important to know

> the position of these paragraphs after they are placed by the build

> function. Is there any way to do that?

>

> I have found a similar thread[2] on the list but I didn't understand

> much of the answer.

>

> Thanks in advance,

> Sergiu

.......

this snippet of code uses a special paragraph to allow you to find out where it
was drawn (and also draws a box around it).

from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus.paragraph import Paragraph
from reportlab.platypus.doctemplate import SimpleDocTemplate
from reportlab.platypus import Spacer

class MyParagraph(Paragraph):
def drawOn(self, canv, x, y, _sW=0):
'''at the point this is called we should know how large we are'''
canv.saveState()
canv.setStrokeColor((1,0,0))
canv.setLineWidth(1)
print 'x=%s y=%s width=%s height=%s' % (x,y,self.width,self.height)
canv.rect(x,y,self.width,self.height,stroke=1,fill=0)
canv.restoreState()
Paragraph.drawOn(self,canv,x,y,_sW)

styleSheet = getSampleStyleSheet()
bt = styleSheet['BodyText']
p1=MyParagraph('''The concept of an integrated one box solution for advanced
voice and
data applications began with the introduction of the IMACS. The
IMACS 200 carries on that tradition with an integrated solution
optimized for smaller port size applications that the IMACS could not
economically address. An array of the most popular interfaces and
features from the IMACS has been bundled into a small 2U chassis
providing the ultimate in ease of installation.''', bt)

p2=MyParagraph('''The concept of an integrated one box solution for advanced
voice and
data applications began with the introduction of the IMACS. The
IMACS 200 carries on that tradition with an integrated solution
optimized for smaller port size applications that the IMACS could not
economically address. An array of the most popular interfaces and
features from the IMACS has been bundled into a small 2U chassis
providing the ultimate in ease of installation.''', bt)

story=[p1,Spacer(72,72),p2]
doc = SimpleDocTemplate('tp.pdf')
doc.build(story)

--
Robin Becker


More information about the reportlab-users mailing list