导出 Java 中演示文稿的数学公式

Introduction

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

Export Math Equations to 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();
                    Path latexPath = Paths.get(latexFileName);
                    byte[] latexBytes = latexText.getBytes(StandardCharsets.UTF_8);
                    Files.write(latexPath, latexBytes);
                    equationNumber++;
                }
            }
        }
    }
} finally {
    presentation.dispose();
}

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

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

Save Math Equations as 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();
}

FAQ

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

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

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

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

演示文稿中的 MathML 来自何处——是 PowerPoint 特有的还是标准?

导出面向标准 MathML(XML)。Aspose 使用演示 MathML——即标准的呈现子集,已在各类应用和网络中广泛使用。

是否支持导出表格、SmartArt、组合等内部的公式?

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

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

不会。写入 MathML 只是对公式内容的序列化,不会修改演示文稿文件。