Search code examples
xmlxsltxsl-fo

Using text as background


I am using this code to add an image background to my file:

<fo:block-container absolute-position="absolute"
                        top="-1cm" left="-3cm" width="26cm" height="29.7cm"
                        background-image="Pictures/afbeelding.png">
                        <fo:block/>
</fo:block-container>

This code works fine. I now want to try someting new.

Is it also possible to add a String as background? Can I also determine the position of the string? I want this string to be positioned diagonally from the top left corner to the bottom right one.

I have searched for a solution but dindn`t find one.


Solution

  • Yes, you can use text as a background. To make sure the background text is rendered as a background, and not on top of the body text:

    • place the text in one of the static-content frames
    • use a block-container with absolute-position to place the container underneath the body frame

    <fo:static-content flow-name="inside">
       <fo:block-container width="170mm" absolute-position="absolute" top="100mm" left="20mm">
            <fo:block text-align="center" font-size="45pt" font-weight="bold"  axf:transform="rotate(-45deg)">
        	  <xsl:text>Watermark text</xsl:text>
            </fo:block>
       </fo:block-container>
    </fo:static-content>

    • in your fo:simple-page-master, the order of the regions determines which content is rendered on top. The regions are processed in order, later regions will be drawn on top of earlier ones. Put the region-body last in the list, and it will be drawn on top of all others:

        <fo:simple-page-master master-name="odd">			
        	<fo:region-before/>
        	<fo:region-after/>
        	<fo:region-start/>
        	<fo:region-end/>
        	<fo:region-body/>
        </fo:simple-page-master>

    If you use Antennahouse, you can rotate the fo:block to a diagonal orientation: axf:transform="rotate(-45deg)"

    The advantage of using this approach compared to putting the text in an SVG element: in an SVG, the text cannot span more than one line. Each line has to become a separate SVG element, and you have to split the text manually. Using my method the text ends up in an fo:block which does support line breaks, hyphenation etc.