C ා add Latex mathematical formula and symbol in Word

Keywords: C#

This article describes how to use spirit.doc for. Net to add Latex mathematical formulas and symbols in Word. Before editing the code, add a reference to the spirit.doc.dll file to the VS program. DLL packages are available through Download and import on official website (if you download the pack package, you need to unzip and install the fire.doc for. Net package to the specified path, and the dll file can be found in Bin under the installation path; if you download the hotfix package, you don't need to install it, and you can find the dll directly under Bin); or through Nuget Search for download import.

Note: sprie.doc for. Net version 7.6.5 and above is required. Hotfix version 8.4.2 is used for download in this article

dll add reference effect, as shown below:

 

 

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields.OMath;

namespace Create
{
    class Program
    {
        static void Main(string[] args)
        {
            //Newly build word Example
            Document doc = new Document();

            //Add a section
            Section section = doc.AddSection();

            //Add a paragraph 
            Paragraph paragraph = section.AddParagraph();

            //Add formula in the first paragraph
            OfficeMath officeMath = new OfficeMath(doc);
            paragraph.Items.Add(officeMath);
            officeMath.FromLatexMathCode("x^{2}+\\sqrt{x^{2}+1}=2");

            //Add second formula to second paragraph
            Paragraph paragraph2 = section.AddParagraph();
            OfficeMath officeMath1 = new OfficeMath(doc);
            paragraph2.Items.Add(officeMath1);
            officeMath1.FromLatexMathCode("\\forall x \\in X, \\quad \\exists y \\leq \\epsilon");

            //Add symbol to third paragraph 
            Paragraph paragraph3 = section.AddParagraph();
            OfficeMath officeMath2 = new OfficeMath(doc);
            paragraph3.Items.Add(officeMath2);
            officeMath2.FromLatexMathCode(" \\alpha,\\beta, \\gamma, \\Gamma, \\pi, \\Pi, \\phi, \\varphi, \\mu, \\Phi");

            //Save document       
            doc.SaveToFile("result.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("result.docx");
        }
    }
}

Formula / symbol addition effect:

 

(end)

Posted by bysable on Tue, 21 Apr 2020 07:50:15 -0700