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