从 Android 上的演示文稿导出数学公式

简介

Aspose.Slides for Android via Java 允许您从演示文稿中导出数学公式。例如,您可能需要提取幻灯片(特定演示文稿)上的数学公式,并在其他程序或平台中使用它们。

导出数学公式为 LaTeX

Aspose.Slides 可以直接将 PowerPoint 数学公式转换为 LaTeX;无需中间的 MathML 文件或外部转换器。数学公式以 IMathPortion 的形式存储在文本框中。使用 IMathPortion.getMathParagraph 获取一个 IMathParagraph,然后调用 IMathParagraph.toLatex。该方法返回一个字符串,您可以保存、显示、发送到其他应用程序或进一步处理。

以下示例检查每张幻灯片上的每个文本框,查找所有数学部分,并将每个公式写入单独的 .tex 文件:

Presentation presentation = new Presentation("equations.pptx");
try {
    int slideCount = presentation.getSlides().size();
    for (int slideIndex = 0; slideIndex < slideCount; slideIndex++) {
        ISlide slide = presentation.getSlides().get_Item(slideIndex);
        int slideNumber = slideIndex + 1;
        int equationNumber = 1;
        ITextFrame[] textFrames = SlideUtil.getAllTextBoxes(slide);

        for (ITextFrame textFrame : textFrames) {
            for (IParagraph paragraph : textFrame.getParagraphs()) {
                for (IPortion portion : paragraph.getPortions()) {
                    if (!(portion instanceof IMathPortion))
                        continue;

                    IMathPortion mathPortion = (IMathPortion) portion;
                    IMathParagraph mathParagraph = mathPortion.getMathParagraph();
                    String latexFileName = "slide_" + slideNumber + "_equation_" + equationNumber + ".tex";

                    String latexText = mathParagraph.toLatex();
                    File latexFile = new File(latexFileName);
                    byte[] latexBytes = latexText.getBytes(StandardCharsets.UTF_8);
                    FileOutputStream outputStream = new FileOutputStream(latexFile);
                    try {
                        outputStream.write(latexBytes);
                    } finally {
                        outputStream.close();
                    }
                    equationNumber++;
                }
            }
        }
    }
} finally {
    presentation.dispose();
}

SlideUtil.getAllTextBoxes 返回幻灯片上找到的所有文本框。IMathPortion 类型检查可将真正可编辑的公式与普通文本和图像区分开来。

LaTeX 引擎和文档模板并不全部支持相同的命令、宏包或 Unicode 字符。请使用您应用程序所使用的 LaTeX 引擎测试返回的字符串。如果某个符号或 Office Math 元素在该环境中没有合适的表示方式,请在返回的字符串中用项目特定的命令替换它,或跳过该公式并记录问题以供审查。

将数学公式保存为 MathML

尽管人类可以轻松编写 LaTeX 等某些公式格式的代码,但编写 MathML 的代码却较为困难,因为后者通常由应用程序自动生成。程序能够轻松读取和解析 MathML,因为其代码采用 XML 结构,所以 MathML 在许多领域被广泛用作输出和打印格式。

以下示例代码演示如何将演示文稿中的数学公式导出为 MathML:

Presentation pres = new Presentation();
try {
    IAutoShape autoShape = pres.getSlides().get_Item(0).getShapes().addMathShape(0, 0, 500, 50);
    IMathParagraph mathParagraph = ((MathPortion)autoShape.getTextFrame().getParagraphs().get_Item(0).
            getPortions().get_Item(0)).getMathParagraph();

    mathParagraph.add(new MathematicalText("a").
            setSuperscript("2").
            join("+").
            join(new MathematicalText("b").setSuperscript("2")).
            join("=").
            join(new MathematicalText("c").setSuperscript("2")));

    FileOutputStream stream = new FileOutputStream("mathml.xml");
    mathParagraph.writeAsMathMl(stream);
} catch (IOException e) {
} finally {
    if (pres != null) pres.dispose();
}

常见问题

到底是将段落还是单个公式块导出为 MathML?

您可以将整个数学段落(MathParagraph)或单个块(MathBlock)导出为 MathML。这两种类型都提供了写入 MathML 的方法。

如何判断幻灯片上的对象是数学公式而不是普通文本或图像?

公式存在于 MathPortion 中,并且拥有一个 MathParagraph。没有 MathParagraph 的图像和普通文本部分不是可导出的公式。

演示文稿中的 MathML 来源是什么——是 PowerPoint 特有的还是标准?

导出面向标准的 MathML(XML)。Aspose 使用的是 Presentation MathML——该标准的呈现子集,已在各类应用程序和 Web 中得到广泛使用。

是否支持导出位于表格、SmartArt、组合等中的公式?

是的,如果这些对象包含带有 MathParagraph 的文本部分(即真实的 PowerPoint 公式),则会被导出。若公式以图像形式嵌入,则不会导出。

导出为 MathML 会修改原始演示文稿吗?

不会。将公式写入 MathML 仅是对其内容的序列化,不会改动演示文稿文件。