0

I have a xml document with chapters, and sub-chapters.

I have created an XSL-FO to convert the document to PDF with apache-fop. In the PDF, chapters begin always in a new page using "break-before".

I would like sub-chapters to only start on a page if there is at least 5-10 lines free: sub-chapters do not need to begin on a new page, but it is ugly to have a title in the last line and the first paragraph in the next page.

enter image description here

Any idea how to perform that?

Very simple example of XML file:

<document>
    <chapter title="Intro">
        <sub-chapter title="any-sub-title">
            Any text here
        </sub-chapter>
    </chapter>
</document>

XSL-FO section:

...
<xsl:for-each select="chapter">
    <fo:block font-weight="bold" break-before="odd-page">
        <xsl:value-of select="@title"/>
    </fo:block>
    <xsl:apply-templates/>
</xsl:for-each>
...
<xsl:template match="sub-chapter">
    <fo:block font-weight="bold">
        <xsl:value-of select="@title"/>
    </fo:block>
    <xsl:apply-templates/>
</xsl:template>
Adrian Maire
  • 14,354
  • 9
  • 45
  • 85

1 Answers1

1

What I think you are looking for is widow and orphan protection. With widows and orphans, you specify the number of lines in a block that cannot be left alone on one page.

<fo:block widows="4" orphans="4">
   your content here.
</fo:block>

You might get a similar behavior with the keep-together or keep-with-next attributes. See the link for a quick how-to.

Abel
  • 56,041
  • 24
  • 146
  • 247
  • You will need likely both based on the description. Set keep-with-next.within-page as "always" on the heading block and setting widows/orphans to a reasonable number on the first paragraph block. This will glue the heading to the paragraph and glue together "X" lines of the paragraph. – Kevin Brown Aug 17 '14 at 17:20