[reportlab-users] NextPageTemplate question

Andy Robinson reportlab-users@reportlab.com
Thu, 22 May 2003 15:29:42 +0100


> Thank you very much, Andy.  By putting NextPageTemplate at the top of 
> my page, I got each unique page template render in the proper place.
> 
> Is there a way to count all the pages that have been created for a 
> document and then create a variable like totalPages so that I can 
> append something like "Page 1 of 8" automatically to each page?

2 approaches.  Quick and simple uses Form XObjects, which
are in the user guide.  You refer to a form on each page
by name with a line like this, BEFORE pagecount has been
defined.
   canvas.doForm('pageCount')

Then at the end, ask the canvas for the page number, define
a routine in a form to draw the number:
  canvas.beginForm('pageCount')
  num = camvas.getPageNumber()
  camvas.setFont(myfont,mysize)
  canvas.drawString(x, y, str(num))
  canvas.endForm()

You can also use this for simple tables-of-contents
where the size of the TOC or number of pages it spans
is calculable in advance.

The second approach is for more complex things like
tables of contents or dynamic text in paragraphs,
where the crossref info may itself disturb where
other things need to go.  In this case, you just
do the whole thing 2-3x until constraints are satisfied.

Hope this helps,

Andy