C#在Word中添加Latex数学公式和符号的方法

本篇内容介绍了“C#在Word中添加Latex数学公式和符号的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

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

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

C#在Word中添加Latex数学公式和符号的方法

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 paragraph3 = section.AddParagraph();
            OfficeMath officeMath2 = new OfficeMath(doc);
            paragraph3.Items.Add(officeMath2);
            officeMath2.FromLatexMathCode("\\forall x \\in X, \\quad \\exists y \\leq \\epsilon");
 
            //添加符号到第三段 
            Paragraph paragraph4 = section.AddParagraph();
            OfficeMath officeMath3 = new OfficeMath(doc);
            paragraph4.Items.Add(officeMath3);
            officeMath3.FromLatexMathCode(" \\alpha,\\beta, \\gamma, \\Gamma, \\pi, \\Pi, \\phi, \\varphi, \\mu, \\Phi");
 
            //保存文档       
            doc.SaveToFile("result.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("result.docx");
        }
    }
}

公式/符号添加效果:

C#在Word中添加Latex数学公式和符号的方法

“C#在Word中添加Latex数学公式和符号的方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!