i want to create a Mathematical expression,below is my code
- Code: Select all
package org;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import org.docx4j.math.CTOMath;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
public class TestMathFormula
{
private static org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();
public static void insertSubSuper(String key,MainDocumentPart documentPart,WordprocessingMLPackage wordMLPackage)throws Exception
{
String xpath = "//w:r[w:t[contains(text(),'"+key+"')]]";
List<Object> list = documentPart.getJAXBNodesViaXPath(xpath, false);
for(int i=0;i<list.size();i++)
{
org.docx4j.wml.R r = (org.docx4j.wml.R) list.get(i);
org.docx4j.wml.P parent = (org.docx4j.wml.P) r.getParent();
org.docx4j.wml.RPr rpr=r.getRPr();
int index =parent.getContent().indexOf(r);
System.out.println(index);
parent.getContent().remove(r);
org.docx4j.math.ObjectFactory mathFactory=new org.docx4j.math.ObjectFactory();
org.docx4j.math.CTOMathPara para=mathFactory.createCTOMathPara();
CTOMath oMath=mathFactory.createCTOMath();
String mr="<m:r xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\"><m:rPr><m:sty m:val=\"p\" /> " +
"</m:rPr><w:rPr><w:rFonts w:ascii=\"Cambria Math\" w:hAnsi=\"Cambria Math\" /> " +
"<w:sz w:val=\"30\" /> " +
"<w:szCs w:val=\"30\" /> " +
"</w:rPr><m:t>${mainDesc}</m:t> </m:r>";
String mpr=" <m:mPr xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\"><m:rPr><m:sty m:val=\"p\" /><m:mcs><m:mc><m:mcPr><m:count m:val=\"1\" /> " +
"<m:mcJc m:val=\"center\" /> </m:mcPr></m:mc></m:mcs><m:ctrlPr><w:rPr>" +
"<w:rFonts w:ascii=\"Cambria Math\" w:hAnsi=\"Cambria Math\" /> <w:sz w:val=\"30\" />" +
" <w:szCs w:val=\"30\" /> </w:rPr></m:ctrlPr></m:mPr>";
String mmr1=retMmr("2");
String mmr2=retMmr("3");
HashMap<String, String> mappings = new HashMap<String, String>();
mappings.put("mainDesc", "C");
Object mrObj=org.docx4j.XmlUtils.unmarshallFromTemplate(mr, mappings);
oMath.getEGOMathElements().add((org.docx4j.math.CTR)mrObj);
Object mprObj=org.docx4j.XmlUtils.unmarshalString(mpr);
Object mmr1Obj=org.docx4j.XmlUtils.unmarshalString(mmr1);
Object mmr2Obj=org.docx4j.XmlUtils.unmarshalString(mmr2);
org.docx4j.math.CTM ctm=mathFactory.createCTM();
ctm.setMPr((org.docx4j.math.CTMPr)mprObj);
ctm.getMr().add((org.docx4j.math.CTMR)mmr1Obj);
ctm.getMr().add((org.docx4j.math.CTMR)mmr2Obj);
oMath.getEGOMathElements().add(ctm);
para.getOMath().add(oMath);
parent.getContent().add(para);
}
}
public static String retMmr(String val)
{
String mmr="<m:mr xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\"><m:rPr><m:sty m:val=\"p\" /><m:e><m:r><m:rPr><m:sty m:val=\"p\" /></m:rPr><w:rPr>" +
"<w:rFonts w:ascii=\"Cambria Math\" w:hAnsi=\"Cambria Math\" />" +
"<w:sz w:val=\"30\" /><w:szCs w:val=\"30\" /></w:rPr><m:t>"+val+"</m:t></m:r></m:e></m:mr>";
return mmr;
}
public static void main(String[] args) throws Exception
{
String inputfilepath = "c:/workspace/aa.docx";
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.load(new java.io.File(inputfilepath));
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
insertSubSuper("label2",documentPart,wordMLPackage);
wordMLPackage.save(new File("c:/workspace/aa_out.docx"));
}
}
below is Exception after the code runs
- Code: Select all
Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.openxmlformats.org/officeDocument/2006/math", local:"r"). Expected elements are <{urn:schemas-microsoft-com:office:excel}ClientData>,<{urn:schemas-microsoft-com:office:office}OLEObject>,<{http://opendope.org/SmartArt/DataHierarchy}SmartArtDataHierarchy>,<{http://schemas.openxmlformats.org/officeDocument/2006/bibliography}Sources>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}alias>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}altChunk>,<{http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing}anchor>,<{urn:schemas-microsoft-com:office:word}anchorlock>,<{http://opendope.org/conditions}and>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}annotationRef>,<{urn:schemas-microsoft-com:vml}arc>,<{urn:schemas-microsoft-com:vml}background>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}bibliography>,<{http://schemas.openxmlformats.org/drawingml/2006/main}blip>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}body>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}bookmarkEnd>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}bookmarkStart>,<{urn:schemas-microsoft-com:office:word}borderbottom>,<{urn:schemas-microsoft-com:office:word}borderleft>,<{urn:schemas-microsoft-com:office:word}borderright>,<{urn:schemas-microsoft-com:office:word}bordertop>,<{urn:schemas-microsoft-com:office:office}bottom>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}br>,<{urn:schemas-microsoft-com:office:office}callout>,<{http://schemas.openxmlformats.org/drawingml/2006/chart}chart>,<{http://schemas.openxmlformats.org/drawingml/2006/chart}chartSpace>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}citation>,<{urn:schemas-microsoft-com:office:office}clippath>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}color>,<{http://schemas.openxmlformats.org/drawingml/2006/diagram}colorsDef>,<{http://schemas.openxmlformats.org/drawingml/2006/diagram}colorsDefHdr>,<{http://schemas.openxmlformats.org/drawingml/2006/diagram}colorsDefHdrLst>,<{urn:schemas-microsoft-com:office:office}column>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}commentRangeEnd>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}commentRangeStart>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}commentReference>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}comments>,<{urn:schemas-microsoft-com:office:office}complex>,<{http://opendope.org/components}components>,<{http://opendope.org/conditions}condition>,<{http://opendope.org/conditions}conditionref>,<{http://opendope.org/conditions}conditions>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}continuationSeparator>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}cr>,<{urn:schemas-microsoft-com:vml}curve>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}dataBinding>,<{http://schemas.openxmlformats.org/drawingml/2006/diagram}dataModel>,<{http://schemas.microsoft.com/office/drawing/2008/diagram}dataModelExt>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}dayLong>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}dayShort>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}del>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}delText>,<{urn:schemas-microsoft-com:office:office}diagram>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}docDefaults>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}document>,<{http://schemas.microsoft.com/office/drawing/2008/diagram}drawing>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}drawing>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}endnoteRef>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}endnotes>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}equation>,<{urn:schemas-microsoft-com:office:office}extrusion>,<{http://opendope.org/conditions}false>,<{urn:schemas-microsoft-com:office:office}fill>,<{urn:schemas-microsoft-com:vml}fill>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}fldChar>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}fldSimple>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}fonts>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}footerReference>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}footnoteRef>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}footnotes>,<{urn:schemas-microsoft-com:vml}formulas>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}ftr>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}glossaryDocument>,<{http://schemas.openxmlformats.org/drawingml/2006/main}graphic>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}group>,<{urn:schemas-microsoft-com:vml}group>,<{urn:schemas-microsoft-com:vml}handles>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}hdr>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}headerReference>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}highlight>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}hyperlink>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}id>,<{urn:schemas-microsoft-com:vml}image>,<{http://opendope.org/SmartArt/DataHierarchy}imageRef>,<{urn:schemas-microsoft-com:vml}imagedata>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}ind>,<{urn:schemas-microsoft-com:office:office}ink>,<{http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing}inline>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}ins>,<{urn:schemas-microsoft-com:office:powerpoint}iscomment>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}jc>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}lastRenderedPageBreak>,<{http://schemas.openxmlformats.org/drawingml/2006/diagram}layoutDef>,<{http://schemas.openxmlformats.org/drawingml/2006/diagram}layoutDefHdr>,<{http://schemas.openxmlformats.org/drawingml/2006/diagram}layoutDefHdrLst>,<{urn:schemas-microsoft-com:office:office}left>,<{http://schemas.openxmlformats.org/drawingml/2006/compatibility}legacyDrawing>,<{urn:schemas-microsoft-com:vml}line>,<{http://opendope.org/SmartArt/DataHierarchy}list>,<{http://opendope.org/SmartArt/DataHierarchy}listItem>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}lock>,<{urn:schemas-microsoft-com:office:office}lock>,<{http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas}lockedCanvas>,<{http://schemas.openxmlformats.org/officeDocument/2006/math}mathPr>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}monthLong>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}monthShort>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}noBreakHyphen>,<{http://opendope.org/conditions}not>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}numPr>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}numbering>,<{http://schemas.openxmlformats.org/officeDocument/2006/math}oMath>,<{http://schemas.openxmlformats.org/officeDocument/2006/math}oMathPara>,<{http://opendope.org/conditions}or>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}outlineLvl>,<{urn:schemas-microsoft-com:vml}oval>,<{http://opendope.org/SmartArt/DataHierarchy}p>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}p>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}pBdr>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}pPr>,<{urn:schemas-microsoft-com:vml}path>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}permEnd>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}permStart>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}pgNum>,<{http://schemas.openxmlformats.org/drawingml/2006/picture}pic>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}pict>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}picture>,<{urn:schemas-microsoft-com:vml}polyline>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}proofErr>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}ptab>,<{http://opendope.org/questions}question>,<{http://opendope.org/questions}questionnaire>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}r>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}rFonts>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}rPr>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}rStyle>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}recipients>,<{urn:schemas-microsoft-com:vml}rect>,<{http://schemas.openxmlformats.org/drawingml/2006/diagram}relIds>,<{http://opendope.org/questions}response>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}richText>,<{urn:schemas-microsoft-com:office:office}right>,<{urn:schemas-microsoft-com:vml}roundrect>,<{http://schemas.openxmlformats.org/schemaLibrary/2006/main}schemaLibrary>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}sdt>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}sdtContent>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}sdtPr>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}sectPr>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}separator>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}settings>,<{urn:schemas-microsoft-com:vml}shadow>,<{urn:schemas-microsoft-com:vml}shape>,<{urn:schemas-microsoft-com:office:office}shapedefaults>,<{urn:schemas-microsoft-com:office:office}shapelayout>,<{urn:schemas-microsoft-com:vml}shapetype>,<{http://opendope.org/SmartArt/DataHierarchy}sibTransBody>,<{urn:schemas-microsoft-com:office:office}signatureline>,<{urn:schemas-microsoft-com:office:office}skew>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}softHyphen>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}spacing>,<{urn:schemas-microsoft-com:vml}stroke>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}style>,<{http://schemas.openxmlformats.org/drawingml/2006/diagram}styleDef>,<{http://schemas.openxmlformats.org/drawingml/2006/diagram}styleDefHdr>,<{http://schemas.openxmlformats.org/drawingml/2006/diagram}styleDefHdrLst>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}styles>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}sym>,<{http://schemas.openxmlformats.org/drawingml/2006/diagram}t>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}t>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}tab>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}tabs>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}tag>,<{http://schemas.openxmlformats.org/drawingml/2006/main}tbl>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}tbl>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}tblGrid>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}tblPr>,<{http://schemas.openxmlformats.org/drawingml/2006/main}tblStyleLst>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}tc>,<{http://opendope.org/SmartArt/DataHierarchy}textBody>,<{urn:schemas-microsoft-com:vml}textbox>,<{urn:schemas-microsoft-com:office:powerpoint}textdata>,<{urn:schemas-microsoft-com:vml}textpath>,<{http://schemas.openxmlformats.org/drawingml/2006/main}theme>,<{http://schemas.openxmlformats.org/drawingml/2006/main}themeManager>,<{http://schemas.openxmlformats.org/drawingml/2006/main}themeOverride>,<{urn:schemas-microsoft-com:office:office}top>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}tr>,<{http://opendope.org/conditions}true>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}txbxContent>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}u>,<{http://schemas.openxmlformats.org/drawingml/2006/chart}userShapes>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}webSettings>,<{urn:schemas-microsoft-com:office:word}wrap>,<{http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing}wsDr>,<{}xml>,<{http://opendope.org/conditions}xpathref>,<{http://opendope.org/xpaths}xpaths>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}yearLong>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}yearShort>
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:616)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:244)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:239)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:116)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1022)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:452)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:433)
at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:137)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:211)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:184)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:105)
at org.docx4j.XmlUtils.unmarshalString(XmlUtils.java:275)
at org.docx4j.XmlUtils.unmarshallFromTemplate(XmlUtils.java:323)
at org.docx4j.XmlUtils.unmarshallFromTemplate(XmlUtils.java:316)
at org.TestMathFormula.insertSubSuper(TestMathFormula.java:54)
at org.TestMathFormula.main(TestMathFormula.java:95)
please help me ,how do i modify code?
thanks in advance!
tosswang