C # Add Latex math formulas and symbols in Word

This article introduces how to add Latex mathematical formulas and symbols in Word using Spire.Doc for .NET. Before editing the code, add the Spire.Doc.dll file to the VS program. The dll file package can be downloaded and imported through the official website (if the download is a pack package, the Spire.Doc for .NET package needs to be decompressed and installed to the specified path, the dll file can be found in Bin under the installation path; if the downloaded is the hotfix package No need to install, you can directly find the dll under the folder Bin); or download and import through Nuget search.

Note : Need to use Spire.Doc for .NET version 7.6.5 and above , the hotfix 8.4.2 version 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) 
        { 
            // New word instance 
            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 = newOfficeMath (doc); 
            paragraph.Items.Add (officeMath); 
            officeMath.FromLatexMathCode ( " x ^ {2} + \\ sqrt {x ^ {2} +1} = 2 " ); 

            // Add the second formula to second stage 
            the Paragraph PARAGRAPH2 = section.AddParagraph (); 
            OfficeMath officeMath1 = new new OfficeMath (DOC); 
            paragraph2.Items.Add (officeMath1); 
            officeMath1.FromLatexMathCode ( " \\ \\ FORALL in X-X, Quad \\ \\ EXISTS y \\ leq \\ epsilon " ); 

            // Add symbol to the 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");

            //保存文档       
            doc.SaveToFile("result.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("result.docx");
        }
    }
}

Formula / symbol addition effect:

 

(Finish)

 

Guess you like

Origin www.cnblogs.com/Yesi/p/12744094.html