本文将介绍如何使用spire.presentation for .net在powerpoint文档中使用正则表达式替换文本。
c#
using spire.presentation;
using system.text.regularexpressions;
namespace replacetextwithregex
{
class program
{
static void main(string[] args)
{
//创建presentation实例
presentation ppt = new presentation();
//加载示例文档
ppt.loadfromfile("sample.pptx");
//获取第一张幻灯片
islide slide = ppt.slides[0];
//替换该幻灯片中所有“abc”以及其后到行尾的内容为“abc def”
regex regex = new regex("abc.*");
string newvalue = "abc def";
foreach (ishape shape in slide.shapes)
{
shape.replacetextwithregex(regex, newvalue);
}
//保存结果文档
ppt.savetofile("replacetextwithregex.pptx", fileformat.pptx2013);
}
}
}
vb.net
imports spire.presentation
imports system.text.regularexpressions
namespace replacetextwithregex
friend class program
private shared sub main(byval args as string())
'创建presentation实例
dim ppt as presentation = new presentation()
'加载示例文档
ppt.loadfromfile("sample.pptx")
'获取第一张幻灯片
dim slide as islide = ppt.slides(0)
'替换该幻灯片中所有“abc”以及其后到行尾的内容为“abc def”
dim regex as regex = new regex("abc.*")
dim newvalue as string = "abc def"
for each shape as ishape in slide.shapes
shape.replacetextwithregex(regex, newvalue)
next
'保存结果文档
ppt.savetofile("replacetextwithregex.pptx", fileformat.pptx2013)
end sub
end class
end namespace
原powerpoint文档:
结果文档: