Insert Math Equations and Symbols in Word Document in Java

This article demonstrates how to insert math equations i.e. Latex and MathML equations and Symbols in a Word document in Java using Spire.Doc for Java.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.Table;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.omath.OfficeMath;

public class AddMathEquationsAndSymbols {
    public static void main(String[] args){
        //Latex code
        String[] latexMathCode1 = {
                "x^{2}+\\sqrt{{x^{2}+1}}+1",
                "2\\alpha - \\sin y + x",
        };

        //MathML code
        String[] mathMLCode = {
                "<mml:math xmlns:mml=\"http://www.w3.org/1998/Math/MathML\" xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\"><mml:msup><mml:mrow><mml:mi>x</mml:mi></mml:mrow><mml:mrow><mml:mn>2</mml:mn></mml:mrow></mml:msup><mml:mo>+</mml:mo><mml:msqrt><mml:msup><mml:mrow><mml:mi>x</mml:mi></mml:mrow><mml:mrow><mml:mn>2</mml:mn></mml:mrow></mml:msup><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:msqrt><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:math>",
                "<mml:math xmlns:mml=\"http://www.w3.org/1998/Math/MathML\" xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\"><mml:mn>2</mml:mn><mml:mi>α</mml:mi><mml:mo>-</mml:mo><mml:mi>s</mml:mi><mml:mi>i</mml:mi><mml:mi>n</mml:mi><mml:mi>y</mml:mi><mml:mo>+</mml:mo><mml:mi>x</mml:mi></mml:math>",
        };

        //Latex code
        String[] latexMathCode2 = {
                "\\alpha",
                "\\beta",
        };

        //Create a Document instance
        Document doc = new Document();
        //Load the Word document
        doc.loadFromFile("MathEquationTemplate.docx");
        //Get the first section
        Section section = doc.getSections().get(0);

        Paragraph paragraph = null;
        OfficeMath officeMath;

        //Insert Latex equations
        Table table1 = section.getTables().get(0);
        for (int i = 0; i < latexMathCode1.length; i++) {
            paragraph = table1.getRows().get(i + 1).getCells().get(0).addParagraph();
            paragraph.setText(latexMathCode1[i]);
            paragraph = table1.getRows().get(i + 1).getCells().get(1).addParagraph();
            officeMath = new OfficeMath(doc);
            officeMath.fromLatexMathCode(latexMathCode1[i]);
            paragraph.getItems().add(officeMath);
        }

        //Insert MathML equations
        Table table2 = section.getTables().get(1);
        for (int i = 0; i < mathMLCode.length; i++) {
            paragraph = table2.getRows().get(i + 1).getCells().get(0).addParagraph();
            paragraph.setText(mathMLCode[i]);
            paragraph = table2.getRows().get(i + 1).getCells().get(1).addParagraph();
            officeMath = new OfficeMath(doc);
            officeMath.fromMathMLCode(mathMLCode[i]);
            paragraph.getItems().add(officeMath);
        }

        //Insert Symbols
        Table table3 = section.getTables().get(2);
        for (int i = 0; i < latexMathCode2.length; i++) {
            //Insert Latex code to the first column of the table
            paragraph = table3.getRows().get(i + 1).getCells().get(0).addParagraph();
            paragraph.setText(latexMathCode2[i]);
            //Insert symbols to the second column of the table
            paragraph = table3.getRows().get(i + 1).getCells().get(1).addParagraph();
            officeMath = new OfficeMath(doc);
            officeMath.fromLatexMathCode(latexMathCode2[i]);
            paragraph.getItems().add(officeMath);
        }

        //Save the document
        String result = "addMathEquationAndSymbols.docx";
        doc.saveToFile(result, FileFormat.Docx_2013);
    }
}

Output:

Insert Math Equations and Symbols in Word Document in Java