【Openxml】如何为OpenXml元素创建超链接

已知在OpenXml有以下几种超链接

功能 说明
跳转页面 跳转某一页:ppaction://hlinksldjump 跳转第一页:ppaction://hlinkshowjump?jump=firstslide 跳转最后一页:ppaction://hlinkshowjump?jump=lastslide 跳转下一页:ppaction://hlinkshowjump?jump=nextslide 跳转上一页:ppaction://hlinkshowjump?jump=previousslide
跳转文件 跳转打开本地文件:ppaction://hlinkfile 跳转打开Office支持的主流文档类文件(office系文档、pdf、txt...):ppaction://hlinkpres?slideindex=1&slidetitle=
跳转网页

例子

我们先准备好这样一份pptx文件,如图:

我们分别为四个形状插入超链接,代码如下:

c# 复制代码
    internal class Program
    {
        static void Main(string[] args)
        {

            var mainExecuteDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            var pptFilePath = Path.Combine(mainExecuteDirectory!, "YourPresentation.pptx");


            // 打开一个PPTX文档
            using PresentationDocument presentationDocument = PresentationDocument.Open(pptFilePath, true);

            var slideIdList = presentationDocument.PresentationPart!.Presentation.SlideIdList;
            if (slideIdList is null || !slideIdList.Any())
            {
                return;
            }

            var firstSlideId =(SlideId) slideIdList.First();
            // 获取页面内容
            var firstSlidePart = (SlidePart) presentationDocument.PresentationPart.GetPartById(firstSlideId.RelationshipId!.Value!);



            //设置跳转到第三页
            var targetSlideId = (SlideId) slideIdList.ChildElements[2];
            var targetSlidePart = (SlidePart) presentationDocument.PresentationPart.GetPartById(targetSlideId.RelationshipId!.Value!);
            var relationshipId = firstSlidePart.CreateRelationshipToPart(targetSlidePart);

            var shapeElements = firstSlidePart.Slide.CommonSlideData!.ShapeTree!.Elements<Shape>().ToList();

            //第一个形状设置跳转第三页
            shapeElements[0]!.NonVisualShapeProperties!.NonVisualDrawingProperties!.HyperlinkOnClick = new HyperlinkOnClick()
            {
                Action = PptAction.SlideJump,
                Id = relationshipId
            };


            //第二个形状设置跳转下一页
            var shapeElement = shapeElements[1];
            shapeElement.NonVisualShapeProperties!.NonVisualDrawingProperties!.HyperlinkOnClick = new HyperlinkOnClick()
            {
                Action = PptAction.JumpNextSlide,
            };

            var filePath = Path.Combine(mainExecuteDirectory!, "两只老虎-原声.mp3");
            var fileHyperlinkRelationship = firstSlidePart.AddHyperlinkRelationship(new Uri(filePath, UriKind.Absolute), true);
            //第三个形状设置打开文件
            shapeElements[2]!.NonVisualShapeProperties!.NonVisualDrawingProperties!.HyperlinkOnClick = new HyperlinkOnClick()
            {
                Action = PptAction.OpenFile,
                Id = fileHyperlinkRelationship.Id
            };

            //第四个形状设置打开网页链接
            var httpHyperlinkRelationship = firstSlidePart.AddHyperlinkRelationship(new Uri($"http://www.baidu.com", UriKind.Absolute), true);
            shapeElements[3]!.NonVisualShapeProperties!.NonVisualDrawingProperties!.HyperlinkOnClick = new HyperlinkOnClick()
            {
                Id = httpHyperlinkRelationship.Id
            };

            // 保存并关闭文档
            presentationDocument.Save();
        }
    }

    public static class PptAction
    {
        /// <summary>
        /// 跳转页面
        /// </summary>
        public const string SlideJump = "ppaction://hlinksldjump";

        /// <summary>
        /// 跳转下一页
        /// </summary>
        public const string JumpNextSlide = "ppaction://hlinkshowjump?jump=nextslide";

        /// <summary>
        /// 打开文件
        /// </summary>
        public const string OpenFile = "ppaction://hlinkfile";

    }

效果如下:

源码

源码链接

相关推荐
c#上位机2 小时前
wpf之TextBlock
c#·wpf
almighty2711 小时前
C# WinForm分页控件实现与使用详解
c#·winform·分页控件·c#分页·winform分页
almighty2712 小时前
C#实现导入CSV数据到List<T>的完整教程
c#·csv·格式转换·c#导入数据·csv数据导入
程序猿多布13 小时前
Lua和C#比较
c#·lua
csdn_aspnet1 天前
使用 MongoDB.Driver 在 C# .NETCore 中实现 Mongo DB 过滤器
mongodb·c#·.netcore
csdn_aspnet1 天前
使用 C# .NETCore 实现MongoDB
mongodb·c#·.netcore
上位机付工1 天前
上位机通信速度有多快?
开发语言·c#·上位机·plc
FuckPatience1 天前
C# 修改基类List中某一元素的子类类型
c#·list
玉面小君1 天前
从 WPF 到 Avalonia 的迁移系列实战篇6:ControlTheme 和 Style区别
c#·wpf·avalonia