Free Support Forum - aspose.com

How to get the coordinates for a hyperlinked text (C# .NET)

I need to be able to get a list of hyperlinks for a slide with the actions and coordinates (x, y, width, height) so we can show a user an image of the slide and respond to taps on the hyperlinks by executing the hyperlink.


It is all easy except for getting the coordinates of a hyperlink which is on a portion rather than on an entire shape. Is there a way to get the coordinates of the portion?

Thanks

Hi Robert,


I have observed your requirements concerning to getting position of hyperlink for portion of text inside any particular shape. I regret to share that there is no property available to get the position of individual portion of text on slide. This feature is even unavailable in PowerPoint as well. The position of the shape containing the text is however available. Please share, if I may help you further in this regard.

Many Thanks,

not that it changes whether this is availble in aspose, but the feature is available in Powerpoint. I am trying to port this from the powerpoint api to aspose.

in powerpoint, if the href type is msoHyperLinkRange, the parent of the href is the action settings object and the parent of action settings object is a text range. Its a little more complicated in a table, but in a normal shape

href.Parent.Parent.BoundHeight
href.Parent.Parent.BoundLeft
href.Parent.Parent.BoundTop
href.Parent.Parent.BoundWidth

gives me what I am looking for.

Hi Robert,


I have tried to understand the feature explanation shared by you and unfortunately I am unable to completely understand it. I request you to please share how can I get the hyperlink position in PowerPoint in the form of presentation and snapshot. I will investigate it on my end to help you further in this regard.

Many Thanks,

Here is an extract giving some context to what I meant. I’m not sure how well this will format. Let me know if this still doesn’t make sense

private void getHyperlinks(Microsoft.Office.Interop.PowerPoint.Slide ppSlide)
{

for (var slideHyperLinkIndex = 1; slideHyperLinkIndex <= ppSlide.Hyperlinks.Count; slideHyperLinkIndex++)
{
try
{
var href = (Microsoft.Office.Interop.PowerPoint.Hyperlink)ppSlide.Hyperlinks[slideHyperLinkIndex];
switch (href.Type)
{
case MsoHyperlinkType.msoHyperlinkRange:
if (href.Parent.Parent.Parent == null)
{
// the parent of the href is the action settings object.
// the parent of the actions settings, for a linkRange is a textRange object
// if the parent of the textRange object is null, the hyperlink is inside a
// table, the binding information is relative to the table instead of absolute.
//
// this isn’t the case I am interested in so this is omitted.
} else {
// this gets me the bounding rectangle for the text link
//
var boundHeight = (int)Math.Round(href.Parent.Parent.BoundHeight, 0);
var boundLeft = (int)Math.Round(href.Parent.Parent.BoundLeft, 0);
var boundTop = (int)Math.Round(href.Parent.Parent.BoundTop, 0);
var boundWidth = (int)Math.Round(href.Parent.Parent.BoundWidth, 0);
}
break;

default:
// other cases omitted since they are just the shape.
break;
}
}
catch (Exception hl1Exc)
{
// error handling omitted for simplicity
}
}
}

Hi Robert,

I have observed the sample code and have discussed with our product team. The X,Y position belong to shapes. We need to investigate the issue and require the working sample project extracting the hyperlink positions and your sample presentation with hyperlink. We will investigate the issue on our end once the requested information will be shared and help you further in this regard.

Many Thanks,

@badams,

I have observed your requirements and now with latest Aspose.Slides API you can achieve your requirements for getting coordinates for hyperlinked text on slide. Actually, the hyperlink on text are defined inside a particular portion belonging to a paragraph of any text frame available on slide. Any shape with text has an associated text frame with it.

We have added the support for getting the X,Y position, width and height of any paragraph and its respective portions inside text frame of any shape on slide. Since, hyperlink is defined on portion of text inside paragraph of a text frame, so when you find particular portion of text with hyperlink defined on it, you can get its coordinates using following example.

	public static void GetPortionCoordinates()
	{
		string dataDir = @"C:\Aspose Data\";
		using (Presentation presentation = new Presentation(dataDir + "TestText.pptx"))
		{
			IAutoShape shape = (IAutoShape)presentation.Slides[0].Shapes[0];
			var textFrame = (ITextFrame)shape.TextFrame;

			foreach (var paragraph in textFrame.Paragraphs)
			{
				Console.WriteLine("The following shows the coordinates for all the portions inside paragraph");

				foreach (Portion portion in paragraph.Portions)
				{
					var hyperlink = portion.PortionFormat.HyperlinkClick;
					
					if (hyperlink != null)
					{
						var rect = portion.GetRect();//.GetCoordinates()
		    			//Console.Write(Environment.NewLine + "Corrdinates X =" + point.X + " Corrdinates Y =" + point.Y);
						Console.WriteLine(String.Format("Portion Text: {4} has coordinates (X: {0}, Y: {1}), Width: {2} and Height: {3}", rect.X, rect.Y, rect.Width, rect.Height, portion.Text));
					}
				}
			}
		}
	}

TestText.zip (24.0 KB)