【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文件,如图:
我们分别为四个形状插入超链接,代码如下:- 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";
- }
复制代码 效果如下:
源码
源码链接
来源:https://www.cnblogs.com/ryzen/p/18047555
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|
|
|
发表于 2024-3-1 16:47:57
举报
回复
分享
|
|
|
|