已知在OpenXml有以下幾種超鏈接 功能 說明 跳轉頁面 跳轉某一頁:ppaction://hlinksldjump跳轉第一頁:ppaction://hlinkshowjump?jump=firstslide跳轉最後一頁:ppaction://hlinkshowjump?jump=lastslid ...
已知在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";
}
效果如下: