Salesforce

How Do I Enumerate Report Pages? (Magic xpa 3.x)

« Go Back

Information

 
Created ByKnowledge Migration User
Approval Process StatusPublished
Objective
Description

How Do I Enumerate Report Pages? (Magic xpa 3.x)

Users often like to have reports with the numbering system “Page n of nn”.

However, it is not possible to know in advance how many pages will be printed in a report, for a given set of data on a particular printer. Page sizes can vary, as can margins and even fonts. For this reason, you need to cycle the report twice: first, to get the total number of pages, and second, to actually do the printing.

Fortunately, this is easy to do in Magic xpa. Here we will show you how.

In our example we are storing the page numbers in our Main Program, which also prints all the headers and footers. So the variable g.Report Page# holds the current page number, and g.Report Total Pages holds the total number of pages that will print. For more information about using global page headers and footers, see How Do I Define a Global Page Header or Footer?.

1. Set up two I/O Devices

In your report subtask, set up two I/O devices. The first one is your actual report. The second one is set up to go to a file, such has %TEMP%Dummy.txt. We used a variable in the parent task to hold the file name, so we can easily use FileDelete() to delete it after we are done.

The presence of this filename keeps the output from actually printing. The output is formatted as if it were going to print, but the output is actually stored in a file.

It is important that each of these devices is set up the same. For instance, you want to use the same headers and footers, or the page count will not be the same on the two devices.

Also in our report subtask, we create a parameter, “p.Count Pages?”. When this is TRUE, the task will only print to Dummy, and no real output will be produced.

Note: Usually you will want to use the same printer for both I/O devices. However, there are a multitude of printer setups in Windows, and there are cases where the printer driver is set up to do something like prompt the user for a filename, which you don’t want to happen twice in the same job. In that case, just set up a “DummyPrinter” in Options->Settings ->Printers.

2. Set up the calling task

The calling task is going to do four things:

  1. Zero out the global variable g.Report Total Pages.

  2. Call the printing subtask with p.Count Pages? = TRUE. This will count the total number of pages.

  3. Call the printing subtask with p.Count Pages? = FALSE. This will actually print the report.

  4. Delete the temporary file.

3. In Task Prefix, zero out report variables

Now, in your subtask, zero out g.Report Page#, and any other variables that are totalled for the entire report.

Note: In is not required that variables, like Total Price, to be zeroed out, because Magic xpa automatically initializes them to zero when the task starts. However, it is a good practice to zero them out anyway, because often tasks end up getting set to “resident” for efficiency. In that case, then the totals will suddenly be off if the user runs the report twice.

4. Create a Page Header event to count the pages

Create a Page Header event to update the global page number. This will keep track of the current page number for either I/O device.

5. Store the total number of pages on the first go-around

Now, in Task Suffix, update your global total number of pages, g.Report Total pages, with the current page number. Note we have a condition on this so it only happens on the first iteration. That isn’t strictly necessary, since the results should be the same for each iteration, but it makes the code a little more clear.

6. Output to two I/O devices

Now here is the part that makes this all work. Replicate each Form Output operation (in most reports there won’t be very many: only one in this example). The first Form Output will go to the Dummy I/O device, if p.Count Pages is TRUE. The second Form Output will go to the Report I/O device, if p.Count Pages is FALSE.

Now, when the report runs, it will cycle twice, and you will get correct “Page n of nn” headers.

See also

The Online and Rich Client Samples projects (program RP05 and RRP05)

Reference
Attachment 
Attachment