How to Insert OfficeMath into Document using Java

how to definition a OfficeMath use microsoft office omml?

String omml = "<xml-fragment xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" xmlns:mml=\"http://www.w3.org/1998/Math/MathML\"><m:bar><m:barPr><m:pos m:val=\"top\"/></m:barPr><m:e><m:r><main:rPr xmlns:main=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><main:rFonts main:ascii=\"Cambria Math\" main:hAnsi=\"Cambria Math\"/></main:rPr><m:t>z</m:t></m:r></m:e></m:bar></xml-fragment>\r\n" ;
OfficeMath mymath = new OfficeMath(WordDoc, omml);

this code is error ,OfficeMath report error ,how to use it ?

thanks.

@hlgao

You can use DocumentBuilder.insertHtml method as shown below to insert the OfficeMath into document.

String omml = "<xml-fragment xmln.......></m:bar>\r\n" ;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertHtml(omml);
doc.save(MyDir + "20.12.docx");

@tahir.manzoor
Thank you for your patience, but this is not the result I want.

String omml = "<xml-fragment xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" xmlns:mml=\"http://www.w3.org/1998/Math/MathML\" xmlns:main=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">\r\n" + 
 		"  <m:sSup>\r\n" + 
 		"    <m:e>\r\n" + 
 		"      <m:bar>\r\n" + 
 		"        <m:barPr>\r\n" + 
 		"          <m:pos m:val=\"top\"/>\r\n" + 
 		"        </m:barPr>\r\n" + 
 		"        <m:e>\r\n" + 
 		"          <m:r>\r\n" + 
 		"            <main:rPr>\r\n" + 
 		"              <main:rFonts main:ascii=\"Cambria Math\" main:hAnsi=\"Cambria Math\"/>\r\n" + 
 		"            </main:rPr>\r\n" + 
 		"            <m:t>z</m:t>\r\n" + 
 		"          </m:r>\r\n" + 
 		"        </m:e>\r\n" + 
 		"      </m:bar>\r\n" + 
 		"    </m:e>\r\n" + 
 		"    <m:sup>\r\n" + 
 		"      <m:r>\r\n" + 
 		"        <main:rPr>\r\n" + 
 		"          <main:rFonts main:ascii=\"Cambria Math\" main:hAnsi=\"Cambria Math\"/>\r\n" + 
 		"        </main:rPr>\r\n" + 
 		"        <m:t>2</m:t>\r\n" + 
 		"      </m:r>\r\n" + 
 		"    </m:sup>\r\n" + 
 		"  </m:sSup>\r\n" + 
 		"</xml-fragment";
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertHtml(omml);
doc.save("D:/temp/20.12.docx");

Such a result is 1. docx, but I want to achieve the result is a officemath, please see 2. docx.

Thanks1225.zip (10.9 KB)

@hlgao

Please make sure that you are inserting the correct MathML equation into document. You can use following MathML to get the desired output.

<math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mrow><mover><mrow><mi>z</mi></mrow><mo>-</mo></mover></mrow><mrow><mn>2</mn></mrow></msup></math>