Hi, nice to ask a question
I want to know width in points of a paragraph?
image.png (53.1 KB)
width in points of paragraph.docx (67.2 KB)
Thank you
Hi, nice to ask a question
I want to know width in points of a paragraph?
image.png (53.1 KB)
width in points of paragraph.docx (67.2 KB)
Thank you
@quanghieumylo As you may know, MS Word documents are flow documents and do not contain any information about document layout. The consumer applications, like MS Word or Open Office builds document layout on the fly. Aspose.Words uses itβs own layout engine to build document layout to render the document to PDF or any other fixed page formats and for printing. Also, Aspose.Words provides LayoutCollector and LayoutEnumerator classes, that allows to get layout information from the document.
Your requirements can be achieved using LayoutCollector
and LayoutEnumerator
classes. It is required to wrap each paragraph in your document into a bookmark and then determine rectangle of bookmark start and bookmark end using LayoutCollector
and LayoutEnumerator
. Union of these rectangles will give you bounding box of the paragraph calculated by Aspose.Words layout engine. For example see the following code:
Document doc = new Document("C:\\Temp\\in.docx");
// Wrap paragraphs into bookmarks to be able to calculate bounds of paragraphs.
List<Paragraph> paragraphs = doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>().ToList();
List<string> paraBookmakrs = new List<string>();
int i = 0;
foreach (Paragraph p in paragraphs)
{
// LayoutCollector and LayoutEnumerator does not work with nodes in header/footer
if (p.GetAncestor(NodeType.HeaderFooter) != null)
continue;
string bkName = "tmp_bookmark_" + i;
paraBookmakrs.Add(bkName);
i++;
p.PrependChild(new BookmarkStart(doc, bkName));
p.AppendChild(new BookmarkEnd(doc, bkName));
}
// Create LayoutCollector and LayoutEnumerator.
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);
// Calculate bounding box of the first and the last lines of the paragraphs.
foreach (string tmpBkName in paraBookmakrs)
{
Bookmark bk = doc.Range.Bookmarks[tmpBkName];
// Move enumerator to the start of the bookmark.
enumerator.Current = collector.GetEntity(bk.BookmarkStart);
// Move enumerator to the line where the bookmark is located.
while (enumerator.Type != LayoutEntityType.Line)
enumerator.MoveParent();
// Print bounding box of the first line of the paragraph.
RectangleF rect = enumerator.Rectangle;
Console.WriteLine(rect);
// Do the same for the bookmark end.
enumerator.Current = collector.GetEntity(bk.BookmarkEnd);
while (enumerator.Type != LayoutEntityType.Line)
enumerator.MoveParent();
rect = enumerator.Rectangle;
Console.WriteLine(rect);
Console.WriteLine("===============================");
// If you need to calculate bounding box of the whole paragraph,
// you can calculate it as union of the start and end lines
// if the paragraph is located on the same page and the same text column.
// Otherwise it will be required to add additional logic to calculate bounding box
// of paragraphs' lines located on different pages or text columns
}