September 28, 2001 - Counting Pages in A Print Job | WebReference

September 28, 2001 - Counting Pages in A Print Job

Yehuda Shiran September 28, 2001
Counting Pages in A Print Job
Tips: September 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

There are several ways to find out how many pages there are in a print job. We define a page by a LAYOUTRECT element within a DEVICERECT element. Here is a typical definition of Page 1, in JavaScript:

newHTML  = "<IE:DEVICERECT ID='devicerect1' MEDIA='print' CLASS='masterstyle'>";
newHTML += "<IE:LAYOUTRECT ID='layoutrect1' CONTENTSRC='document' 
  ONLAYOUTCOMPLETE='onPageComplete()' NEXTRECT='layoutrect2' CLASS='contentstyle'/>";
newHTML += "</IE:DEVICERECT>";
The first method to count pages is by simply counting the number of DEVICERECT elements:

pageTotal = document.all.tags("DEVICERECT").length
The other way is to find out the page where there was no content overflow (last page):

pageTotal = parseInt(event.srcElement.parentElement.id.substring(10), 10);
As you can see from Page 1's definition, the ONLAYOUTCOMPLETE event handler is attached to the LAYOUTRECT element. Its parent is a DEVICERECT element which has an ID of devicerecti. If we extract the substring that begins with the numeric part, we'll end up with the page number. There are 10 characters before the page number in the DEVICERECT's ID.