Page Headers for Multiple Chapters

This section provides a tutorial example on how to generate page headers for multiple chapters. The page header should display the chapter title, and the first page of each chapter should have no header.

If you are using XSL-FO to format a book and want to add page headers in the book, you may encounter the following scenarios:

Scenario 1 - If your book has multiple chapters, and you want to the chapter title to be displayed in page headers. You need to find a way to change page header text, when the page content changes from one chapter to the next chapter.

This can be done by using multiple page-sequence formatting objects. Each page-sequence is for a single chapter, with its own static-content for page headers of one chapter only.

Scenario 2 - If you add a page header as a static-content in a page-sequence that uses a simple-page-master layout, the page header will show up in the region-before area on all pages in that sequence.

But sometimes, you may want to format the first page without the header, because the header contains the chapter title, which is redundant with the chapter title on the first page.

This can be done by using a page-sequence-master layout sequence with two layouts: one for the first page without region-before declared, and one for other pages with region-before declared.

Here is my tutorial example, Page-Header-for-Multiple-Chapters.fo that uses two page-sequence formatting objects to generate two chapters with different page headers. It also uses a page-sequence-master with two layouts to turn off the header on the first page of each chapter.

<?xml version="1.0" encoding="utf-8"?>
<!-- Page-Header-for-Multiple-Chapters.fo
 - Copyright (c) 2016, HerongYang.com, All Rights Reserved.
-->
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
 <fo:layout-master-set>
  <fo:simple-page-master master-name="first_page" 
   margin="0.1in" page-height="3.3in" page-width="2.55in">
   <fo:region-body region-name="body"
    margin-top="0.4in" margin-bottom="0.2in"/>
   <fo:region-after region-name="footer" extent="0.15in"/>
  </fo:simple-page-master>
  <fo:simple-page-master master-name="other_page" 
   margin="0.1in" page-height="3.3in" page-width="2.55in">
   <fo:region-body region-name="body"
    margin-top="0.4in" margin-bottom="0.2in"/>
   <fo:region-before region-name="header" extent="0.3in"/>
   <fo:region-after region-name="footer" extent="0.15in"/>
  </fo:simple-page-master>
  <fo:page-sequence-master master-name="book">
   <fo:single-page-master-reference 
    master-reference="first_page"/>
   <fo:repeatable-page-master-reference 
    master-reference="other_page"/>
  </fo:page-sequence-master>
 </fo:layout-master-set>
 <fo:page-sequence master-reference="book">
  <fo:static-content flow-name="header">
   <fo:block-container height="0.3in" display-align="after">
    <fo:block text-align="center" font-size="8pt">
Night 1
    </fo:block>
    <fo:block text-align="center" font-size="8pt"
     border-bottom-width="1px" border-bottom-style="solid">
The Story of the Merchant and the Jinni
    </fo:block>
   </fo:block-container>
  </fo:static-content>
  <fo:static-content flow-name="footer">
    <fo:table font-size="8pt"
     border-top-width="1px" border-top-style="solid">
     <fo:table-body>
      <fo:table-row>
       <fo:table-cell>
        <fo:block>Arabian Nights</fo:block>
       </fo:table-cell>
       <fo:table-cell>
<fo:block text-align="end">Page <fo:page-number/></fo:block>
       </fo:table-cell>
      </fo:table-row>
     </fo:table-body>
    </fo:table>
  </fo:static-content>
  <fo:flow flow-name="body" font-size="10pt">
   <fo:block text-align="center" margin-top="6pt" font-weight="bold">
Night 1
   </fo:block>
   <fo:block text-align="center" margin-top="6pt" font-weight="bold">
The Story of the Merchant and the Jinni
   </fo:block>
   <fo:block text-align="justify" margin-top="6pt">
