文档中的超链接用于从一个位置导航到另一个位置。通常,在 PowerPoint 演示文稿中,它们用于插入网页的 URL。您可以在 PowerPoint PPT 幻灯片中创建文本、图像、形状或媒体元素的超链接。在本文中,您将学习如何在 C# 中向 PowerPoint PPT/PPTX 添加超链接。此外,我们将介绍如何以编程方式从 PPT 幻灯片中删除超链接。
C# .NET API 在 PowerPoint PPT 中添加或删除超链接
要使用 PowerPoint 演示文稿中的超链接,我们将使用 Aspose.Slides for .NET。它是一种流行的 API,用于创建和修改 PPT/PPTX 演示文稿。此外,它还允许您将演示文稿转换为其他流行格式。您可以 下载 其 DLL 或从 NuGet 安装它。
PM> Install-Package Aspose.Slides.NET
在 C# 中的 PowerPoint PPT/PPTX 中添加超链接
您可以使用 Aspose.Slides for .NET 在 PowerPoint 演示文稿中添加不同类型的超链接。超链接可以是文本、图像、形状、音频或视频元素。在以下部分中,我们将介绍如何在 PPT/PPTX 演示文稿中添加所有这些超链接。
在 PowerPoint PPT 中添加文本超链接
以下是在 C# 中的 PowerPoint PPT/PPTX 中添加文本超链接的步骤。
- 首先,加载演示文件或使用 Presentation 类创建一个新文件。
- 然后,使用 ISlide.Shapes.AddAutoShape(ShapeType, single, single, single, single) 方法将矩形自动形状添加到形状集合中。
- 使用 IAutoShape.AddTextFrame(string) 方法向形状添加文本。
- 使用 IAutoShape.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick 属性创建超链接。
- 设置超链接的其他属性,例如工具提示。
- 最后,使用 Presentation.Save(string, SaveFormat) 方法保存演示文稿。
以下代码示例演示如何使用 C# 在 PowerPoint PPTX 中添加文本超链接。
// 加载演示文稿
using (Presentation presentation = new Presentation("presentation.pptx"))
{
// 为文本创建自动形状
IAutoShape shape1 = presentation.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 600, 50, false);
// 设置文本
shape1.AddTextFrame("Aspose: File Format APIs");
// 设置超链接
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick.Tooltip = "More than 70% Fortune 100 companies trust Aspose APIs";
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FontHeight = 32;
// 保存演示文稿
presentation.Save("text-hyperlink.pptx", SaveFormat.Pptx);
}
在 PowerPoint 中插入形状超链接
以下步骤演示如何在 C# 中的 PowerPoint 演示文稿中插入形状超链接。
- 首先,加载演示文件或使用 Presentation 类创建一个新文件。
- 然后,使用 ISlide.Shapes.AddAutoShape(ShapeType, single, single, single, single) 方法添加所需类型的自动形状。
- 使用 IAutoShape.HyperlinkClick 属性创建超链接。
- 最后,使用 Presentation.Save(string, SaveFormat) 方法保存演示文稿。
以下代码示例演示如何使用 C# 在 PowerPoint 中创建形状超链接。
// 加载演示文稿
using (Presentation presentation = new Presentation("presentation.pptx"))
{
// 创建自动形状
IShape shape = presentation.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 600, 50);
// 创建超链接
shape.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
shape.HyperlinkClick.Tooltip = "More than 70% Fortune 100 companies trust Aspose APIs";
// 保存演示文稿
presentation.Save("shape-hyperlink.pptx", SaveFormat.Pptx);
}
在 PowerPoint 中插入图像超链接
以下是使用 C# 在 PowerPoint PPT 中插入图像超链接的步骤。
- 首先,使用 Presentation 类加载演示文件或创建一个新文件。
- 然后,使用 Presentation.Images.AddImage() 方法将图像添加到集合中。
- 使用 Presentation.Slides[index].Shapes.AddPictureFrame() 方法将相框添加到所需的幻灯片。
- 通过设置 IPictureFrame.HyperlinkClick 属性创建超链接。
- 最后,使用 Presentation.Save(string, SaveFormat) 方法保存演示文稿。
以下代码示例演示如何使用 C# 在 PowerPoint 中创建图像的超链接。
// 加载演示文稿
using (Presentation presentation = new Presentation("presentation.pptx"))
{
// 将图像添加到演示文稿
IPPImage image = presentation.Images.AddImage(File.ReadAllBytes("image.png"));
// 根据先前添加的图像在幻灯片 1 上创建相框
IPictureFrame pictureFrame = presentation.Slides[0].Shapes.AddPictureFrame(ShapeType.Rectangle, 10, 10, 100, 100, image);
// 创建超链接
pictureFrame.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
pictureFrame.HyperlinkClick.Tooltip = "More than 70% Fortune 100 companies trust Aspose APIs";
// 保存演示文稿
presentation.Save("image-hyperlink.pptx", SaveFormat.Pptx);
}
在 PPT 中添加音频超链接
您可以按照以下步骤将音频超链接添加到 PowerPoint PPT/PPTX:
- 首先,使用 Presentation 类加载演示文件或创建一个新文件。
- 然后,使用 Presentation.Audios.AddAudio() 方法将音频添加到集合中。
- 之后,使用 Presentation.Slides[index].Shapes.AddAudioFrameEmbedded() 方法将音频帧添加到所需的幻灯片。
- 通过设置 IAudioFrame.HyperlinkClick 属性创建超链接。
- 最后,使用 Presentation.Save(string, SaveFormat) 方法保存演示文稿。
以下代码片段演示了如何在 C# 中的 PowerPoint 中添加音频超链接:
// 加载演示文稿
using (Presentation presentation = new Presentation("presentation.pptx"))
{
// 插入音频
IAudio audio = presentation.Audios.AddAudio(File.ReadAllBytes("audio.mp3"));
IAudioFrame audioFrame = presentation.Slides[0].Shapes.AddAudioFrameEmbedded(10, 10, 100, 100, audio);
// 创建超链接
audioFrame.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
audioFrame.HyperlinkClick.Tooltip = "More than 70% Fortune 100 companies trust Aspose APIs";
// 保存演示文稿
presentation.Save("audio-hyperlink.pptx", SaveFormat.Pptx);
}
在 PowerPoint PPT 中添加视频超链接
要将视频超链接添加到 PowerPoint PPT/PPTX,请按照以下步骤操作:
- 首先,加载演示文件或使用 Presentation 类创建一个新文件。
- 然后,使用 Presentation.Videos.AddVideo() 方法将视频添加到集合中。
- 使用 Presentation.Slides[index].Shapes.AddVideoFrame() 方法将视频帧添加到所需的幻灯片。
- 通过设置 IVideoFrame.HyperlinkClick 属性创建超链接。
- 最后,使用 Presentation.Save(string, SaveFormat) 方法保存演示文稿。
以下代码片段演示了如何在 C# 中的 PowerPoint 中添加视频超链接:
// 加载演示文稿
using (Presentation presentation = new Presentation("presentation.pptx"))
{
// 插入视频
IVideo video = presentation.Videos.AddVideo(File.ReadAllBytes("wildlife.mp4"));
IVideoFrame videoFrame = presentation.Slides[0].Shapes.AddVideoFrame(10, 10, 100, 100, video);
// 创建超链接
videoFrame.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
videoFrame.HyperlinkClick.Tooltip = "More than 70% Fortune 100 companies trust Aspose APIs";
// 保存演示文稿
presentation.Save("video-hyperlink.pptx", SaveFormat.Pptx);
}
在 C# 中从 PowerPoint PPT 中删除超链接
在本节中,我们将演示如何从 PowerPoint 幻灯片中删除超链接。这些超链接可以是我们上面讨论过的任何类型。以下是执行此操作的步骤。
- 首先,使用 Presentation 类加载演示文件。
- 然后,使用 ISlide.Shapes 集合循环遍历幻灯片中的所有形状。
- 之后,使用 IShape.HyperlinkManager.RemoveHyperlinkClick() 方法从形状中删除超链接。
- 要删除文本超链接,请将 shape 转换为 IAutoShape。
- 循环遍历形状的段落。
- 循环遍历每个段落的文本部分。
- 使用 IPortion.PortionFormat.HyperlinkManager.RemoveHyperlinkClick() 方法删除文本超链接。
- 最后,使用 Presentation.Save(string, SaveFormat) 方法保存演示文稿。
以下代码示例演示如何在 C# 中从 PowerPoint PPT 中的幻灯片中删除超链接。
// 加载演示文稿
using (Presentation presentation = new Presentation("text-hyperlink.pptx"))
{
// 获取幻灯片参考
ISlide slide = presentation.Slides[0];
// 循环通过形状
foreach (IShape shape in slide.Shapes)
{
// 如果将超链接应用于形状,请删除它
shape.HyperlinkManager.RemoveHyperlinkClick();
// 继续从文本中删除超链接
IAutoShape autoShape = shape as IAutoShape;
if (autoShape != null)
{
foreach (IParagraph paragraph in autoShape.TextFrame.Paragraphs)
{
foreach (IPortion portion in paragraph.Portions)
{
portion.PortionFormat.HyperlinkManager.RemoveHyperlinkClick();
}
}
}
}
// 保存演示文稿
presentation.Save("remove-hyperlinks.pptx", SaveFormat.Pptx);
}
获得免费许可证
通过获得免费临时许可证,使用 Aspose.Slides for .NET 不受评估限制。
结论
在本文中,您学习了如何在 C# 中添加或删除 PowerPoint PPT 中的超链接。此外,我们还介绍了如何制作文本、图像、形状、音频和视频元素的超链接。此外,您可以通过访问 文档 了解更多关于 Aspose.Slides for .NET 的信息。此外,您可以将您的查询发布到我们的 论坛。