我已经使用c#向powerpoint插入了一个图像,并且插入了一个超链接到图片并且它的工作正常。但是现在我需要读取我用c#插入的那张图片的超链接。
然后使用c#将带有超链接的文本插入powerpoint,并通过以下方法从powerpoint读回超链接。
for (int i = 0; i < presentation.Slides.Count; i++)
{
foreach (var item in presentation.Slides[i + 1].Shapes)
{
var shape = (PPT.Shape)item;
if (shape.HasTextFrame == MsoTriState.msoTrue)
{
if (shape.TextFrame.HasText == MsoTriState.msoTrue)
{
var textRange = shape.TextFrame.TextRange;
var text = textRange.Text;
string address=textRange.ActionSettings[PPT.PpMouseActivation.ppMouseClick].Hyperlink.Address;
}
}
}
}
我在变量地址中获取超链接地址,同样我需要从使用c#插入PPT的图像中获取超链接。
有可能。??
答案 0 :(得分:1)
一种选择是遍历幻灯片上的形状并查看它们是否包含超链接。或者你应该在创建它时给你的图片一个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);