IT has been related to me, O happy King, said Shahrazad, 
that there was a certain merchant who had great wealth, 
and traded extensively with surrounding countries; and 
one day he mounted his horse, and journeyed to a 
neighbouring country to collect what was due to him, 
and, the heat oppressing him, he sat under a tree, in a 
garden, and put his hand into his saddle-bag, and ate a 
morsel of bread and a date which were among his 
provisions. Having eaten the date, he threw aside the 
stone, and immediately there appeared before him an 
‘Efrit, of enormous height, who, holding a drawn sword 
in his hand, approached him, and said, Rise, that I may 
kill thee, as thou hast killed my son. the merchant asked 
him, How have I killed thy son? He answered, When thou 
atest the date, and threwest aside the stone, ...
   </fo:block>
  </fo:flow>
 </fo:page-sequence>
 <fo:page-sequence master-reference="book">
  <fo:static-content flow-name="header">
   <fo:block-container height="0.3in" display-align="after">
    <fo:block text-align="center" font-size="8pt">
Night 2
    </fo:block>
    <fo:block text-align="center" font-size="8pt"
     border-bottom-width="1px" border-bottom-style="solid">
The Story of the First Sheykh and the Gazelle
    </fo:block>
   </fo:block-container>
  </fo:static-content>
  <fo:static-content flow-name="footer">
    <fo:table font-size="8pt"
     border-top-width="1px" border-top-style="solid">
     <fo:table-body>
      <fo:table-row>
       <fo:table-cell>
        <fo:block>Arabian Nights</fo:block>
       </fo:table-cell>
       <fo:table-cell>
<fo:block text-align="end">Page <fo:page-number/></fo:block>
       </fo:table-cell>
      </fo:table-row>
     </fo:table-body>
    </fo:table>
  </fo:static-content>
  <fo:flow flow-name="body" font-size="10pt">
   <fo:block text-align="center" margin-top="6pt" font-weight="bold">
Night 2
   </fo:block>
   <fo:block text-align="center" margin-top="6pt" font-weight="bold">
The Story of the First Sheykh and the Gazelle
   </fo:block>
   <fo:block text-align="justify" margin-top="5pt">
THEN said the sheykh, Know, O ‘Efrit, that this gazelle 
is the daughter of my paternal uncle, and she is of my 
flesh and my blood. I took her as my wife when she was 
young, and I lived with her about thirty years; but I 
was not blessed with a child by her; so I took to me a 
concubine slave, and by her I was blessed with a male 
child, like the rising full moon, with beautiful eyes, 
and delicately-shaped eyebrows, and perfectly-formed 
limbs; and he grew up by little and little until he 
attained the age of fifteen years. At this period, 
unexpectedly had occasion to journey to a certain city, 
and went thither with a great stock of merchandise. ...
   </fo:block>
  </fo:flow>
 </fo:page-sequence>
</fo:root>

If you process this example XSL-FO document with Apache FOP to a PDF file, you will see 2 short stories from the "Arabian Nights". Each story is formatted as chapter with first page has no header and other pages have headers display the chapter title.

Page Headers for Multiple Chapters

Last update: 2016.

Table of Contents

 About This XSL-FO Tutorial Book

 Introduction of XSL-FO

 Apache™ FOP (Formatting Objects Processor)

 RenderX XEP as an XSL-FO Tool

 Introduction of Area Model

 XSL-FO Document Basics and Examples

 Page Layout Masters

 Block-Level Formatting Objects

 Inline-Level Formatting Objects

 Including Graphics in XSL-FO document

 Table of Rows and Columns

 List, Item, Label, and Body

 Floating Blocks - "float" and "footnote"

 Hyperlinks, Table of Contents and Indexes

Headers and Footers using "static-content"

 Page Header in region-before Area

 Page Header with 3 Parts

 Page Footer with 3 Parts

Page Headers for Multiple Chapters

 Font Attributes and Font Families

 Apache FOP Font Configurations

 RenderX XEP Font Configurations

 References

 PDF Printing Version