首页 > 解决方案 > 外部图形相对路径

问题描述

如何将 fo:external-graphic 与相对路径一起使用?

我一直在到处寻找,但找不到答案。大多数答案都谈到将图像放入我的 Java 项目的资源中,但这对我来说不起作用,因为它是生成运行时的图像。

所以简而言之..我可以将某种变量放入我的 XSL 文档中,然后通过回调在我的 java 代码中设置它吗?

<fo:table-cell>
    <fo:block margin-top="23mm">
        <fo:external-graphic src="url({$ImagePath}CAPTION.PNG)" height="17mm" 
                             content-height="scale-to-fit"      content-width="65mm"
                             scaling="non-uniform"/>
    </fo:block>
</fo:table-cell>

标签: javajava-8apache-fop

解决方案


我找到了解决方案!参数!

首先,在您的 XSL 文档中,您必须定义您的变量:

然后稍后您可以使用它:

<fo:table-cell>
    <fo:block margin-top="23mm">
        <fo:external-graphic src="url({$ImagePath}CAPTION.PNG)" height="17mm" 
                             content-height="scale-to-fit"      content-width="65mm"
                             scaling="non-uniform"/>
    </fo:block>
</fo:table-cell>

在呈现您的文档之前,您已经在 J​​AVA 代码中设置了变量:

// 设置 XSLT TransformerFactory factory = TransformerFactory.newInstance(); 变压器变压器 = factory.newTransformer(new StreamSource(xsltFile));

        // Resulting SAX events (the generated FO) must be piped through to FOP
        Result res = new SAXResult(fop.getDefaultHandler());

        transformer.setParameter("ImagePath", "C:/Temp/Poland/");
        transformer.transform(xmlSource, res);

推荐阅读