spire.presentation 拥有强大的转换功能,前面我们介绍了, 该文将详细介绍如果使用c#将html 字符串转换为powerpoint 文档。
c#
//新建一个powerpoint文档并获取第一个幻灯片
presentation ppt = new presentation();
islide slide = ppt.slides[0];
//添加一个shape到第一张幻灯片并设置其大小
iautoshape shape = slide.shapes.appendshape(shapetype.rectangle, new rectanglef(150, 100, 200, 200));
//使用addfromhtml方法为段落添加文本并设置其样式,颜色
string code = "输入一段html字符串
";
shape.textframe.paragraphs.addfromhtml(code);
string codecolor = "输入一段html字符串
";
shape.textframe.paragraphs.addfromhtml(codecolor);
//添加第二个shape
iautoshape shape1 = ppt.slides[0].shapes.appendshape(shapetype.rectangle, new rectanglef(350, 100, 200, 200));
//清除默认段落
shape1.textframe.paragraphs.clear();
//设置shape颜色和填充方式
shape1.fill.filltype = fillformattype.solid;
shape1.fill.solidcolor.color = color.white;
//使用addfromhtml方法为段落添加文本并设置其样式,颜色
shape1.textframe.paragraphs.addfromhtml(code);
textparagraph par = shape1.textframe.paragraphs[0];
foreach (textrange tr in par.textranges)
{
tr.fill.filltype = fillformattype.solid;
tr.fill.solidcolor.color = color.blue;
}
//将文档保存为powerpoint格式
ppt.savetofile("htmlstringtoppt.pptx", fileformat.pptx2010);
vb.net
'新建一个powerpoint文档并获取第一个幻灯片
dim ppt as new presentation()
dim slide as islide = ppt.slides(0)
'添加一个shape到第一张幻灯片并设置其大小
dim shape as iautoshape = slide.shapes.appendshape(shapetype.rectangle, new rectanglef(150, 100, 200, 200))
'使用addfromhtml方法为段落添加文本并设置其样式,颜色
dim code as string = "输入一段html字符串
"
shape.textframe.paragraphs.addfromhtml(code)
dim codecolor as string = "输入一段html字符串
"
shape.textframe.paragraphs.addfromhtml(codecolor)
'添加第二个shape
dim shape1 as iautoshape = ppt.slides(0).shapes.appendshape(shapetype.rectangle, new rectanglef(350, 100, 200, 200))
'清除默认段落
shape1.textframe.paragraphs.clear()
'设置shape颜色和填充方式
shape1.fill.filltype = fillformattype.solid
shape1.fill.solidcolor.color = color.white
'使用addfromhtml方法为段落添加文本并设置其样式,颜色
shape1.textframe.paragraphs.addfromhtml(code)
dim par as textparagraph = shape1.textframe.paragraphs(0)
for each tr as textrange in par.textranges
tr.fill.filltype = fillformattype.solid
tr.fill.solidcolor.color = color.blue
next
'将文档保存为powerpoint格式
ppt.savetofile("htmlstringtoppt.pptx", fileformat.pptx2010)