【问题标题】:How to read the hyper-link of an image in power Point using c#如何使用 c# 在 power Point 中读取图像的超链接
【发布时间】:2014-07-22 18:57:41
【问题描述】:

我已经使用 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;
                    }
                }
            }

        }

在变量address中获取超链接地址的地方,同样我需要从我使用c#插入到PPT中的图像中获取超链接。

有可能吗??

【问题讨论】:

    标签: c# asp.net hyperlink ms-office office-interop


    【解决方案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);
    

    【讨论】:

    • @vDhhnal : 但它也可能显示其他人的超链接。?
    • 是的,你必须先通过它的ID找到合适的形状,然后你才能很容易地得到超链接。
    • 但是如何区分文本框和图表之间的形状
    • 好吧,我会试试这个。
    • @Arshad - 对于文本框,我猜 HasTextFrame 属性将为真。您可以检查该属性的哪些形状是错误的...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    • 2021-07-02
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多