Search code examples
xsl-foapache-fop

Setting XSL FO background image from embedded SVG


I am creating a background text within a XSL FO document this way:

<svg:svg width="285" height="70">
      <svg:g transform="rotate(-5)">
            <svg:text x="10" y="60" style="font-family:Courier;font-size:40;font-weight:normal;stroke-width:0.5;fill:none;stroke:lightgray;stroke-opacity:0.75;">
                  Background Watermark Text 
            </svg:text>
      </svg:g>
</svg:svg>

Is there any way to display this SVG in background of the page? The problem is that importing an external image as watermark works fine, but I do not find any way to set this embedded SVG as background image...


Solution

  • Here's one way. To do a background-image on a page, you can set the extent of the region-after to the height of the page and then use the static-content of that region to place the image. I did nothing special with your image, but doing it this way, you could make the SVG the size of the page and then do a nice job centering and such.

    (I changed the color to make it easier to see):

        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"
            xmlns:rx="http://www.renderx.com/XSL/Extensions">
            <fo:layout-master-set>
                <fo:simple-page-master page-width="8in" page-height="11in" master-name="first">
                    <fo:region-body margin-top="1in" margin-left="1in" margin-bottom="1in"
                        margin-right="1in"/>
                    <fo:region-before extent="0pt"/>
                    <fo:region-after extent="11in"/>
                </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="first">
                <fo:static-content flow-name="xsl-region-before">
                    <fo:block line-height="0">
                        <fo:instream-foreign-object>
                            <svg:svg width="285" height="70" xmlns:svg="http://www.w3.org/2000/svg">
                                <svg:g transform="rotate(-5)">
                                    <svg:text x="10" y="60" style="font-family:Courier;font-size:40;font-weight:normal;stroke-width:0.5;fill:cyan;stroke:lightgray;stroke-opacity:0.75;">
                                        Background Watermark Text 
                                    </svg:text>
                                </svg:g>
                            </svg:svg>
                        </fo:instream-foreign-object>
                    </fo:block>
                </fo:static-content>
                <fo:flow flow-name="xsl-region-body">
                    <fo:block>
                        I am a happy little block in the page. I am a happy little block in the page. I am a happy little block in the page. I am a happy little block in the page. 
                    </fo:block>
                    <fo:block>
                        I am a happy little block in the page. I am a happy little block in the page. I am a happy little block in the page. I am a happy little block in the page. 
                    </fo:block>
                    <fo:block>
                        I am a happy little block in the page. I am a happy little block in the page. I am a happy little block in the page. I am a happy little block in the page. 
                    </fo:block>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    

    Result using Apache FOP:

    enter image description here

    Of course, you can adjust your positioning, size, rotation, etc. I use a bit different Watermark SVG, here for you if you wish:

            <svg width="612pt" height="792pt" xmlns="http://www.w3.org/2000/svg" version="1.1">
                <text transform="translate(306pt, 396pt) rotate(47)" text-anchor="middle" fill="rgb(255,0,0)" font-family="Helvetica" font-size="92pt" stroke="rgb(255,0,0)" fill-opacity="0.07">WATERMARK</text>
            </svg>
    

    However, I have only tested that second Watermark with RenderX XEP. It does not work with FOP as something is not supported.

    Sample:

    enter image description here