Ask Reuben

Page N of M

How do I print a page number on a report?

How do I print total pages on a report? 

How do I print total pages of a group on a report? 

How do I print a Page N of M string i.e Page 1 of 2, Page 2 of 2?

How do I reset page number count for each document in my report?

One of the key elements in the architecture of Genero Report Writer is the streaming concept.  If the format allows, this is the ability to view or physically print page 1 of a report as soon as the report engine has finished page 1 and has moved onto rendering page 2.  This means that in the event of a large report, the engine does not have to keep many pages in memory, and the person wanting to print or view the report does not have to wait for the report to be finished in entirety before they starting printing or viewing the report.  The printer will start printing page 1 whilst the engine figures out what is in page 2 etc.

This design has some implications for when you want to print a page count, typically in the form of a “Page n of m”.  If you look at a typical invoice your household receives, say a credit card statement, if there are 3 pages it may say ‘Page 1 of 3″, “Page 2 of 3”, “Page 3 of 3”.  With the architecture of Genero Report Writer, this requires the streaming concept to pause until the engine knows what the number it needs to print on the report is.  It will leave some space on the page and come back and fill in the space when it knows the total number of pages in the report or group.  In the worst case scenario where you are printing the total number of physical pages on every page, this may mean that the whole document maybe held in memory until the whole report is determined and the engine can go back to every page and insert the total page numbers onto each page.

Due to this special processing that is required with a page number, it gets its own drawable “Page Number“, that you can find in the Drawables section of the Toolbox …



… that you drag and drop onto the report and position accordingly.

The total number of pages is not known so this object can not stretch to accomodate the maximum content.  You as the designer can define a size in a number of ways defined here, if none are defined then the default is to leave enough room to display four digits.   Typically you would either set the X-size property to a value e.g. 3cm, or set the text property to the value whose width you want calculated in the current font. This might mean a value of “88” or “Page 88 of 88”.  What digits you use should not matter unless the font chosen displays digits in a proportional font in which case you should use the widest digit in that font.

For the actual value, the important property is the Text Expression property.

If the Text Expression property  is not set then the value displayed is simply the page number…

  • in a format arabic or roman as determined by the format property
  • incremented by the offset property (you may wish to start from a number other than 1)
  • reset to 1 everytime the trigger referend by the name property is fired.

When it comes to printing more than the page number, typically in a string that is similar to “Page N of M”, then this is where you set a value in the Text Expression property.  The typical value is illustrated in this page of the documentation and involves the use of 5 different methods which are used to construct your string.

  • the getPhysicalPageNumber and getTotalNumberOfPhysicalPages methods are used when you want to include the physical page count in your report.
  • the getPageNumber and getTotalNumberOfPages methods are used when you want the page count to reset every time a trigger is fired.  These are typically used when you are printing multiple documents in a single report and you want the page count to reset for each document.  These methods have a pageName property and it is normally a case of selecting from the drop-down the appropriate trigger on which you want to restart the count.
  • finally the format method is similar to the format property in allowing you to specify arabic (1,2,3,4,5, …) or roman numbers (i, ii, iii, iv, v, …)

A typical expression maybe something like …

"Page "+format(getPageNumber("document_number"),ARABIC)+" of "+format(getTotalNumberOfPages("document_number"),ARABIC)

… and this will be used to print output similar to the following on each page …

Page 1 of 2
Page 2 of 2
Page 1 of 3
Page 2 of 3
Page 3 of 3
Page 1 of 1
Page 1 or 3
Page 2 of 3
Page 3 of 3

If you are struggling to come up with the value to use for the name argument, if you click on the drop down for the Page Number.name property this will give you the available values …



In Tutorials & Samples, you will find a number of page number examples.  OrderReport.4rp has a Page Number string  in the bottom right corner and if you investigate that …



… you will see that it has a text expression to display a Page N of M expression that resets for each customers orders.  Note how the report viewer indicates that you are looking at page 1 of 6 but the page count displayed just relates to the individual customer order.  Note when happens to this value as you look at each page, note Page 1 of 2, Page 2 of 2, Page 1 of 1 etc




The final point is that sometimes a barcode needs to display the equivalent of a Page N of M expression.  Typically in barcodes that are used by envelope stuffing machines.    To cater for these type of barcodes, there is a Code Value Expression property on barcode objects that functions similarly to the Text Expression property for page numbers.

The use of these expressions may look complicated but once you have done your first one, got the right value for the name parameter in the methods, then you will find you know what is required.

The main thing to be wary of is the size of the report and what impact will having the total page numbers have on the streaming concept.  if you are counting physical pages then use of getTotalNumberOfPhysicalPages will have minimal effect if the total number of pages is close to 1.  If you have 100 pages in your report then now the reporting engine is now having to keep 100 pages in memory until it prints the report.  Use of the getTotalNumberOfPages(pageName) will face similar concerns but you are less likely to hit the large numbers.

If you get stuck, look at the examples in Tutorials & Samples, such as OrderReport.4rp that do display a page number in the form Page N of M.