C# 在Word中添加Latex 數學公式和符號


本篇內容介紹使用Spire.Doc for .NET在Word中添加Latex數學公式和符號的方法。編輯代碼前,將Spire.Doc.dll文件添加引用至VS程序。dll文件包可通過官網下載導入(如果下載的是pack包,需要將Spire.Doc for .NET包解壓安裝到指定路徑,dll文件可在安裝路徑下的Bin中找到;如果下載的是hotfix包,則無需安裝,可直接在文件夾Bin下找到dll);或者通過Nuget搜索下載導入。

注意需要使用7.6.5版本及以上的Spire.Doc for .NET,本文中下載使用的是hotfix 8.4.2版本

dll添加引用效果,如下圖:

 

 

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

namespace Create
{
    class Program
    {
        static void Main(string[] args)
        {
            //新建word實例
            Document doc = new Document();

            //添加一個section
            Section section = doc.AddSection();

            //添加一個段落 
            Paragraph paragraph = section.AddParagraph();

            //在第一段添加公式
            OfficeMath officeMath = new OfficeMath(doc);
            paragraph.Items.Add(officeMath);
            officeMath.FromLatexMathCode("x^{2}+\\sqrt{x^{2}+1}=2");

            //添加第二個公式到第二段
            Paragraph paragraph2 = section.AddParagraph();
            OfficeMath officeMath1 = new OfficeMath(doc);
            paragraph2.Items.Add(officeMath1);
            officeMath1.FromLatexMathCode("\\forall x \\in X, \\quad \\exists y \\leq \\epsilon");

            //添加符號到第三段 
            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");
        }
    }
}

公式/符號添加效果:

 

(完)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM