Using XSL Formatting Objects - Page masters
(Page 4 of 4 )
Let's explore XSL-FO in more detail, starting with page masters. As mentioned earlier, page masters govern the geometry of pages. Pages created from content within a page-sequence element that references a given page master will use the page master's properties.
In the first example, the text "Hello, world." was crammed into the top left-hand corner of the document, flush with the edge of the page. This is very unappealing. In order to fix this, we need to add some margins to the page. Before we do that, though, it helps to determine the size of the page itself. This can be done by setting the page-height and page-width attributes of the simple-page-master element. Let's create a letter-sized page (8.5" x 11") by modifying the simple-page-master element within helloworld.fo:
<fo:simple-page-master master-name="BasicPage" page-width="8.5in" page-height="11in">
With the page's dimensions defined, we can now set some margins. There are a few ways we can do this. The first is to define the margins uniformly, giving each side of the page the same margin. This can be done using the margin attribute. Let's make the page have a margin of one inch on all sides:
<fo:simple-page-master master-name="BasicPage" page-width="8.5in" page-height="11in" margin="1in">
Another way way is to set the margins individually. Margins for each side can be set using the margin-top, margin-bottom, margin-left and margin-right attributes. Suppose we wanted to specify margins of one inch on the top and the bottom and margins of three-quarters of an inch on the left and right. We'd do it like this:
<fo:simple-page-master master-name="BasicPage" page-width="8.5in" page-height="11in" margin-top="1in" margin-bottom="1in" margin-left=".75in" margin-right=".75in">
The above markup is a bit lengthy though. It's also possible to set the margins individually with the margin attribute. Margin sizes for each side can be passed in, separated by whitespace, just as they can with HTML. We can accomplish the same thing as above like this:
<fo:simple-page-master master-name="BasicPage" page-width="8.5in" page-height="11in" margin="1in .75in 1in .75in">
The first measurement passed in is the top margin. From there, it moves clockwise, so that the second measurement is the right margin, and so forth. We can simplify the above code even more, though. If we just pass in two measurements, then the first measurement will be used for the top and bottom margins, and the second measurement will be used for the left and right margins. The following markup would produce the same output:
<fo:simple-page-master master-name="BasicPage" page-width="8.5in" page-height="11in" margin="1in .75in">
Text isn't simply thrown onto the page in the same area, though. Later, we'll cover regions. Thanks for reading!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |