Free Support Forum - aspose.com

How to get paragraph position ( X, Y, Width and Height) (C# .NET)

There is some text inside a shape. (Create the page from the default ppt theme)


We can get the position and size of the red rectangle. but how about the green one( the exact text frame position without margin, padding etc.)

Hi Jollibee,


I have observed your requirements. I have shared a piece of code in a text file with you. I hope you will find it helpful to achieve your requirements.

Best Regards,

I tried the sample code, but I didn’t get what I want.


There are two title in pres.pptx, and The two text box has different margins.

But I got the same result for two text box:

margin top: 3.6
margin left: 7.2
margin right: 7.2
margin bottom: 3.6

margin top: 3.6
margin left: 7.2
margin right: 7.2
margin bottom: 3.6

Hi Jollibee,


I have observed your comments. We are working on this and will get back to you with feedback soon.

Best Regards,

Hi Jollibee,


We have worked on your presentation. You got these values because does not use margin values. You use text orientation and etc. Please check this code snippet . These two lines of code check text position in shape. You use 2 text positions for Text 1 - Center for Text 2 - Bottom, you does not change Margin values manually. All margin values for both shapes are default values. Also please see attachments.

<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>System.<span class=“kwrd” style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>out<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>.println("Anchoring Type " +textframe.getTxtFrameFormat().getAnchoringType());

System.out.println("Center Text "+(textframe.getTextFrameFormat().getCenterText() == NullableBool.True));

Best Regards,

You’re right. I can get the anchoring type from your code.


But I still got the question. How can I get the exact position(x, y, width, height) of the two text box without any margins. (the position of the red rectangle in the attachment picture.)

We still need more param to calculate the position even if we know the anchoring type. For example, the height of the text?

Hi Jollibee,


We are working on this and will get back to you with feedback soon.

Best Regards,

@tonnyjaasksongmail,

I have observed your requirements and like to share that Aspose.Slides exposes the API calls to not only access any paragraph’s but its respective portions position coordinates. Which includes access to X, Y position, width and height of any paragraph or portion belonging to any text frame. The following example demonstrates the usage of the API calls to fulfill the requirement.

	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)