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

Free Support Forum - aspose.com

Detect if a heading takes up several lines

Is there a way to detect whether a heading takes up more than one line? I’d like to be able to reduce the font size of some headings if they take up several lines. That is, reduce the font size until the heading only takes up one or two lines instead of 3 or 4 (for example)


Hi Marc,

for your inquiry. The Aspose.Words.Layout namespace provides classes
that allow to access information such as on what page and where on a
page particular document elements are positioned, when the document is
formatted into pages.

Please check “DocumentLayoutHelper” example project in Aspose.Words for .NET examples repository at GitHub. Using DocumentLayoutHelper utility, you can get the lines of a page. Please use following code example to get the total number of lines in HeaderFooterType.HeaderPrimary.

Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "in.docx");
// Copy primary header contents into first sections's body.
Document docClone = (Document)doc.Clone(true);
foreach (Paragraph paragraph in docClone.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary].GetChildNodes(NodeType.Paragraph, true))
// Clears the headers and footers of this section. 
// Remove first empty paragraph. 
RenderedDocument layoutDoc = new RenderedDocument(docClone);
RenderedPage page = layoutDoc.Pages[0];
LayoutCollection<LayoutEntity> lines = page.GetChildEntities(LayoutEntityType.Line, true);
Console.WriteLine("Total lines in HeaderPrimary {0} ", lines.Count);

Thanks very much. That looks promising. I’ll post back here once I’ve had time to try it out and let you know how I go.

Hi Marc,

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

Thanks Tahir. I’ve decided against using this for now as it will probably slow down the creation of documents, but thanks for posting the solution.