Print Templates, Part V: Re-pagination: Hiding Excess Pages - Doc JavaScript | WebReference

Print Templates, Part V: Re-pagination: Hiding Excess Pages - Doc JavaScript

Print Templates, Part V: Re-pagination

Hiding Excess Pages

The function AddFirstPage() is similar to what we described in template6.html:

function AddFirstPage() {
  newHTML  = "<IE:DEVICERECT ID='devicerect1'
    MEDIA='print' CLASS='masterstyle'>";
  newHTML += "<IE:LAYOUTRECT ID='layoutrect1'
    CONTENTSRC='document' ONLAYOUTCOMPLETE='OnRectComplete()'
    NEXTRECT='layoutrect2' CLASS='contentstyle'/>";
  newHTML += "</IE:DEVICERECT>";
  zoomcontainer.insertAdjacentHTML("afterBegin", newHTML);
  headfoot.textHead = printer.header;
  headfoot.textFoot = printer.footer;
  headfoot.url = dialogArguments.__IE_BrowseDocument.URL;
  headfoot.title = dialogArguments.__IE_BrowseDocument.title; = 1;

Notice the ONLAYOUTCOMPLETE event handler. It is pointing to the OnRectComplete() function. This is the default overflow handler:

function OnRectComplete() {
  if (event.contentOverflow == true) {AddNewPage();}
  else {
    headfoot.pageTotal =
    for (i = 1; i <= headfoot.pageTotal; i++)
    setTimeout("CheckPrint();", 100);

We add a new page if there is a content overflow. Otherwise, we just compute the total number of pages and update all pages with it. We call the printing job ("CheckPrint") after a delay of 100 milliseconds, to let the pagination complete before the printing.

The function AddNewPage() is more complex:

function AddNewPage() {
  document.all("layoutrect" + lastPage).onlayoutcomplete
    = OnRectCompleteSimple; = lastPage;
  newHTML  = "<IE:DEVICERECT ID='devicerect" +
    (lastPage + 1) +  "' MEDIA='print' CLASS='masterstyle'>";
  newHTML += "<IE:LAYOUTRECT ID='layoutrect" +
    (lastPage + 1) + "' ONLAYOUTCOMPLETE='OnRectComplete()'
    NEXTRECT='layoutrect" + (lastPage + 2) + "'  CLASS='contentstyle'/>";
  newHTML += "</IE:DEVICERECT>";
  zoomcontainer.insertAdjacentHTML("beforeEnd", newHTML);
  headfoot.pageTotal = lastPage + 1;

First, we change the onlayoutcomplete event of the previous page to OnRectCompleteSimple:

document.all("layoutrect" + lastPage).onlayoutcomplete =

This is done in order to avoid creation of new pages during zooming pagination:

function OnRectCompleteSimple() {
  if (event.contentOverflow == true) return;
  headfoot.pageTotal =
    parseInt(, 10);

The OnRectCompleteSimple() function handles the excess pages, after the last page has been laid out. The last page is identified by event.contentOverflow = false. On every other page, where event.contentOverflow = true, we just return back to the caller. The total number of pages is computed from the last page's ID, as the page ID is concatenated from "devicerect" and the sequence number of the page. Once we know the total number of pages, we update all pages' headers, and then call ShowFilledPagesAndHideExcessPages():

function ShowFilledPagesAndHideExcessPages() {
  for (i = 1; i <= headfoot.pageTotal; i++)
  document.all("devicerect" + i).style.position = "static";
  var i = headfoot.pageTotal + 1;
  while (document.all("devicerect" + i)) {
    document.all("devicerect" + i).style.position = "absolute";

In this function we set all active pages' position to "static", while we set the excess pages' position to "absolute". Setting their position to "absolute" places them outside the active screen area, as we set the left property of masterstyle to -100 in.

Next: How to initialize the print template

Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: October 8, 2001
Revised: October 8, 2001