如何在 Apache POI Word 中添加与文本内联的多个方程式?
How to add multiple equations inline with text in Apache POI Word?
我正在使用 Apache POI 将具有乳胶样式方程式的文本转换为 MS word 文档。在一些帮助下,我能够成功地实现它,但是如果该行有多个方程,那么它会产生不正确的结果。
以下是我的代码:
import java.io.*;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.officeDocument.x2006.math.CTOMath;
import org.openxmlformats.schemas.officeDocument.x2006.math.CTOMathPara;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import uk.ac.ed.ph.snuggletex.SnuggleInput;
import uk.ac.ed.ph.snuggletex.SnuggleEngine;
import uk.ac.ed.ph.snuggletex.SnuggleSession;
import java.io.IOException;
public class CreateWordFormulaFromMathML {
static File stylesheet = new File("MML2OMML.XSL");
static TransformerFactory tFactory = TransformerFactory.newInstance();
static StreamSource stylesource = new StreamSource(stylesheet);
static CTOMath getOMML(String mathML) throws Exception {
Transformer transformer = tFactory.newTransformer(stylesource);
StringReader stringreader = new StringReader(mathML);
StreamSource source = new StreamSource(stringreader);
StringWriter stringwriter = new StringWriter();
StreamResult result = new StreamResult(stringwriter);
transformer.transform(source, result);
String ooML = stringwriter.toString();
stringwriter.close();
CTOMath ctOMath = CTOMath.Factory.parse(ooML);
return ctOMath.getOMathArray(0);
}
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
String mstr = "The expression is as: $ax^2 + bx = c$ is easier to understand than $$ax^2 + \frac{\sin^{-1}\theta}{\cot{-1}} \times y_1$$ or anything in \[ ay^2 + b_2 \theta\]";
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
// run.setText("");
SnuggleEngine engine = new SnuggleEngine();
SnuggleSession session = engine.createSession();
SnuggleInput input = new SnuggleInput(mstr);
session.parseInput(input);
String mathML = session.buildXMLString();
System.out.println("Input " + input.getString() + " was converted to:\n" + mathML + "\n\n");
for(String s : mathML.split("\s+(?=<math)|(?<=</math>)\s+")){
if (s.startsWith("<math"))
{
CTOMath ctOMath = getOMML(s);
System.out.println(s);
CTP ctp = paragraph.getCTP();
ctp.setOMathArray(new CTOMath[]{ctOMath});
}
else
{
run.setText(s + " ");
System.out.println(s);
}
}
document.write(new FileOutputStream("CreateWordFormulaFromMathML.docx"));
document.close();
}
}
这会生成一个包含
的文档
表达式为: is easier to understand than or anything in ay^2+b_2 \theta
注意:(ay^2+b_2 \theta) 是正确的字方程格式。
我需要的是中间带有多项式的内联文本。
如何解决创建 Office OpenXML
文件的任务,例如 *.docx
?
Office OpenXML
文件,例如 *.docx
只是 ZIP
档案。我们可以解压缩它们并查看内部结构。在 *.docx
中我们找到 /word/document.xml
并且在那里我们找到描述文档结构的 XML
。对于具有内联公式的段落,我们发现类似:
<w:p>
<w:r>
<w:t>text</w:t>
</w:r>
<m:oMath>... </m:oMath>
<w:r>
<w:t>text</w:t>
</w:r>
<m:oMath>... </m:oMath>
...
</w:p>
所以我们需要多次运行来保存文本,并且在它们之间需要多次运行 <m:oMath>... </m:oMath>
。
这就是该段落有 OMathArray
CTOMath[]
的原因。并且您的代码确实用具有 one CTOMath
each 的新数组覆盖了此数组,同时发现了一个额外的 CTOMath
。相反,每次找到一个额外的 CTOMath
时,都需要向数组中添加一个额外的 CTOMath
。
要了解我们可以用 org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP
段落做什么,我们需要一个文档。我发现最好的是 grepcode.com. There we find CTP.addNewOMath() and CTP.setOMathArray(int, CTOMath).
所以改变你的代码:
for(String s : mathML.split("\s+(?=<math)|(?<=</math>)\s+")){
if (s.startsWith("<math")) {
CTOMath ctOMath = getOMML(s);
System.out.println(s);
CTP ctp = paragraph.getCTP();
ctp.addNewOMath();
ctp.setOMathArray(ctp.sizeOfOMathArray()-1, ctOMath);
}
else {
run = paragraph.createRun();
run.setText(s + " ");
System.out.println(s);
}
}
应该可以。
我正在使用 Apache POI 将具有乳胶样式方程式的文本转换为 MS word 文档。在一些帮助下,我能够成功地实现它,但是如果该行有多个方程,那么它会产生不正确的结果。
以下是我的代码:
import java.io.*;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.officeDocument.x2006.math.CTOMath;
import org.openxmlformats.schemas.officeDocument.x2006.math.CTOMathPara;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import uk.ac.ed.ph.snuggletex.SnuggleInput;
import uk.ac.ed.ph.snuggletex.SnuggleEngine;
import uk.ac.ed.ph.snuggletex.SnuggleSession;
import java.io.IOException;
public class CreateWordFormulaFromMathML {
static File stylesheet = new File("MML2OMML.XSL");
static TransformerFactory tFactory = TransformerFactory.newInstance();
static StreamSource stylesource = new StreamSource(stylesheet);
static CTOMath getOMML(String mathML) throws Exception {
Transformer transformer = tFactory.newTransformer(stylesource);
StringReader stringreader = new StringReader(mathML);
StreamSource source = new StreamSource(stringreader);
StringWriter stringwriter = new StringWriter();
StreamResult result = new StreamResult(stringwriter);
transformer.transform(source, result);
String ooML = stringwriter.toString();
stringwriter.close();
CTOMath ctOMath = CTOMath.Factory.parse(ooML);
return ctOMath.getOMathArray(0);
}
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
String mstr = "The expression is as: $ax^2 + bx = c$ is easier to understand than $$ax^2 + \frac{\sin^{-1}\theta}{\cot{-1}} \times y_1$$ or anything in \[ ay^2 + b_2 \theta\]";
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
// run.setText("");
SnuggleEngine engine = new SnuggleEngine();
SnuggleSession session = engine.createSession();
SnuggleInput input = new SnuggleInput(mstr);
session.parseInput(input);
String mathML = session.buildXMLString();
System.out.println("Input " + input.getString() + " was converted to:\n" + mathML + "\n\n");
for(String s : mathML.split("\s+(?=<math)|(?<=</math>)\s+")){
if (s.startsWith("<math"))
{
CTOMath ctOMath = getOMML(s);
System.out.println(s);
CTP ctp = paragraph.getCTP();
ctp.setOMathArray(new CTOMath[]{ctOMath});
}
else
{
run.setText(s + " ");
System.out.println(s);
}
}
document.write(new FileOutputStream("CreateWordFormulaFromMathML.docx"));
document.close();
}
}
这会生成一个包含
的文档表达式为: is easier to understand than or anything in ay^2+b_2 \theta
注意:(ay^2+b_2 \theta) 是正确的字方程格式。
我需要的是中间带有多项式的内联文本。
如何解决创建 Office OpenXML
文件的任务,例如 *.docx
?
Office OpenXML
文件,例如 *.docx
只是 ZIP
档案。我们可以解压缩它们并查看内部结构。在 *.docx
中我们找到 /word/document.xml
并且在那里我们找到描述文档结构的 XML
。对于具有内联公式的段落,我们发现类似:
<w:p>
<w:r>
<w:t>text</w:t>
</w:r>
<m:oMath>... </m:oMath>
<w:r>
<w:t>text</w:t>
</w:r>
<m:oMath>... </m:oMath>
...
</w:p>
所以我们需要多次运行来保存文本,并且在它们之间需要多次运行 <m:oMath>... </m:oMath>
。
这就是该段落有 OMathArray
CTOMath[]
的原因。并且您的代码确实用具有 one CTOMath
each 的新数组覆盖了此数组,同时发现了一个额外的 CTOMath
。相反,每次找到一个额外的 CTOMath
时,都需要向数组中添加一个额外的 CTOMath
。
要了解我们可以用 org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP
段落做什么,我们需要一个文档。我发现最好的是 grepcode.com. There we find CTP.addNewOMath() and CTP.setOMathArray(int, CTOMath).
所以改变你的代码:
for(String s : mathML.split("\s+(?=<math)|(?<=</math>)\s+")){
if (s.startsWith("<math")) {
CTOMath ctOMath = getOMML(s);
System.out.println(s);
CTP ctp = paragraph.getCTP();
ctp.addNewOMath();
ctp.setOMathArray(ctp.sizeOfOMathArray()-1, ctOMath);
}
else {
run = paragraph.createRun();
run.setText(s + " ");
System.out.println(s);
}
}
应该可以。