C# 在PPT中添加数学公式

如果你不相信努力和时光,那么成果就会是第一个选择辜负你的。不要去否定你自己的过去,也不要用你的过去牵扯你现在的努力和对未来的展望。不是因为拥有希望你才去努力,而是去努力了,你才有可能看到希望的光芒。C# 在PPT中添加数学公式,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

本次内容介绍在C#程序中给PPT幻灯片添加Latex数学公式,添加公式前,首先需要在幻灯片中插入一个Shape形状,在形状的段落中通过方法Paragraphs.AddParagraphFromLatexMathCode( string latexMathCode)写入公式,最后保存。

【dll引用】

本次使用PPT库Spire.Presentation for .NET Version 6.9.2,在VS程序中添加引用Spire.Presentation.dll。2种引用方法:

1.下载包到本地,解压,将Bin文件夹下的dll引用至VS

C# 在PPT中添加数学公式

 

 

 C# 在PPT中添加数学公式

 C# 在PPT中添加数学公式

 C# 在PPT中添加数学公式

 C# 在PPT中添加数学公式

 

2. NuGet搜索下载安装到VS程序

C# 在PPT中添加数学公式

 

 

 C# 在PPT中添加数学公式

 

【代码示例】

C#

using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;

namespace AddFormular
{
    class Program
    {
        static void Main(string[] args)
        {
            //新建一个PPT幻灯片文档,并获取第一张幻灯片(新建的幻灯片已默认包含一张幻灯片)
            Presentation ppt = new Presentation();
            ISlide slide = ppt.Slides[0];

            //添加形状到幻灯片
            IAutoShape shape = slide.Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(30, 100, 400, 30));
            shape.Fill.FillType = FillFormatType.None;
            shape.ShapeStyle.LineColor.Color = Color.White;
            shape.TextFrame.Paragraphs.Clear();

            //添加公式
            string latexMathCode = @"$ f(x,y) = \sqrt[n]{{x^2}{y^3}} $";
            shape.TextFrame.Paragraphs.AddParagraphFromLatexMathCode(latexMathCode);           

            //保存
            ppt.SaveToFile("AddLatexMathCode.pptx", FileFormat.Pptx2013);
            System.Diagnostics.Process.Start("AddLatexMathCode.pptx");
        }
    }
}

VB.NET

Imports Spire.Presentation
Imports Spire.Presentation.Drawing
Imports System.Drawing

Namespace AddFormular
    Class Program
        Private Shared Sub Main(args As String())
            '新建一个PPT幻灯片文档,并获取第一张幻灯片(新建的幻灯片已默认包含一张幻灯片)
            Dim ppt As New Presentation()
            Dim slide As ISlide = ppt.Slides(0)

            '添加形状到幻灯片
            Dim shape As IAutoShape = slide.Shapes.AppendShape(ShapeType.Rectangle, New RectangleF(30, 100, 400, 30))
            shape.Fill.FillType = FillFormatType.None
            shape.ShapeStyle.LineColor.Color = Color.White
            shape.TextFrame.Paragraphs.Clear()

            '添加公式
            Dim latexMathCode As String = "$ f(x,y) = \sqrt[n]{{x^2}{y^3}} $"
            shape.TextFrame.Paragraphs.AddParagraphFromLatexMathCode(latexMathCode)

            '保存
            ppt.SaveToFile("AddLatexMathCode.pptx", FileFormat.Pptx2013)
            System.Diagnostics.Process.Start("AddLatexMathCode.pptx")
        End Sub
    End Class
End Namespace

公式添加效果如图:

C# 在PPT中添加数学公式

 

【API】

代码中涉及到的类(如Presentation)、接口(如ISlide、IAutoShape)等相关解释和使用方法可在Spire.Presentation Namespace中查看。

 

—End—

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/209230.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!