首页 > 解决方案 > 如何使用 Open XML 在 PowerPoint 幻灯片中插入超链接?

问题描述

我有一个场景,我必须用表格数据替换幻灯片模板中的某些变量,但在这种情况下,数据和幻灯片文本是重叠的,经过一些研究,我发现 PowerPoint 不是为这种情况设计的[MS Link](img 1 )。为了克服这个问题,我不应该用表格数据替换变量,而是应该用链接替换变量,该链接将指向新创建的幻灯片,我可以在其中发布表格数据(img 2)。

所以回到我的问题,有什么方法可以在不弄乱模板的情况下编写数据?或如何将变量替换为幻灯片的超链接?

图像 1

图像 2

标签: c#asp.netpowerpointopenxmlpresentation

解决方案


根据文档,您可以执行类似的操作,只需根据您的情况以及如何找到变量 1 进行调整。

    // Open the presentation file as read-only.
    using (PresentationDocument document = PresentationDocument.Open(fileName, false))
    {
        // Iterate through all the slide parts in the presentation part.
        foreach (SlidePart slidePart in document.PresentationPart.SlideParts)
        {
            IEnumerable<Drawing.HyperlinkType> links = slidePart.Slide.Descendants<Drawing.HyperlinkType>();

            // Iterate through all the links in the slide part.
            foreach (Drawing.HyperlinkType link in links)
            {
                // Iterate through all the external relationships in the slide part. 
                foreach (HyperlinkRelationship relation in slidePart.HyperlinkRelationships)
                {
                    // If the relationship ID matches the link ID…
                    if (relation.Id.Equals(link.Id))
                    {
                        // Add the URI of the external relationship to the list of strings.
                        ret.Add(relation.Uri.AbsoluteUri);
                    }
                }
            }
        }
    }

推荐阅读