C#实现代码在PowerPoint演示文稿中添加超链接
作者:2501_93070778
超链接是一种可点击的元素,通常嵌入在文本或图片中,本文将演示如何使用 Spire.Presentation for .NET 在 PowerPoint 中以编程方式添加超链接,有需要的小伙伴可以了解下
超链接是一种可点击的元素,通常嵌入在文本或图片中,用户可以通过它快速访问不同的网页、文档或资源。在 PowerPoint 演示文稿中添加超链接,可以让观众在查看或展示幻灯片时方便地访问相关内容,提高演示的便捷性和互动性。本文将演示如何使用 Spire.Presentation for .NET 在 PowerPoint 中以编程方式添加超链接。
安装 Spire.Presentation for .NET
首先,需要将 Spire.Presentation for .NET 包含的 DLL 文件添加到你的 .NET 项目中作为引用。这些 DLL 文件可以通过官方链接获取,也可以通过 NuGet 直接安装。
PM> Install-Package Spire.Presentation
在幻灯片文本中添加超链接
使用 Spire.Presentation for .NET,可以通过 TextRange.ClickAction.Address 属性轻松地为幻灯片中的文本添加超链接。下面是具体步骤:
- 创建一个新的 PowerPoint 演示文稿。
- 使用
Presentation.LoadFromFile()方法加载现有的 PowerPoint 文件。 - 通过
Presentation.Slides[]属性获取第一张幻灯片。 - 使用
ISlide.Shapes.AppendShape()方法在幻灯片上添加一个矩形形状。 - 删除形状中的默认段落。
- 创建一个
TextParagraph实例,用于表示一个文本段落。 - 创建一个
TextRange实例表示文本范围,并通过TextRange.ClickAction.Address属性设置超链接地址。 - 再创建一个
TextRange实例,表示段落中其余的文本。 - 使用
TextParagraph.TextRanges.Append()方法将文本范围添加到段落中。 - 使用
IAutoShape.TextFrame.Paragraphs.Append()方法将段落添加到形状中。 - 遍历段落中的所有文本范围,为它们设置字体样式。
- 使用
Presentation.SaveToFile()方法保存生成的文件。
示例代码如下:
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;
namespace Hyperlink
{
internal class Program
{
static void Main(string[] args)
{
// 创建一个 Presentation 实例
Presentation presentation = new Presentation();
// 加载 PowerPoint 文件
presentation.LoadFromFile("sample.pptx", FileFormat.Pptx2010);
// 获取演示文稿的第一张幻灯片
ISlide slide = presentation.Slides[0];
// 在幻灯片上添加一个矩形形状
RectangleF rec = new RectangleF(
presentation.SlideSize.Size.Width / 2 - 120, // X 坐标
200, // Y 坐标
500, // 宽度
150 // 高度
);
IAutoShape shape = slide.Shapes.AppendShape(ShapeType.Rectangle, rec);
shape.Fill.FillType = FillFormatType.None; // 设置形状填充为无
shape.ShapeStyle.LineColor.Color = Color.White; // 设置边框颜色为白色
// 清除形状中的默认段落
shape.TextFrame.Paragraphs.Clear();
// 创建一个 TextParagraph 实例
TextParagraph para = new TextParagraph();
// 创建一个 TextRange 实例
TextRange tr = new TextRange("Spire.Presentation for .NET");
// 为文本范围设置超链接地址
tr.ClickAction.Address = "http://www.e-iceblue.com/Introduce/presentation-for-net-introduce.html";
// 将文本范围添加到段落中
para.TextRanges.Append(tr);
// 创建另一个 TextRange 实例,用于其余文本
tr = new TextRange(
"是一个专业的 PowerPoint® 兼容 API,开发者可在任意 .NET 平台上创建、读取、修改、转换及打印 PowerPoint 文档。" +
"作为独立的 PowerPoint .NET API,Spire.Presentation for .NET 无需在电脑上安装 Microsoft PowerPoint。"
);
// 将文本范围添加到段落中
para.TextRanges.Append(tr);
// 将段落添加到形状中
shape.TextFrame.Paragraphs.Append(para);
// 遍历形状中的所有段落
foreach (TextParagraph textPara in shape.TextFrame.Paragraphs)
{
if (!string.IsNullOrEmpty(textPara.Text))
{
// 遍历每个段落中的文本范围
foreach (TextRange textRange in textPara.TextRanges)
{
// 设置字体样式
textRange.LatinFont = new TextFont("Calibri"); // 字体
textRange.FontHeight = 24; // 字号
textRange.Fill.FillType = FillFormatType.Solid; // 实心填充
textRange.Fill.SolidColor.Color = Color.Black; // 文字颜色
}
}
}
// 保存演示文稿
presentation.SaveToFile("TextHyperlink.pptx", FileFormat.Pptx2013);
presentation.Dispose();
}
}
}在幻灯片图片中添加超链接
使用 Spire.Presentation for .NET,你也可以为幻灯片中的图片添加超链接。只需创建一个 ClickHyperlink 对象,然后通过图片的 IEmbedImage.Click 属性将超链接添加到图片上,即可实现点击图片跳转到指定网页或资源。
示例代码如下:
using Spire.Presentation;
using System.Drawing;
namespace ImageHyperlink
{
class Program
{
static void Main(string[] args)
{
// 初始化 Presentation 类对象
Presentation presentation = new Presentation();
// 加载 PowerPoint 文件
presentation.LoadFromFile("TextHyperlink.pptx", FileFormat.Pptx2010);
// 获取演示文稿的第二张幻灯片
ISlide slide = presentation.Slides[1];
// 在幻灯片上添加图片
RectangleF rect = new RectangleF(100, 50, 150, 150); // 设置图片位置和大小
IEmbedImage image = slide.Shapes.AppendEmbedImage(ShapeType.Rectangle, @"logo.png", rect);
// 为图片添加超链接
ClickHyperlink hyperlink = new ClickHyperlink("http://www.e-iceblue.com/Introduce/presentation-for-net-introduce.html");
image.Click = hyperlink;
// 保存生成的演示文稿
presentation.SaveToFile("ImageHyperlink.pptx", FileFormat.Pptx2010);
presentation.Dispose();
}
}
}到此这篇关于C#实现代码在PowerPoint演示文稿中添加超链接的文章就介绍到这了,更多相关C# PowerPoint添加超链接内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
