環(huán)境
核心代碼
依賴項(xiàng)
docx4j
:操作word文檔(.docx)
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j</artifactId>
<version>6.0.1</version>
</dependency>
<!--下面這個(gè)jar好像不引入也沒問題-->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.1</version>
</dependency>
latex-word(第三方j(luò)ar包)
:將latex值轉(zhuǎn)成符合word xml標(biāo)準(zhǔn)的工具
github項(xiàng)目地址為:https://github.com/mao-yuwei/latex_word
相關(guān)文章為:https://blog.csdn.net/weixin_30633405/article/details/98518909
關(guān)鍵步驟
-
調(diào)用
com.latextoword.Latex_Word.latexToWordAlreadyClean
方法獲取到latex對(duì)應(yīng)的xml字符串提佣,例如:<m:oMath>......</m:oMath>
-
給根節(jié)點(diǎn)添加必要(非常重要,否則生成的文檔將無法正常打開)的
namespace
:xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\"
<m:oMath xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">......</m:oMath>
調(diào)用
org.docx4j.XmlUtils.unmarshalString
方法將最終拼接好的xml字符串解析為Object將轉(zhuǎn)換好的對(duì)象add到word的元素對(duì)象中(諸如org.docx4j.wml.P)
-
最終效果如下
代碼參考
注意事項(xiàng)
參考
https://blog.csdn.net/weixin_30633405/article/details/98518909
https://github.com/mao-yuwei/latex_word
https://github.com/plutext/docx4j/blob/master/docx4j-samples-docx4j/src/main/java/org/docx4j/samples/MathsEquationsFormulae.java