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

相关推荐
AI工具指南3 小时前
从复制粘贴到一键生成:2026年AI生成PPT工具使用指南
人工智能·powerpoint·ppt
身如柳絮随风扬4 小时前
Apache POI导出Word,PPT完整实现
spring boot·word·powerpoint·apache
带土115 天前
11. MathType卸载后PowerPoint启动时无显示法加载MathType公式编辑器加载项
powerpoint
m5655bj15 天前
通过 C# 将 PPT 文档转换为 HTML 格式
c#·html·powerpoint
道纪书生16 天前
解决报错:很抱歉,powerpoint/word/excel遇到错误,使其无法正常工作......
word·powerpoint·excel
johnny23317 天前
PPT生成工具
powerpoint
小真zzz20 天前
ChatPPT Nano Banana Pro · Magic模式深度解析 ——重新定义“所想即所得”的PPT智能编辑
人工智能·ai·powerpoint·ppt·aippt
王解22 天前
AI生成PPT的技术演进:从智能填充到认知增强
人工智能·powerpoint