XSL 转换后保留 PDF 中的空格 [英] Preserving whitespace in PDF after XSL transform
问题描述
我正在使用 XSL 转换将 XML 转换为 PDF.不幸的是,它没有保留 XML 中的空格.例如:
I'm converting an XML to PDF using XSL transformation. Unfortunately, it is not preserving the whitespace from the XML. For example:
我想转换这个:
Test Line Data : 0xAA
从 XML 到 PDF.它在 XML 中看起来不错,它在 Data 和 : 之间有 9 个空格,但在 PDF 中它显示
to PDF from an XML. It looks fine in the XML, it has the 9 spaces between Data and : but in the PDF it shows
Test Line Data : 0xAA
这是我目前所做的.将数据写入 XML 后,我将执行以下操作:
Here is what I currently do. After writing the data to the XML, I will perform the following:
XPathDocument xPathDocDiag = new XPathDocument(this.FileNameDiagXml);
XslCompiledTransform xslTransDiag = new XslCompiledTransform();
XmlTextWriter xmlWriterDiag = new XmlTextWriter(outputFO, System.Text.Encoding.UTF8);
xslTransDiag.Transform(xPathDocDiag, null, xmlWriterDiag);
xmlWriterDiag.Flush();
xmlWriterDiag.Close();
然后启动 Apache FOP 将 FO 转换为 PDF.就像我说的,不幸的是,空白在我需要时没有保留.我曾尝试手动添加 代替 XML(查找和替换)中的空格,这在转换后确实有效,但众所周知,文字 &不能在 XML 中,因此该选项已失效.我曾尝试使用 XmlReader,然后使用
And then launch Apache FOP to convert the FO to PDF. Like I said, unfortunately, the white space is not preserved when I need it to be. I have tried manually adding in place of spaces in the XML (find and replace) which does work after the conversion, but as we all know, the literal & can't be in XML so that option is out. I have tried using XmlReader and then using
<xsl:preserve-space elements="*"/>
但同样,这也不起作用(没有错误或任何东西,只是不起作用).
XSL 部分如下所示:
but again, that doesn't work either (no error or anything, just doesn't work).
The section of XSL looks like so:
<xsl:when test="Data != ''">
<fo:table-cell text-align="left">
<fo:block />
</fo:table-cell>
<fo:table-cell text-align="left" number-columns-spanned="7">
<fo:block font-family="Courier New, Courier, monospace" font-size="9pt"><xsl:value-of select="Data" /></fo:block>
</fo:table-cell>
</xsl:when>
我尝试了各种属性修改都无济于事.我在这里遗漏了一些明显的东西吗?
I have tried all sorts of attribute modifying to no avail. Am I missing something obvious here?
推荐答案
将属性 white-space="pre"
添加到您想要保留的任何 fo:block
中的空白.
Add the attribute white-space="pre"
to whatever fo:block
you want to preserve the white-space in.
示例:
<fo:block white-space="pre">Test Line Data : 0xAA</fo:block>
属性 white-space-treatment="preserve"
也应该有效,但是当我在 FOP 中尝试时,它没有.
The attribute white-space-treatment="preserve"
should also work, but when I tried it in FOP, it didn't.
属性 white-space-collapse="false"
也有效.
这篇关于XSL 转换后保留 PDF 中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!