[Working with scripts]: Page Count

Normally, the page count is needed when we want specific labels/elements to do specific actions on certain pages.

The reason why there isn’t an option to get the current page number is that at the time the scripts are run, we don’t know what page the component will end up on. If it is small, it might fit on this page. If it can grow it might get pushed onto the next page. If it doesn’t have to keep together, then it might be split across two pages. The component has to be rendered before we know where it will fit, so there is no point having an API that tells you the wrong number.

One recent extension (added in ver.8.2.0) was to handle components where canGrow=false differently. If we know they can’t grow then we know what page they will be on before we start rendering.
Users will need to ensure the constraints are satisfied.

There are several workarounds for this via scripts.
For the examples below, please ensure can grow/shrink is turned off.


Using a Page Counter

1/ Set the page count=1 when the report renders.

Report: OnRenderBegin

    pageCount=1;

2/ Increment the page count at the end of each page. This can track the number of pages in the report.

PageFooter: OnRenderEnd

    pageCount++;

3/ Now, you can set the condition of the label in the Page Header. eg. When Page=2, show label.

Field: RenderIf

    pageCount==2;

4/ Render report. The output will show the label only on Page 2.


Alternative script using Paginator

Paginator.getPageNumber() is only guaranteed to give the right number if called in the page header or page footer (in all cases) and in report bands that have a fixed size.

1/ Create a label to display the current page number.

Field Type>Script

    Paginator.getPageNumber()

Field: OnRenderBegin

importPackage(java.lang);
System.out.print(Paginator.getPageNumber());
setText(Paginator.getPageNumber());

2/ Set the condition of the label in the Page Header. eg. When Page=2, show label.

Field: RenderIf

    pageCount==2;

3/ Render report. The output will show the label only on Page 2.

Please note that the constraints mentioned above apply.

You can find the samples attached for your reference.

PageCount_Sample.zip