zoukankan      html  css  js  c++  java
  • Java 在Word中添加数学公式(Latex/MathML)

    本文介绍通过Java程序在Word文档中添加数学公式的方法。添加时,可添加latex数学公式或者MathML数学公式。详细内容见下文。

    1. 程序环境

    • Word测试文档:.docx 2013
    • Word jar包:free spire.doc.jar 3.9.0
    • 代码编译环境:IntelliJ IDEA
    • Jdk版本:1.8.0

    其中,jar导入可分手动导入或者maven仓库下载导入。

    1.1 手动导入下载jar包,解压并将lib文件夹下的jar文件导入程序,如图1;

    图1

    1.2 Maven导入:需配置pom.xml,如下,

    <repositories>
            <repository>
                <id>com.e-iceblue</id>
                <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
            </repository>
        </repositories>
    <dependencies>
        <dependency>
            <groupId> e-iceblue </groupId>
            <artifactId>spire.doc.free</artifactId>
            <version>3.9.0</version>
        </dependency>
    </dependencies>

    点击“Import Changes”下载导入,如图2,

    图2

    Maven中导入jar结果,如图3:

    图3

    2. Java代码

    import com.spire.doc.*;
    import com.spire.doc.documents.Paragraph;
    import com.spire.doc.fields.omath.OfficeMath;
    
    public class AddFormular {
        public static void main(String[] args) {
            //新建Word示例,添加一个section
            Document doc = new Document();
            Section section = doc.addSection();
    
            //添加段落1和段落2,添加Latex数学公式
            Paragraph paragraph1 = section.addParagraph();
            OfficeMath officeMath1 = new OfficeMath(doc);
            paragraph1.getItems().add(officeMath1);
            officeMath1.fromLatexMathCode("$f(x, y) = 100 * \lbrace[(x + y) * 3] - 5\rbrace$");
    
            Paragraph paragraph2 = section.addParagraph();
            OfficeMath officeMath2 = new OfficeMath(doc);
            paragraph2.getItems().add(officeMath2);
            officeMath2.fromLatexMathCode("$S=a_{1}^2+a_{2}^2+a_{3}^2$");
    
            //添加段落3,插入MathML数学公式
            Paragraph paragraph3 = section.addParagraph();
            OfficeMath officeMath3 = new OfficeMath(doc);
            paragraph3.getItems().add(officeMath3);
            officeMath3.fromMathMLCode("<mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"><mml:msup><mml:mrow><mml:mi>x</mml:mi></mml:mrow><mml:mrow><mml:mn>2</mml:mn></mml:mrow></mml:msup><mml:mo>+</mml:mo><mml:msqrt><mml:msup><mml:mrow><mml:mi>x</mml:mi></mml:mrow><mml:mrow><mml:mn>2</mml:mn></mml:mrow></mml:msup><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:msqrt><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:math>");
            //保存文档
            doc.saveToFile("addMathEquation.docx", FileFormat.Docx_2013);
            doc.dispose();
        }
    }

    执行程序,生成Word文档,可查看如下公式添加结果,如图4:

    图4

    原创内容,如需转载,请务必注明出处

    -End-

  • 相关阅读:
    分割回文串(力扣第131题)
    子集 II(力扣第91题)
    子集(力扣第78题)
    组合总和 III(力扣第216题)
    JavaWeb部分 (前序)
    JavaSE部分 (IO流下)
    JavaSE部分 (IO流上)
    JavaSE部分 (File类)
    Leetcode 08.04 幂集 回溯与位图
    Leetcode 1405 最长快乐字符串 穷举与贪心
  • 原文地址:https://www.cnblogs.com/Yesi/p/14781544.html
Copyright © 2011-2022 走看看