使用C#代码在 PowerPoint 演示文稿中添加超链接

超链接是一种可点击的元素,通常嵌入在文本或图片中,用户可以通过它快速访问不同的网页、文档或资源。在 PowerPoint 演示文稿中添加超链接,可以让观众在查看或展示幻灯片时方便地访问相关内容,提高演示的便捷性和互动性。本文将演示如何使用 Spire.Presentation for .NET 在 PowerPoint 中以编程方式添加超链接。

安装 Spire.Presentation for .NET

首先,需要将 Spire.Presentation for .NET 包含的 DLL 文件添加到你的 .NET 项目中作为引用。这些 DLL 文件可以通过官方链接获取,也可以通过 NuGet 直接安装。

cs 复制代码
PM> Install-Package Spire.Presentation

在幻灯片文本中添加超链接

使用 Spire.Presentation for .NET ,可以通过 TextRange.ClickAction.Address 属性轻松地为幻灯片中的文本添加超链接。下面是具体步骤:

  1. 创建一个新的 PowerPoint 演示文稿。

  2. 使用 Presentation.LoadFromFile() 方法加载现有的 PowerPoint 文件。

  3. 通过 Presentation.Slides[] 属性获取第一张幻灯片。

  4. 使用 ISlide.Shapes.AppendShape() 方法在幻灯片上添加一个矩形形状。

  5. 删除形状中的默认段落。

  6. 创建一个 TextParagraph 实例,用于表示一个文本段落。

  7. 创建一个 TextRange 实例表示文本范围,并通过 TextRange.ClickAction.Address 属性设置超链接地址。

  8. 再创建一个 TextRange 实例,表示段落中其余的文本。

  9. 使用 TextParagraph.TextRanges.Append() 方法将文本范围添加到段落中。

  10. 使用 IAutoShape.TextFrame.Paragraphs.Append() 方法将段落添加到形状中。

  11. 遍历段落中的所有文本范围,为它们设置字体样式。

  12. 使用 Presentation.SaveToFile() 方法保存生成的文件。

示例代码如下:

cs 复制代码
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 属性将超链接添加到图片上,即可实现点击图片跳转到指定网页或资源。

示例代码如下:

cs 复制代码
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();
        }
    }
}

申请临时许可证

如果您希望移除生成文档中的评估提示,或解除功能限制,请为自己申请 30 天试用许可证。

相关推荐
Fariy*1 天前
述职PPT应该怎么写!?
powerpoint
xiami_world1 天前
AI Agent生成PPT技术解析:从一键生成到意图理解,Agent模式如何重构PPT工作流?
人工智能·经验分享·ai·信息可视化·powerpoint
小小工匠2 天前
用 Nano Banana Pro 一小时做完高质量 PPT:从提示词到完整工作流实战
powerpoint
王哥儿聊AI4 天前
微软开源神器MarkItDown:一键把PPT/PDF/Excel转成markdown,LLM直呼内行!
人工智能·深度学习·microsoft·机器学习·开源·powerpoint
新缸中之脑7 天前
12个最佳AI演示文稿(PPT)制作工具
人工智能·powerpoint
reasonsummer9 天前
【办公类-133-02】20260319_学区化展示PPT_02_python(图片合并文件夹、提取同名图片归类文件夹、图片编号、图片GIF)
前端·数据库·powerpoint
reasonsummer11 天前
【办公类-133-01】20260319_学区化展示PPT_01_“豆包大纲文字”+“天工AI”制作基础模版
powerpoint
科技圈快讯11 天前
文多多AIPPT:部分开源+私有化部署重构PPT创作体验
重构·开源·powerpoint
zzh9407711 天前
2026年AI文件上传功能实战:聚合站处理图片、PDF、PPT全指南
人工智能·pdf·powerpoint
百事牛科技12 天前
更新你的保护:如何修改PPT“打开密码”
windows·powerpoint