一种选择是遍历幻灯片上的形状并查看它们是否包含超链接。或者你应该在创建图片时给你的图片一个id,然后通过给定的id找到它。
private void GetHyperlink()
{
Microsoft.Office.Interop.PowerPoint.Application objApp = new Microsoft.Office.Interop.PowerPoint.Application();
objApp.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
Presentations objPresSet = objApp.Presentations;
Presentation p = objPresSet.Open("C:\test.ppt");
Slide slide = p.Slides[1];
// or Slide slide = objApp.ActiveWindow.View.Slide;
for (int i = 1; i <= slide.Shapes.Count; i++)
{
//If the hyperlink address is filled then display it in MessageBox
if (slide.Shapes[i].ActionSettings[PpMouseActivation.ppMouseClick].Hyperlink.Address != null)
MessageBox.Show(slide.Shapes[i].ActionSettings[PpMouseActivation.ppMouseClick].Hyperlink.Address);
}
}
此代码将显示幻灯片 1 上的所有超链接。
此外,将 Open XML SDK 用于此目的而不是自动化也会很有趣。
有趣的链接:
http://www.aspose.com/docs/display/slidesnet/Finding+a+Shape+in+a+Slide
编辑:
我建议您首先修改创建带有超链接的图像的代码,如下所示:
//Add a picture
Shape pic = slide.Shapes.AddPicture(@"C:\Users\Public\Pictures\Sample Pictures\koala.jpg",
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoTrue,
shape.Left, shape.Top, shape.Width, shape.Height);
//Here you have various options how to distinguish your shape
pic.Name = "MyPic";
pic.AlternativeText = "Koala";
pic.Tags.Add("MyPic", "@http://www.google.com/");
//adding hyperlink, etc...
pic.ActionSettings[PpMouseActivation.ppMouseClick].Hyperlink.Address =@"http://www.google.com/"
那么当你阅读文件时,你可以使用标签或名称来区分形状:
//tags check
if (slide.Shapes[i].Tags.Count > 0 && slide.Shapes[i].Tags["MyPic"]!=null && slide.Shapes[i].ActionSettings[PpMouseActivation.ppMouseClick].Hyperlink.Address != null)
MessageBox.Show(slide.Shapes[i].ActionSettings[PpMouseActivation.ppMouseClick].Hyperlink.Address);
//or
//name check
if (slide.Shapes[i].Name.Equals("MyPic", StringComparison.InvariantCultureIgnoreCase) && slide.Shapes[i].ActionSettings[PpMouseActivation.ppMouseClick].Hyperlink.Address != null)
MessageBox.Show(slide.Shapes[i].ActionSettings[PpMouseActivation.ppMouseClick].Hyperlink.Address);