latex 是一个强大的排版数学公式的工具,它支持多种数学符号来创建数学公式,如分数、积分、矩阵等。spire.presentation for .net 提供了使用 latex 代码创建并添加数学公式到 powerpoint 幻灯片的方法,可供开发者创建多种类型的复杂公式,本文将对此做详细介绍。
安装spire.presentation for .net
首先,您需要将 spire.presentation for.net 包含的 dll 文件作为引用添加到您的 .net 项目中。dll 文件可以从 此链接 下载,也可以通过 安装。
pm> install-package spire.presentation
添加公式到幻灯片
下面是实现公式添加的详细步骤:
- 创建 presentation 类的实例。
- 通过索引值获取指定幻灯片。
- 使用 shapelist.appendshape() 方法添加形状到幻灯片。
- 使用 paragraphcollection.addparagraphfromlatexmathcode(string) 方法从 latex 代码创建一个数学公式,并将其添加到形状。
- 使用 presentation.savetofile(string, fileformat) 方法保存结果文档,并指定保存路径。
- c#
- vb.net
using spire.presentation;
using spire.presentation.drawing;
using system.drawing;
namespace addformula
{
class program
{
static void main(string[] args)
{
//定义latex公式代码
string latexcode1 = @"x^{2} \sqrt{x^{2} 1}=2";
string latexcode2 = @"f(x) &= \int^a_b \frac{1}{3}x^3";
string latexcode3 = @"\alpha \beta \geq \gamma";
string latexcode4 = @"\overrightarrow{abc}";
string latexcode5 = @"\begin{bmatrix} 1 & 0 & \cdots & 0\\ 1 & 0 & \cdots & 0\\ \vdots & \vdots & \ddots & \vdots\\ 1 & 0 & 0 & 0 \end{bmatrix}";
string latexcode6 = @"\log_a{b}";
//创建presentation类的实例
presentation ppt = new presentation();
//获取第一张幻灯片
islide slide = ppt.slides[0];
//添加形状到幻灯片
iautoshape shape = slide.shapes.appendshape(shapetype.rectangle, new rectanglef(30, 100, 200, 30));
shape.textframe.paragraphs.clear();
//使用latex代码添加数学公式到形状
shape.textframe.paragraphs.addparagraphfromlatexmathcode(latexcode1);
//重复以上操作,添加形状,并添加公式到形状
shape = slide.shapes.appendshape(shapetype.rectangle, new rectanglef(240, 100, 200, 40));
shape.textframe.paragraphs.clear();
shape.textframe.paragraphs.addparagraphfromlatexmathcode(latexcode2);
shape = slide.shapes.appendshape(shapetype.rectangle, new rectanglef(30, 180, 200, 40));
shape.textframe.paragraphs.clear();
shape.textframe.paragraphs.addparagraphfromlatexmathcode(latexcode3);
shape = slide.shapes.appendshape(shapetype.rectangle, new rectanglef(240, 180, 200, 40));
shape.textframe.paragraphs.clear();
shape.textframe.paragraphs.addparagraphfromlatexmathcode(latexcode4);
shape = slide.shapes.appendshape(shapetype.rectangle, new rectanglef(30, 280, 200, 70));
shape.textframe.paragraphs.clear();
shape.textframe.paragraphs.addparagraphfromlatexmathcode(latexcode5);
shape = slide.shapes.appendshape(shapetype.rectangle, new rectanglef(240, 280, 200, 40));
shape.textframe.paragraphs.clear();
shape.textframe.paragraphs.addparagraphfromlatexmathcode(latexcode6);
//设置形状边框和填充类型
for (int i = 0; i < slide.shapes.count; i )
{
slide.shapes[i].fill.filltype = fillformattype.none;
slide.shapes[i].line.filltype = fillformattype.none;
}
//保存文档
ppt.savetofile("mathequations.pptx", fileformat.pptx2013);
}
}
}
imports spire.presentation
imports spire.presentation.drawing
imports system.drawing
namespace addformula
class program
private shared sub main(args as string())
'定义latex公式代码
dim latexcode1 as string = "x^{2} \sqrt{x^{2} 1}=2"
dim latexcode2 as string = "f(x) &= \int^a_b \frac{1}{3}x^3"
dim latexcode3 as string = "\alpha \beta \geq \gamma"
dim latexcode4 as string = "\overrightarrow{abc}"
dim latexcode5 as string = "\begin{bmatrix} 1 & 0 & \cdots & 0\\ 1 & 0 & \cdots & 0\\ \vdots & \vdots & \ddots & \vdots\\ 1 & 0 & 0 & 0 \end{bmatrix}"
dim latexcode6 as string = "\log_a{b}"
'创建presentation类的实例
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, 200, 30))
shape.textframe.paragraphs.clear()
'使用latex代码添加数学公式到形状
shape.textframe.paragraphs.addparagraphfromlatexmathcode(latexcode1)
'重复以上操作,添加形状,并添加公式到形状
shape = slide.shapes.appendshape(shapetype.rectangle, new rectanglef(240, 100, 200, 40))
shape.textframe.paragraphs.clear()
shape.textframe.paragraphs.addparagraphfromlatexmathcode(latexcode2)
shape = slide.shapes.appendshape(shapetype.rectangle, new rectanglef(30, 180, 200, 40))
shape.textframe.paragraphs.clear()
shape.textframe.paragraphs.addparagraphfromlatexmathcode(latexcode3)
shape = slide.shapes.appendshape(shapetype.rectangle, new rectanglef(240, 180, 200, 40))
shape.textframe.paragraphs.clear()
shape.textframe.paragraphs.addparagraphfromlatexmathcode(latexcode4)
shape = slide.shapes.appendshape(shapetype.rectangle, new rectanglef(30, 280, 200, 70))
shape.textframe.paragraphs.clear()
shape.textframe.paragraphs.addparagraphfromlatexmathcode(latexcode5)
shape = slide.shapes.appendshape(shapetype.rectangle, new rectanglef(240, 280, 200, 40))
shape.textframe.paragraphs.clear()
shape.textframe.paragraphs.addparagraphfromlatexmathcode(latexcode6)
'设置形状边框和填充类型
for i as integer = 0 to slide.shapes.count - 1
slide.shapes(i).fill.filltype = fillformattype.none
slide.shapes(i).line.filltype = fillformattype.none
next
'保存文档
ppt.savetofile("mathequations.pptx", fileformat.pptx2013)
end sub
end class
end namespace
公式添加效果:
申请临时 license
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请 该email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用javascript。 获取有效期 30 天的临时许可证。