C# 在PPT中添加數學公式


本次內容介紹在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

 

 

 

 

 

 

 

2. NuGet搜索下載安裝到VS程序

 

 

 

 

【代碼示例】

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

公式添加效果如圖:

 

【API】

代碼中涉及到的類(如Presentation)、接口(如ISlide、IAutoShape)等相關解釋和使用方法可在Spire.Presentation Namespace中查看。

 

—End—

 


免責聲明!

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



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