XSL-FO Region-Body 背景图像

XSL-FO Region-Body background image

我必须向页面添加背景图像并在页面上添加一些 text/content。我是 XSL-FO 的新手,所以我做了一些研究。看起来我需要使用 Region-Body 并使用 background-image 属性添加图像。

<?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" xmlns:fox="http://xmlgraphics.apache.org/fop/extensions">

  <!-- Entry point -->
  <xsl:template match="/">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:fox="http://xmlgraphics.apache.org/fop/extensions">

      <fo:layout-master-set>

        <fo:simple-page-master master-name="cover-page" page-height="21cm" page-width="29.7cm">
          <fo:region-body background-image="url('Cover.jpg')" fox:background-image-width="29.7cm" fox:background-image-height="21cm"/>
        </fo:simple-page-master>

      </fo:layout-master-set>


      <!-- Cover Page -->
      <fo:page-sequence master-reference="cover-page" force-page-count="no-force" format="i">
        <fo:flow flow-name="xsl-region-body">
          <xsl:call-template name="tpltCoverPage"/>
        </fo:flow>
      </fo:page-sequence>


    </fo:root>
  </xsl:template>

  <!-- Cover Page -->
  <xsl:template name="tpltCoverPage">
    <fo:block></fo:block>
  </xsl:template>

</xsl:stylesheet>

图像显示为背景,但它是 1700 * 1200,因此图像被放大,我只能在背景中看到部分图像。无论如何我可以缩小图像以适合页面高度和宽度(而不改变实际图像)?

仅使用 XSL 1.1 属性,您可以定位背景图像,但不能缩放它。

FOP 具有用于设置背景图像的宽度和高度的扩展属性:https://xmlgraphics.apache.org/fop/2.3/extensions.html#backgroundimages

(AH Formatter 可以做到这一点,甚至更多:https://www.antennahouse.com/product/ahf66/ahf-ext.html#background


仅格式化您示例中的 XSL-FO 对我来说适用于 FOP 2.2:

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"
     xmlns:fox="http://xmlgraphics.apache.org/fop/extensions">
  <fo:layout-master-set>
    <fo:simple-page-master master-name="cover-page"
               page-height="21cm" page-width="29.7cm">
      <fo:region-body background-image="url('Cover.jpg')"
          fox:background-image-width="29.7cm"
          fox:background-image-height="21cm"/>
    </fo:simple-page-master>
  </fo:layout-master-set>

  <!-- Cover Page -->
  <fo:page-sequence master-reference="cover-page"
        force-page-count="no-force" format="i">
    <fo:flow flow-name="xsl-region-body">
      <fo:block></fo:block>
    </fo:flow>
  </fo:page-sequence>
</fo:root>