spacer
Yehuda Shiran August 12, 2001
Creating Dynamic Print Templates
Tips: August 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

You've probably asked yourself by now how to prepare the exact number of DeviceRect and LayoutRect elements for the source you want to format. You are also wondering how to create these elements without copying and pasting 100 times. The answer is the same for both. We can write JavaScript code that will create the DeviceRect and LayoutRect elements on the fly. Try it.

Creating the first page is straightforward and explained elsewhere. The trickier part is how to add a new page whenever the previous one is overflowed. The function onPageComplete() is called whenever the last page is overflowed by a longer source. This function is similar to the addFirstPage() function we showed in a previous tip. We assemble a variable, newHTML, with one LayoutRect element within a DeviceRect element:

function onPageComplete() {
  if (event.contentOverflow) {
    document.all("layoutrect" + lastPage).onlayoutcomplete = null;
    newHTML  = "<IE:DEVICERECT ID='devicerect" + (lastPage + 1) +  
               "' MEDIA='print' CLASS='masterstyle'>";
    newHTML += "<IE:LAYOUTRECT ID='layoutrect" + (lastPage + 1) + 
               "' ONLAYOUTCOMPLETE='onPageComplete()' NEXTRECT='layoutrect" + 
               (lastPage + 2) + "'  CLASS='contentstyle'/>";
    newHTML += "</IE:DEVICERECT>";

    devicecontainer.insertAdjacentHTML("beforeEnd", newHTML);
    lastPage++;
  }
}
The challenge is how to assemble those attributes that are unique per page. These are ID (for both DeviceRect and LayoutRect) and NEXTRECT. They all consist of a fixed string concatenated with the page number. The current page number is lastPage + 1. The next page number is lastPage + 2. The DeviceRect element's ID is assembled by concatenating 'devicerect' and (lastPage + 1). The LayoutRect element's ID is assembled by 'layoutrect' and (lastPage + 1). The attribute NEXTRECT needs to reference the next page's LayoutRect element, so its value is 'layoutrect' + (lastPage + 2).

The variable lastPage is initialized to 1 before calling the first page, and is incremented after creating a new page.

For more information on print templates, go to Column 89, Print Templates, Part I.


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business