在 .NET 中管理演示文稿超链接
超链接是对对象、数据或某处位置的引用。这些是在 PowerPoint 演示文稿中常见的超链接:
- 在文本、形状或媒体中链接到网站
- 链接到幻灯片
Aspose.Slides for .NET 允许您在演示文稿中执行许多与超链接相关的任务。
添加 URL 超链接
向文本添加 URL 超链接
此 C# 代码演示如何向文本添加网站超链接:
using (Presentation presentation = new Presentation())
{
IAutoShape shape1 = presentation.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 600, 50, false);
shape1.AddTextFrame("Aspose: File Format APIs");
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick.Tooltip = "More than 70% Fortune 100 companies trust Aspose APIs";
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FontHeight = 32;
presentation.Save("presentation-out.pptx", SaveFormat.Pptx);
}
向形状或框架添加 URL 超链接
此 C# 示例代码演示如何向形状添加网站超链接:
using (Presentation pres = new Presentation())
{
IShape shape = pres.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 600, 50);
shape.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
shape.HyperlinkClick.Tooltip = "More than 70% Fortune 100 companies trust Aspose APIs";
pres.Save("pres-out.pptx", SaveFormat.Pptx);
}
向媒体添加 URL 超链接
Aspose.Slides 允许您向图像、音频和视频文件添加超链接。
此示例代码演示如何向图像添加超链接:
using (Presentation pres = new Presentation())
{
// 向演示文稿添加图像
IPPImage image = pres.Images.AddImage(File.ReadAllBytes("image.png"));
// 在第 1 张幻灯片上创建基于先前添加的图像的图片框
IPictureFrame pictureFrame = pres.Slides[0].Shapes.AddPictureFrame(ShapeType.Rectangle, 10, 10, 100, 100, image);
pictureFrame.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
pictureFrame.HyperlinkClick.Tooltip = "More than 70% Fortune 100 companies trust Aspose APIs";
pres.Save("pres-out.pptx", SaveFormat.Pptx);
}
此示例代码演示如何向音频文件添加超链接:
using (Presentation pres = new Presentation())
{
IAudio audio = pres.Audios.AddAudio(File.ReadAllBytes("audio.mp3"));
IAudioFrame audioFrame = pres.Slides[0].Shapes.AddAudioFrameEmbedded(10, 10, 100, 100, audio);
audioFrame.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
audioFrame.HyperlinkClick.Tooltip = "More than 70% Fortune 100 companies trust Aspose APIs";
pres.Save("pres-out.pptx", SaveFormat.Pptx);
}
此示例代码演示如何向视频添加超链接:
using (Presentation pres = new Presentation())
{
IVideo video = pres.Videos.AddVideo(File.ReadAllBytes("video.avi"));
IVideoFrame videoFrame = pres.Slides[0].Shapes.AddVideoFrame(10, 10, 100, 100, video);
videoFrame.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
videoFrame.HyperlinkClick.Tooltip = "More than 70% Fortune 100 companies trust Aspose APIs";
pres.Save("pres-out.pptx", SaveFormat.Pptx);
}
Tip
您可能想查看 管理 OLE。使用超链接创建目录
由于超链接允许您添加对对象或位置的引用,您可以使用它们来创建目录。
此示例代码演示如何使用超链接创建目录:
using (var presentation = new Presentation())
{
var firstSlide = presentation.Slides[0];
var secondSlide = presentation.Slides.AddEmptySlide(firstSlide.LayoutSlide);
var contentTable = firstSlide.Shapes.AddAutoShape(ShapeType.Rectangle, 40, 40, 300, 100);
contentTable.FillFormat.FillType = FillType.NoFill;
contentTable.LineFormat.FillFormat.FillType = FillType.NoFill;
contentTable.TextFrame.Paragraphs.Clear();
var paragraph = new Paragraph();
paragraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
paragraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
paragraph.Text = "Title of slide 2 .......... ";
var linkPortion = new Portion();
linkPortion.Text = "Page 2";
linkPortion.PortionFormat.HyperlinkManager.SetInternalHyperlinkClick(secondSlide);
paragraph.Portions.Add(linkPortion);
contentTable.TextFrame.Paragraphs.Add(paragraph);
presentation.Save("link_to_slide.pptx", SaveFormat.Pptx);
}
格式化超链接
颜色
使用 IHyperlink 接口中的 ColorSource 属性,您可以设置超链接的颜色,也可以获取超链接的颜色信息。此功能首次在 PowerPoint 2019 中引入,因此对属性的更改不适用于旧版 PowerPoint。
此示例代码演示了向同一幻灯片添加不同颜色超链接的操作:
using (Presentation presentation = new Presentation())
{
IAutoShape shape1 = presentation.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 450, 50, false);
shape1.AddTextFrame("This is a sample of colored hyperlink.");
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick.ColorSource = HyperlinkColorSource.PortionFormat;
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FillFormat.FillType = FillType.Solid;
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FillFormat.SolidFillColor.Color = Color.Red;
IAutoShape shape2 = presentation.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 100, 200, 450, 50, false);
shape2.AddTextFrame("This is a sample of usual hyperlink.");
shape2.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
presentation.Save("presentation-out-hyperlink.pptx", SaveFormat.Pptx);
}
声音
Aspose.Slides 提供以下属性,以便您通过声音强调超链接:
添加超链接声音
此 C# 代码演示如何设置播放声音的超链接,并使用另一个超链接停止它:
using (Presentation pres = new Presentation())
{
// 向演示文稿的音频集合添加新音频
IAudio playSound = pres.Audios.AddAudio(File.ReadAllBytes("sampleaudio.wav"));
ISlide firstSlide = pres.Slides[0];
// 添加指向下一张幻灯片的超链接形状
IShape firstShape = firstSlide.Shapes.AddAutoShape(ShapeType.SoundButton, 100, 100, 100, 50);
firstShape.HyperlinkClick = Hyperlink.NextSlide;
// 检查超链接是否为“无声音”
if (!firstShape.HyperlinkClick.StopSoundOnClick && firstShape.HyperlinkClick.Sound == null)
{
// 设置播放声音的超链接
firstShape.HyperlinkClick.Sound = playSound;
}
// 添加空白幻灯片
ISlide secondSlide = pres.Slides.AddEmptySlide(firstSlide.LayoutSlide);
// 添加带有 NoAction 超链接的新形状
IShape secondShape = secondSlide.Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 100, 50);
secondShape.HyperlinkClick = Hyperlink.NoAction;
// 设置超链接的“停止先前声音”标志
secondShape.HyperlinkClick.StopSoundOnClick = true;
pres.Save("hyperlink-sound.pptx", SaveFormat.Pptx);
}
提取超链接声音
此 C# 代码演示如何提取超链接使用的声音:
using (Presentation pres = new Presentation("hyperlink-sound.pptx"))
{
ISlide firstSlide = pres.Slides[0];
// 获取第一个形状的超链接
IHyperlink link = firstSlide.Shapes[0].HyperlinkClick;
if (link.Sound != null)
{
// 将超链接的声音提取为字节数组
byte[] audioData = link.Sound.BinaryData;
}
}
从演示文稿中删除超链接
从文本中删除超链接
此 C# 代码演示如何从演示文稿幻灯片中的文本删除超链接:
using (Presentation pres = new Presentation("pres.pptx"))
{
ISlide slide = pres.Slides[0];
foreach (IShape shape in slide.Shapes)
{
IAutoShape autoShape = shape as IAutoShape;
if (autoShape != null)
{
foreach (IParagraph paragraph in autoShape.TextFrame.Paragraphs)
{
foreach (IPortion portion in paragraph.Portions)
{
portion.PortionFormat.HyperlinkManager.RemoveHyperlinkClick();
}
}
}
}
pres.Save("pres-removed-hyperlinks.pptx", SaveFormat.Pptx);
}
从形状或框架中删除超链接
此 C# 代码演示如何从演示文稿幻灯片中的形状删除超链接:
using (Presentation pres = new Presentation("demo.pptx"))
{
ISlide slide = pres.Slides[0];
foreach (IShape shape in slide.Shapes)
{
shape.HyperlinkManager.RemoveHyperlinkClick();
}
pres.Save("pres-removed-hyperlinks.pptx", SaveFormat.Pptx);
}
可变超链接
Hyperlink 类是可变的。使用此类,您可以更改以下属性的值:
此代码片段演示如何向幻灯片添加超链接并随后编辑其工具提示:
using (Presentation presentation = new Presentation())
{
IAutoShape shape1 = presentation.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 600, 50, false);
shape1.AddTextFrame("Aspose: File Format APIs");
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick.Tooltip = "More than 70% Fortune 100 companies trust Aspose APIs";
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FontHeight = 32;
presentation.Save("presentation-out.pptx", SaveFormat.Pptx);
}
IHyperlinkQueries 中支持的属性
您可以从演示文稿、幻灯片或定义了超链接的文本中访问 IHyperlinkQueries。
IHyperlinkQueries 类支持以下方法和属性:
- IHyperlinkQueries.GetHyperlinkClicks();
- IHyperlinkQueries.GetHyperlinkMouseOvers();
- IHyperlinkQueries.GetAnyHyperlinks();
- IHyperlinkQueries.RemoveAllHyperlinks();
常见问题
如何在内部导航时不仅定位到幻灯片,还定位到“章节”或章节的第一张幻灯片?
PowerPoint 中的章节是幻灯片的分组;导航技术上仍然定位到具体的幻灯片。若要“导航到章节”,通常链接到该章节的第一张幻灯片。
我可以将超链接附加到母版幻灯片元素,以便在所有幻灯片上使用吗?
可以。母版幻灯片和版面布局元素支持超链接。此类链接会出现在子幻灯片上,并在放映时可点击。
在导出为 PDF、HTML、图像或视频时,超链接会被保留吗?
在 PDF 和 HTML 中,是的——链接通常会被保留。导出为 images 和 video 时,由于这些格式的本质(光栅帧/视频不支持超链接),可点击性将不再保留。