We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to Get Paragraph height and width in Pixel?

Hello Team,
I am using Aspose.Words. How to get height and width of paragraph in pixel ?
Please provide any guide for my requirements.

@AlpeshChaudhari Aspose.Words provides LayoutCollector and LayoutEnumerator classes which allow to work with document layout. To calculate rectangle occupied by a paragraph you should insert a bookmark at the paragraph’s start and end to use them as markers for LayoutCollector and then calculate dimensions of the paragraph. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");

// DocumentBuilder will be used to insert a temporary bookmakrs.
DocumentBuilder builder = new DocumentBuilder(doc);

// Get the paragrap we need to calculate the rect. For example the third one.
Paragraph para = doc.FirstSection.Body.Paragraphs[3];

// Wrap the paragraph into a temporary bookmark.
builder.MoveTo(para.FirstChild); // Moves the DocumentBuilder cursor to the beginning of the paragraph.
BookmarkStart bkStart = builder.StartBookmark("tmp");
builder.MoveTo(para); // Moves the DocumentBuilder cursor to the end of the paragraph.
BookmarkEnd bkEnd = builder.EndBookmark(bkStart.Name);

// Use LayoutCollector and LayoutEnumerator to calculate paragraph bounds.
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

enumerator.Current = collector.GetEntity(bkStart);
while (enumerator.Type != LayoutEntityType.Line)

// Get the rectangle occuped by the first line of the paragraph.
RectangleF rect1 = enumerator.Rectangle;

// Now do the same woth the last line.
enumerator.Current = collector.GetEntity(bkEnd);
while (enumerator.Type != LayoutEntityType.Line)

RectangleF rect2 = enumerator.Rectangle;

// Union of the rectangles is the region occuped by the paragraph.
RectangleF result = RectangleF.Union(rect1, rect2);

// For demonstraction purposes draw rectangle shape around the paragraph.
Shape rectShape = new Shape(doc, ShapeType.Rectangle);
rectShape.StrokeColor = Color.Red;
rectShape.StrokeWeight = 1;
rectShape.Filled = false;
rectShape.Width = result.Width;
rectShape.Height = result.Height;
rectShape.WrapType = WrapType.None;
rectShape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
rectShape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
rectShape.Left = result.Left;
rectShape.Top = result.Top;



Node: the code demonstrates the basic technique and does not work for paragraphs spanned several pages.

Note: Aspose.Words uses points as the measurement unit. If you need to convert the dimensions in other units you should use ConvertUtil.

@alexey.noskov thanks for response…

1 Like