Stylesheet Development with PTC ALD > Adding PTC ALD Code to Stylesheet Source > Samples > Testing > Test Page Number and Apply Formatting Properties
  
Test Page Number and Apply Formatting Properties
Samples directory: Arbortext-path/samples/APP/Testing/PageNumber
Sample file: testPageNumber.xml
Main stylesheet: testPageNumber.style
Stylesheet modules: baseStylesheet.style and testPageNumberPropertySets.style
The sample is based on the standard axdocbook document type. It demonstrates how to test the current page number to ascertain if it is an odd or even value. The content of the paragraphs on the page will either be right aligned or left aligned, depending on the result of the test.
Refer to the title elements in the sample file Arbortext-path/samples/APP/Testing/PageNumber/testPageNumber.xml sample file. The title in chapter context in the associated stylesheet references the pageTest property set. The property set includes PTC ALD source code edits to test the page number and drive text alignment accordingly. Access the source code for the property set by selecting it in the Property Sets list, and using the Edit > Edit Property Set Source > APP menu option.
The relevant code from the property set is given below:
if (formatting.currentPage.pageNumber % 2 == 0) {
//Page is even, align left
paragraph.textAlign = fParagraph.ALIGN_LEFT;
}
else {
//Page is odd, align right
paragraph.textAlign = fParagraph.ALIGN_RIGHT;
}
The code is based on two conditions. The first if condition will match if the current page number is divisible by two. The second else condition will match for all other occurrences, i.e. odd page numbers.
if (formatting.currentPage.pageNumber % 2 == 0) {
//Page is even, align left
paragraph.textAlign = fParagraph.ALIGN_LEFT;
}
This condition refers to the currentPage property of the fFormatting object, which calls an fPage object. The value of the pageNumber property of the fPage object is evaluated to see if it is equal to 0 when interpreted via the % 2 mechanism, i.e. divisible by 2.
The JavaScript operator % interprets the current value in the form of the remainder left when the value is divided by the supplied number. With % 2 in operation, the current value of pageNumber will be provided as the remainder when the value is divided by 2. For example, if the actual value of pageNumber is 8, it will be given as 0. The condition will match for a page numbered 8. If the actual value of pageNumber is 33, it will be given as 1. The condition will not match for a page numbered 33.
If the condition matches for the current element, i.e. the current page is even, the next phrase will align the content in paragraphs to the left. The first part of the phrase paragraph.textAlign specifies that the textAlign property of the paragraph, accessed via a fParagraph object, is to be set. The second half of the phrase =fParagraph.ALIGN_LEFT calls the ALIGN_LEFT constant of the fParagraph object to align paragraph content to the left horizontally. Note that ALIGN_LEFT is described in the HorizontalAlignment group for fParagraph.
else {
//Page is odd, align right
paragraph.textAlign = fParagraph.ALIGN_RIGHT;
}
This condition provides a simple alternative to the first condition and will match for odd numbered pages.
If the condition matches for the current element, the next phrase will align the content in paragraphs to the right. The first part of the phrase paragraph.textAlign specifies that the textAlign property of the paragraph, accessed via a fParagraph object, is to be set. The second half of the phrase =fParagraph.ALIGN_RIGHT calls the ALIGN_RIGHT constant of the fParagraph object to align paragraph content to the right horizontally. Note that ALIGN_RIGHT is described in the HorizontalAlignment group for fParagraph.