我如何可以使用Apache POI添加嵌入式方程docx文件? [英] How can I add embedded equations to docx files by using Apache POI?
问题描述
我想通过Apache POI编程创建的docx文件。
I want to create a docx file programatically via Apache POI.
我想在一些线路增加一些数学公式。
I want to add some mathematical equations in some lines.
我怎么能在某种程度上做到这一点,当用户打开该文件的docx它看到方程式等式DOCX格式。
How can I do this in a way that when the user open the docx file it see that equations as docx equation form.
我的意思是,我不想简单地给一个背景颜色到运行,我想,当用户执行双击我的方程MS-Word中公式的形式打开它。
I mean I don't want simply give a background colour to that run, I want when the user does double click on my equation MS-Word open it in equation forms.
在此先感谢
推荐答案
这是不是真的复杂的:
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.officeDocument.x2006.math.CTOMath;
import org.openxmlformats.schemas.officeDocument.x2006.math.CTRad;
import org.openxmlformats.schemas.officeDocument.x2006.math.CTR;
import org.openxmlformats.schemas.officeDocument.x2006.math.STStyle;
/*
To
import org.openxmlformats.schemas.officeDocument.x2006.math.CTOMath;
import org.openxmlformats.schemas.officeDocument.x2006.math.CTRad;
import org.openxmlformats.schemas.officeDocument.x2006.math.CTR;
import org.openxmlformats.schemas.officeDocument.x2006.math.STStyle;
the fully ooxml-schemas-1.3.jar is needed as mentioned in https://poi.apache.org/faq.html#faq-N10025
*/
public class CreateWordFormula {
public static void main(String[] args) throws Exception {
XWPFDocument doc= new XWPFDocument();
XWPFParagraph paragraph = doc.createParagraph();
XWPFRun run=paragraph.createRun();
run.setText("The Formula: ");
CTOMath cTOMath = paragraph.getCTP().addNewOMath();
CTR cTR = cTOMath.addNewR();
cTR.addNewRPr().addNewSty().setVal(STStyle.P);
cTR.addNewT2().setStringValue("a²+b²=c²");
run=paragraph.createRun();
run.setText(" text after the formula");
paragraph = doc.createParagraph();
run=paragraph.createRun();
run.setText("The Formula: ");
cTOMath = paragraph.getCTP().addNewOMath();
CTRad cTRad = cTOMath.addNewRad();
cTR = cTRad.addNewDeg().addNewR();
cTR.addNewRPr().addNewSty().setVal(STStyle.P);
cTR.addNewT2().setStringValue("2");
cTR = cTRad.addNewE().addNewR();
cTR.addNewRPr().addNewSty().setVal(STStyle.P);
cTR.addNewT2().setStringValue("a²+b²");
run=paragraph.createRun();
run.setText(" text after the formula");
doc.write(new FileOutputStream("WordFormula.docx"));
}
}
有关 CTOMath
看<一个href=\"http://grep$c$c.com/file/repo1.maven.org/maven2/org.apache.poi/ooxml-schemas/1.1/org/openxmlformats/schemas/officeDocument/x2006/math/CTOMath.java#CTOMath.addNewRad%28%29\" rel=\"nofollow\">http://grep$c$c.com/file/repo1.maven.org/maven2/org.apache.poi/ooxml-schemas/1.1/org/openxmlformats/schemas/officeDocument/x2006/math/CTOMath.java#CTOMath.addNewRad%28%29.
这篇关于我如何可以使用Apache POI添加嵌入式方程docx文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!