本教程展示了如何在创建幻灯片的时候添加超链接,以及如何修改和删除现有文档中超链接。
添加超链接
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