Java 在Word中添加数学公式(Latex/MathML)
2024-10-19 15:27:33
本文介绍通过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-
最新文章
- MySQL学习笔记十六:锁机制
- httpd的警告
- VBA_Excel_教程:变量,数组
- Contest Hunter Round #70 - 连续两大交易事件杯省选模拟赛
- [转] 判断html页是否滚动停止
- JNI之HelloWorld
- SilkTest Q&;A 10
- JGUI源码:实现简单进度条(19)
- vmware彻底隐藏控制栏白条
- Python爬虫(2):urllib库
- c指针存放的是常量的地址
- python词云
- Sql Server 修改表所属用户
- selenium元素定位Xpath,Contains,CssSelector
- linux内核分析第二周
- Windows XP 安装使用的序列号
- telerik WinForm主题切换
- 《C++ Primer Plus》12.6 复习各种(类和动态内存分配的)技术 笔记
- centos7开机不进入图形界面
- Linux 安装tomcat 及过程中遇到的问题
热门文章
- python报错:AttributeError: module &#39;pdb&#39; has no attribute &#39;set_trace&#39;
- C#控制鼠标自动连续点(DEMO)
- 【wp】2021V&;NCTF
- Cookie与Session的安全性
- AggregateReport V2.2.0
- Android 之 使用 Intent 在活动间传递数据
- Linux 软链接link/ln -s
- TCP 和 UDP 协议简介
- 软件工程第一次作业:Warm Up
- 一次使用IDEA中HTTP Client的经历