使用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 天试用许可证。

相关推荐
吾爱神器1 天前
PPT关键词云图标制作工具,丰富PPT素材库
办公软件·powerpoint·ppt制作·ppt词云·ppt工具
2501_930707782 天前
使用C#代码在 PowerPoint 文本框中添加或删除列
powerpoint
用PPT构建世界2 天前
PPT插入视频的播放控制:自动播放与点击播放设置!
职场和发展·powerpoint·ppt·ppt模板·职场分享
愚公搬代码2 天前
【愚公系列】《OpenClaw实战指南》012-分析与展示:一句话生成可发给老板的报表与 PPT(Excel/WPS 表格自动化处理)
人工智能·自动化·powerpoint·excel·飞书·wps·openclaw
asdzx672 天前
Python: 从 PPT 提取图片和文本
开发语言·python·powerpoint
绎奇PPT3 天前
国家科技重大专项PPT冲刺!最后5点核心注意事项
信息可视化·powerpoint·ppt
拂晓 AI 编程3 天前
claude code 加上 PPT Master skill 生成可手改PPT
人工智能·powerpoint
教育知暖意3 天前
从0到1:AI时代,解锁PPT高效生成新姿势
人工智能·powerpoint
oscar9993 天前
Claude Code + NanoBanana PPT Skills:一句自然语言,生成动态PPT
powerpoint·ai ppt
xiami_world3 天前
Claude Design vs. 博思AIPPT深度对比:从架构、交互、数据处理看垂直AI PPT工具的优势
人工智能·ai·信息可视化·powerpoint·思维导图·ppt