-
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
原创内容,如需转载,请务必注明出处!
原文:https://www.cnblogs.com/Yesi/p/14781544.html
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
数据库审计与智能监控:从日志分析到异
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比