19

please find the following xsl-fo , tried to set header and footer for every page in pdf, but got only header at first page and footer at last page. But here i needed for every page. How to perform this.

    <?xml version="1.0" encoding="UTF-8" ?>
     <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match='/'>
    <fo:root>
        <fo:layout-master-set>
            <fo:simple-page-master master-name="my-page"
                               page-height="29.7cm"
                  page-width="21cm"
                  margin-top="1cm"
                  margin-bottom="0.1cm"
                  margin-left="0.8cm"
                  margin-right="1.0cm" >
                <fo:region-body margin-top="2.5cm" margin-bottom="2.5cm"/>
                <fo:region-before extent="2.0cm"/>
                <fo:region-after extent="2.0cm"/>
            </fo:simple-page-master>
        </fo:layout-master-set>
        <fo:page-sequence master-reference="my-page">
            <fo:flow flow-name="xsl-region-before">
                <fo:block>
                    Message Body
                </fo:block>
            </fo:flow>
            <fo:flow flow-name="xsl-region-body">
                Message Content
            </fo:flow>

            <fo:flow flow-name="xsl-region-after">
                <h2>
                    Page Footer
                </h2>
            </fo:flow>
        </fo:page-sequence>
    </fo:root>
</xsl:template>
<xsl:template name="replace-returns">
    <xsl:param name="text"/>
    <xsl:choose>
        <xsl:when test="contains($text, '&#xa;')">
            <xsl:value-of select="substring-before($text, '&#xa;')"/>
            <xsl:value-of select="'&lt;br /&gt;'" disable-output-escaping="yes"/>
            <xsl:call-template name="replace-returns">
                <xsl:with-param name="text" select="substring-after($text, '&#xa;')"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
Vishnu
  • 1,029
  • 4
  • 14
  • 24
  • I don't see where you are setting your page size. It looks like an incomplete sample code. You should have somewhere that you are setting page-width="" page-height="" margin-top="" margin-bottom="" margin-left="" margin-right="" defaults for page. – Brian Anderson Dec 18 '13 at 12:09
  • please find my edits above. – Vishnu Dec 18 '13 at 13:12
  • 1
    html (e.g.

    ) tags are not in the fo namespace and will not be rendered by the FO processor. And I believe the xsl-region-before and xsl-region-after need to come before xsl-region-body... at least in FOP I believe it does.

    – PhillyNJ Dec 18 '13 at 16:58
  • @Phil, i tried , but cant get header and footer for every page. can u attach here a sample xsl-fo for that – Vishnu Dec 19 '13 at 08:56
  • 1
    http://www.data2type.de/en/xml-xslt-xslfo/xsl-fo/xsl-fo-introduction/tables/#c440. – swamy Jan 06 '14 at 11:19

3 Answers3

29

Below is a simple example on how to get a header and footer on each page. Hope this helps

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <fo:layout-master-set>
        <fo:simple-page-master master-name="simple" page-height="11in" page-width="8.5in" margin-top=".5in" margin-bottom=".5in" margin-left=".5in" margin-right=".5in">
            <fo:region-body region-name="xsl-region-body" margin-bottom=".5in" margin-top=".50in"/>
            <fo:region-before region-name="xsl-region-before" extent="5in"/>
            <fo:region-after region-name="xsl-region-after" extent=".5in"/>
        </fo:simple-page-master>
    </fo:layout-master-set>
    <fo:page-sequence master-reference="simple">
        <fo:static-content flow-name="xsl-region-before">
            <fo:block>header</fo:block>
        </fo:static-content>
        <fo:static-content flow-name="xsl-region-after">
            <fo:block>footer</fo:block>
        </fo:static-content>
        <fo:flow flow-name="xsl-region-body">
            <fo:block break-after="page">
            Body
            </fo:block>
            <fo:block>
            Body
            </fo:block>
        </fo:flow>
    </fo:page-sequence>
</fo:root>
PhillyNJ
  • 3,859
  • 4
  • 38
  • 64
  • thanks for your post. but i tried it doesnt work. here im trying to pass XML to XSl-fo to generate pdf. but in pdf im getting only two lines. – Vishnu Dec 19 '13 at 14:44
  • What FO processor are you using? This was tested in FOP .95 and works. – PhillyNJ Dec 19 '13 at 14:49
  • Phil, i have XML and related above xsl-fo, and in C#.net it doesnt works. how to perform this in .NET – Vishnu Dec 24 '13 at 06:27
  • @Vishnu, "doest work" is a very broad term. Little googling, I found this http://nfop.sourceforge.net/ – PhillyNJ Dec 24 '13 at 13:11
  • Phil,yes working. your answer works perfectly in online pdf converter, got perfect header and footers, but i use this same XML and xsl-fo in c# code, it doesnt works. – Vishnu Dec 24 '13 at 14:52
  • check out this link http://www.codeproject.com/Articles/12682/Creating-PDF-with-nFOP, I just tested it and it works in a c# application. You will need to download the Microsoft Visual J# Version 2.0 Redistributable Package @ http://www.microsoft.com/en-us/download/confirmation.aspx?id=4712 – PhillyNJ Dec 24 '13 at 14:57
4

If you want header and footer, don't put them into the fo:flow element. They belong into <fo:static-content flow-name="your_flow"> where your_flow can be xsl-region-before or xsl-region-after or any other name you like to give.

This leads to the missing definition of your regions. The ones you use are not defined. Make this work like <fo:region-body region-name="xsl-region-body"> or <fo:region-before region-name="xsl-region-before">

I didn't check if something else prevents the script from working, but your question should be answered with this.

Knut Holm
  • 3,988
  • 4
  • 32
  • 54
Andreas
  • 1,220
  • 8
  • 21
-2

Use this in an itemstyle to output a header:

<xsl:if test="count(preceding-sibling::*)=0">
</xsl:if>

Use this in an itemstyle to output a footer:

<xsl:if test="count(following-sibling::*)=0">
</xsl:if>

More info about this.

Regent
  • 5,142
  • 3
  • 21
  • 35
Sunil
  • 1
  • 1