Free Support Forum - aspose.com

How to access the coordinates of Text within Text Shape (C# .NET)

Hi,


How can I get coordinates (x,y) of the text/paragraph/portion within frame or slide doesn’t matter.

Please, give answer to ppt and pptx.

Regards,

Hi Leonid,


I regret to share that feature of getting X and Y coordinates for individual paragraph, text or portion inside text frame is not available in Aspose.Slides. Moreover, PowerPoint also does not provide this feature support. The X or Y coordinates of shape can be extracted and TextFrame is embedded inside it. The position of text inside text frame varies according to amount of text entered and importantly font size of text. It is not possible to get the position of individual portion or paragraph. The odd work around for this can be that you may add several text shapes with each having one paragraph and one portion. In this way you can have the control of getting text position of individual portion or paragraph.

Many Thanks,

@leonidp,

The new Aspose.Slides API provides you the support for accessing the coordinates position for any text available on slide. As you know the text on slide is actually enclosed in a text frame for any shape. The text frame has got collection of paragraphs and each paragraph contain collection of portions inside it. We have introduced support where you can now access the coordinates of any text available on slide not only on paragraph but portion level well as. In the following example, I hope you will be able to fulfill your requirements for accessing the coordinates for any text on slide.

	public static void GetParagraphCoordinates()
	{
		// The path to the documents directory.
		string dataDir = @"C:\Aspose Data\";

		// Instantiate a Presentation object that represents a presentation file
		using (Presentation presentation = new Presentation(dataDir + "TestText.pptx"))
		{
			IAutoShape shape = (IAutoShape)presentation.Slides[0].Shapes[0];
			var textFrame = (ITextFrame)shape.TextFrame;
			RectangleF rect = ((Paragraph)textFrame.Paragraphs[0]).GetRect();
			Console.WriteLine("The following shows the paragraph's actual coordinates on slide");
			Console.WriteLine(String.Format("Paragraph Text: {4} has coordinates (X: {0}, Y: {1}), Width: {2} and Height{3}", rect.X, rect.Y, rect.Width, rect.Height, ((Paragraph)textFrame.Paragraphs[0]).Text));
		}
	}

	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 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)