[reportlab-users] restart page numbering
Christian Jacobsen
cljacobsen at gmail.com
Mon Nov 2 05:26:50 EST 2009
2009/11/2 David Hughes <dfh at forestfield.co.uk>:
>> Jeffrey O'Neill wrote:
>
>> Currently, the first body page starts with page number 3. Is there a
>> way to change this so that the first body page is number 1?
>
> Please don't do that ;-)
>
> One of my pet hates is to read a PDF document where the page numbers in the
> TOC are not the same as the physical page numbers the browser uses for
> navigation.
There is absolutely nothing wrong with restarting the page numbers
printed in a PDF. I agree that it is annoying when the logical page
numbering (what the PDF viewer thinks a page number is) does not match
up with what is printed on the page. Luckily this is reasonably easy
to get around using PDFPageLabels:
http://www.reportlab.org/apis/reportlab/reportlab.pdfbase.pdfdoc.PDFPageLabel-class.html
See also the PDF standard section 8.3.1 on "Page Labels" (page 481):
http://www.adobe.com/devnet/pdf/pdfs/PDFReference.pdf
Incidentally you will notice if you open up that document in a PDF
viewer, that roman numerals are displayed up front and arabic
numbering only starts well into the document. Jumping to page 481 in
your PDF viewer will get you to the right place in the document (where
as counting 481 physical pages from the first page in the document
wont).
Here is example usage (using the addPageLabel helper in
reportlab.pdfgen.canvas):
-------------8<-------------------8<-------------------8<------------------
from reportlab.pdfgen import canvas
from reportlab.lib.units import cm
c = canvas.Canvas("page_numbering.pdf")
c.addPageLabel(c.getPageNumber()-1, 'ROMAN_LOWER')
for num in ['i', 'ii', 'iii', 'iv', 'v']:
c.drawString(3*cm, 3*cm, num)
c.showPage()
c.addPageLabel(c.getPageNumber()-1, 'ARABIC')
for num in range(1,5):
c.drawString(3*cm, 3*cm, str(num))
c.showPage()
c.addPageLabel(c.getPageNumber()-1, 'ARABIC', 10)
for num in range(10,15):
c.drawString(3*cm, 3*cm, str(num))
c.showPage()
c.addPageLabel(c.getPageNumber()-1, 'ARABIC', prefix='A.')
for num in range(1,5):
c.drawString(3*cm, 3*cm, 'A.' + str(num))
c.showPage()
c.save()
-------------8<-------------------8<-------------------8<------------------
Christian
More information about the reportlab-users
mailing list