【问题标题】:How to print/display MathML expressions?如何打印/显示 MathML 表达式?
【发布时间】:2021-05-27 11:06:31
【问题描述】:

目前我正在尝试使用带有markup="html" 的文本字段,但它不起作用。

示例

<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement positionType="Float" stretchType="RelativeToTallestObject" x="100" y="0" width="450" height="25" isPrintWhenDetailOverflows="true" uuid="1aeaa5e9-4136-4239-a301-2733598340d9">
    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
</reportElement>
<textElement verticalAlignment="Middle" markup="html">
    <paragraph lineSpacing="Single" leftIndent="5" rightIndent="3"/>
</textElement>
<textFieldExpression><![CDATA[$F{question}]]></textFieldExpression>

$F{question} 包含:

"<p id="id"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>A</mi><mo>+</mo><mo>&#160;</mo><mi>B</mi><mo>&#160;</mo><mo>&#160;</mo><msqrt><mi>c</mi><mfenced><mrow><mi>d</mi><mfenced open="[" close="]"><mi>r</mi></mfenced></mrow></mfenced></msqrt><mo>&#160;</mo><mi>&#948;</mi><mo>&#160;</mo><mo>&#8734;</mo><mi mathvariant="normal">&#960;</mi><mo>&#160;</mo></math></p>"

预期结果:

我得到的结果:

【问题讨论】:

    标签: java jasper-reports mathml


    【解决方案1】:

    textField支持 MathML,它只支持非常基本的 html,也不能使用 html 组件,因为构建它的 JEditorPane 也不支持 MathML

    您将需要一个外部库,例如 jeuclid,一旦您在类路径中拥有此库,您就可以使用 Converter.render 将 xml 呈现为 BufferedImage,然后将其显示在 jasper 报告中。

    这可以在没有 java 帮助器类的情况下完成,如本例所示,因此将所有代码直接写入 jrxml 的 1 行(使用下面的构建器模式),但为了示例清晰起见,我将使用外部帮助器类.

    示例

    java

    public class MathML {
         public static BufferedImage getImage(String xml, float size) throws IOException, SAXException, ParserConfigurationException {          
            // Load the string to a node
            Element node =  DocumentBuilderFactory
                    .newInstance()
                    .newDocumentBuilder()
                    .parse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)))
                    .getDocumentElement();              
            //Generate the layout parameter
            MutableLayoutContext params = new LayoutContextImpl(
                    LayoutContextImpl.getDefaultLayoutContext());
            params.setParameter(Parameter.MATHSIZE, size);      
            //Render the xml to a BufferedImage
            return Converter.getInstance().render(node, params);
        }
    }
    

    jrxml

    <?xml version="1.0" encoding="UTF-8"?>
    <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Blank_A4_12" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="8deaea2e-3739-4c4e-b2b5-8c58773ab1a0">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
        <queryString>
            <![CDATA[]]>
        </queryString>
        <title>
            <band height="50" splitType="Stretch">
                <image>
                    <reportElement x="0" y="0" width="100" height="50" uuid="9ee17167-a91e-4725-b36e-a4bba5e24acb">
                    </reportElement>
                    <imageExpression><![CDATA[it.jdd.MathML.getImage("<math xmlns='http://www.w3.org/1998/Math/MathML'><mi>A</mi><mo>+</mo><mo>&#160;</mo><mi>B</mi><mo>&#160;</mo><mo>&#160;</mo><msqrt><mi>c</mi><mfenced><mrow><mi>d</mi><mfenced open='[' close=']'><mi>r</mi></mfenced></mrow></mfenced></msqrt><mo>&#160;</mo><mi>&#948;</mi><mo>&#160;</mo><mo>&#8734;</mo><mi mathvariant='normal'>&#960;</mi><mo>&#160;</mo></math>",70f)]]></imageExpression>
                </image>
            </band>
        </title>
    </jasperReport>
    

    输出(导出为pdf)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      • 1970-01-01
      • 1970-01-01
      • 2020-03-25
      • 1970-01-01
      • 2011-07-31
      相关资源
      最近更新 更多