Determining the Total Pages Including the Table of Contents of a Project

Introduction

In principle, you can simply use the familiar designer functions Page$() and TotalPages$() to convey the number of total pages in a report. However, the catch here is that the table of contents has its own page counter and thus its pages are ignored in the main project.

Example

In our example, the table of contents consists of three pages, the main project consists of ten pages, so the report has a total of 13 pages. In each project we use the following formula for this purpose:
"Page " + Page$() + " of " + TotalPages$().

With this we get
… for the pages of the table of contents the output
Page 1 of 3, ..., Page 3 of 3

… and for the pages of the main project the output
page 1 of 10, ..., page 10 of 10.

Here it becomes clear that both the table of contents and the main project each work with an independent page counter. The goal, however, is a continuous and overlapping total page count, i.e.

…for the pages of the table of contents the output
page 1 of 13, ..., page 3 of 13

…and for the pages of the main project the output
page 4 of 13, ..., page 13 of 13.

Implementation

In order to implement this, you need to make adjustments to the source code of your List & Label integration.

Note:
From List & Label version 26 on there is an alternative possibility available. You can now use the multi-pass print: Finally – a Glance in the Crystal Ball

In this case no source code adjustment is necessary anymore - but you would have to do without the integrated table of contents and implement it yourself in the main project.

The following steps are to be carried out with the source code adjustment

Step 1

Register two additional variables that contain the total number of pages of the entire report (TotalPages) and the total number of pages of the table of contents (TableofContents).

Step 2

Export the complete report (including the table of contents) to a temporary preview file (*.ll).

Step 3

Determine the total number of pages of the table of contents from the temporary preview file and register it as the PagesTableOfContents variable.

Step 4

Determine the total number of pages of the entire report from the temporary preview file and register it as variable PagesTotal.

Step 5

Delete the temporary preview file again.

Step 6

Final print/export of the project. Of course, the new variables must be used accordingly in the project beforehand as follows:

Table of contents:
"Page " + Page$() + " of " + ToString$(PageTotal).

Main project:
"Page " + Page$(PagesTableOfContents) + " of " + ToString$(PageTotal)

Index (optional):
"Page " + Page$(PagesTableOfContents) + " of " + ToString$(PageTotal)

Implementation in .NET (C#)

...
// Step 1: Register additional variables
LL.Variables.Add("PagesTableOfContents", 0);
LL.Variables.Add("PageTotal", 0);

// Step 2: Temporary export to a preview file (*.ll)
ExportConfiguration exportConfigTemp = new ExportConfiguration(LlExportTarget.Preview, previewFileTemp, projectFile);
LL.Export(exportConfigTemp);

// Step 3: Get the total number of pages in the table of contents
IntPtr llFile = LlCore.LlStgsysStorageOpen(previewFileTemp, "", true, false);
int pagesTableOfContentCount= LlCore.LlStgsysGetPageCount(llFile);
LL.Variables.Add("PagesTableOfContents", pagesTableOfContentCount);
LlCore.LlStgsysStorageClose(llFile);

// Step 4: Get the total number of pages of the whole report
llFile = LlCore.LlStgsysStorageOpen(previewFileTemp, "", true, true);
int totalPageCount = LlCore.LlStgsysGetPageCount(llFile);
LL.Variables.Add("TotalPages", totalPagesCount);
LlCore.LlStgsysStorageClose(llFile);

//Note, from version 27 on the PreviewFile class has a constructor that allows to pass
//the value for OneJobTranslation (which needs to be false in step 3), so it can be used as well then.

// Step 5: Delete temporary preview file again
File.Delete(previewFileTemp);

// Step 6: Final print/export of the project
ExportConfiguration exportConfig = new ExportConfiguration(LlExportTarget.Pdf, exportFile, projectFile);
LL.Export(exportConfig);
...

Further links

For details on the APIs used, see List & Label Programmer’s Reference.