本教程展示了如何在创建幻灯片的时候添加超链接,以及如何修改和删除现有文档中超链接。
添加超链接
c#
//初始化presentation实例
presentation presentation = new presentation();
//创建一个图形
iautoshape shape = presentation.slides[0].shapes.appendshape(shapetype.rectangle, new rectanglef(50, 70, 600, 100));
//设置填充背景色和边框颜色
shape.fill.filltype = fillformattype.solid;
shape.fill.solidcolor.color = color.lightgray;
shape.shapestyle.linecolor.color = color.white;
//声明两个字符串变量
string s1 = "spire.presentation for .net";
string s2 = " 是一款专业的 powerpoint 组件。使用该组件,开发者可以在 .net 平台上对 powerpoint 文档进行生成、读取、写入、修改、"
"转换和打印等操作。作为一个独立的控件,spire.presentation for .net 的运行无需要安装 microsoft powerpoint。";
//获取图形上段落(默认有一个空白段落)
textparagraph paragraph = shape.textframe.textrange.paragraph;
paragraph.alignment = textalignmenttype.left;
//根据字符串1创建textrange 1并文字上添加链接
textrange tr1 = new textrange(s1);
tr1.clickaction.address = "https://www.e-iceblue.com/introduce/presentation-for-net-introduce.html";
//根据字符串2创建textrange 2
textrange tr2 = new textrange(s2);
//将两个textrange添加到段落
paragraph.textranges.append(tr1);
paragraph.textranges.append(tr2);
//设置该段落的字体样式
foreach (textrange tr in paragraph.textranges)
{
tr.latinfont = new textfont("宋体 (body)");
tr.fontheight = 14f;
tr.isbold = tristate.true;
tr.fill.filltype = fillformattype.solid;
tr.fill.solidcolor.color = color.black;
}
//保存文档
presentation.savetofile("添加超链接.pptx", fileformat.pptx2010);
vb.net
'初始化presentation实例
dim presentation as new presentation()
'创建一个图形
dim shape as iautoshape = presentation.slides(0).shapes.appendshape(shapetype.rectangle, new rectanglef(50, 70, 600, 100))
'设置填充背景色和边框颜色
shape.fill.filltype = fillformattype.solid
shape.fill.solidcolor.color = color.lightgray
shape.shapestyle.linecolor.color = color.white
'声明两个字符串变量
dim s1 as string = "spire.presentation for .net"
dim s2 as string = " 是一款专业的 powerpoint 组件。使用该组件,开发者可以在 .net 平台上对 powerpoint 文档进行生成、读取、写入、修改、" "转换和打印等操作。作为一个独立的控件,spire.presentation for .net 的运行无需要安装 microsoft powerpoint。"
'获取图形上段落(默认有一个空白段落)
dim paragraph as textparagraph = shape.textframe.textrange.paragraph
paragraph.alignment = textalignmenttype.left
'根据字符串1创建textrange 1并文字上添加链接
dim tr1 as new textrange(s1)
tr1.clickaction.address = "https://www.e-iceblue.com/introduce/presentation-for-net-introduce.html"
'根据字符串2创建textrange 2
dim tr2 as new textrange(s2)
'将两个textrange添加到段落
paragraph.textranges.append(tr1)
paragraph.textranges.append(tr2)
'设置该段落的字体样式
for each tr as textrange in paragraph.textranges
tr.latinfont = new textfont("宋体 (body)")
tr.fontheight = 14f
tr.isbold = tristate.[true]
tr.fill.filltype = fillformattype.solid
tr.fill.solidcolor.color = color.black
next
'保存文档
presentation.savetofile("添加超链接.pptx", fileformat.pptx2010)
修改超链接
c#
//初始化presentation实例
presentation presentation = new presentation();
//加载现有的文档
presentation.loadfromfile("添加超链接.pptx");
//获取第一张幻灯片
islide slide = presentation.slides[0];
//遍历shape
foreach (ishape shape in slide.shapes)
{
//判断是否为autoshape
if (shape is iautoshape)
{
//将shape转换为autoshape
iautoshape autoshape = shape as iautoshape;
//遍历autoshape中的paragraph
foreach (textparagraph tp in autoshape.textframe.paragraphs)
{
//判断paragraph下是否含有textrange
if (tp.textranges != null && tp.textranges.count > 0)
{
//遍历textrange
for (int tpcount = 0; tpcount < tp.textranges.count; tpcount )
{
//判断是否含有文本且含有clickaction和链接
if (tp.textranges[tpcount].clickaction != null && !string.isnullorwhitespace(tp.textranges[tpcount].clickaction.address) && !string.isnullorwhitespace(tp.textranges[tpcount].text))
{
//判断是否含有http链接或https链接
if (tp.textranges[tpcount].clickaction.address.tolower().contains("http") || tp.textranges[tpcount].clickaction.address.tolower().contains("https"))
{
//为链接重新赋值
tp.textranges[tpcount].clickaction.address = "http://www.e-iceblue.com";
//重新设置超链接文本
tp.textranges[tpcount].text = "e-iceblue";
}
}
}
}
}
}
}
//保存文档
presentation.savetofile("result.pptx", fileformat.pptx2010);
vb.net
'初始化presentation实例
dim presentation as new presentation()
'加载现有的文档
presentation.loadfromfile("添加超链接.pptx")
'获取第一张幻灯片
dim slide as islide = presentation.slides(0)
'遍历shape
for each shape as ishape in slide.shapes
'判断是否为autoshape
if typeof shape is iautoshape then
'将shape转换为autoshape
dim autoshape as iautoshape = trycast(shape, iautoshape)
'遍历autoshape中的paragraph
for each tp as textparagraph in autoshape.textframe.paragraphs
'判断paragraph下是否含有textrange
if tp.textranges isnot nothing andalso tp.textranges.count > 0 then
'遍历textrange
for tpcount as integer = 0 to tp.textranges.count - 1
'判断是否含有文本且含有clickaction和链接
if tp.textranges(tpcount).clickaction isnot nothing andalso not string.isnullorwhitespace(tp.textranges(tpcount).clickaction.address) andalso not string.isnullorwhitespace(tp.textranges(tpcount).text) then
'判断是否含有http链接或https链接
if tp.textranges(tpcount).clickaction.address.tolower().contains("http") orelse tp.textranges(tpcount).clickaction.address.tolower().contains("https") then
'为链接重新赋值
tp.textranges(tpcount).clickaction.address = "http://www.e-iceblue.com"
'重新设置超链接文本
tp.textranges(tpcount).text = "e-iceblue"
end if
end if
next
end if
next
end if
next
'保存文档
presentation.savetofile("result.pptx", fileformat.pptx2010)
删除超链接
在第二部分中,我们已经获取到图形中的链接,只需要将clickaction设置为空即可删除超链接。
c#
tp.textranges[tpcount].clickaction = null;
vb.net
tp.textranges(tpcount).clickaction = nothing