No of Lines in word document at runtime as i modify

Hi,

How to get no of lines in a word document. Cant find any relevant method?

Hi there,

Thanks 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.

In your case, I suggest you please use DocumentLayoutHelper code example to achieve your requirements. Please check “DocumentLayoutHelper” example project in Aspose.Words for .NET examples repository at GitHub.

Please use the following code example to get the total number of lines of document. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToDocumentEnd();
builder.Writeln("Get total number of lines of document.");
RenderedDocument layoutDoc = new RenderedDocument(doc);
int totallines = 0;
foreach (RenderedPage page in layoutDoc.Pages)
{
    LayoutCollection<LayoutEntity> lines = page.GetChildEntities(LayoutEntityType.Line, true);
    totallines += lines.Count;
}
Console.WriteLine("Lines: {0}", totallines);

Hi Tahir,

Thank you for your reply.

I have been using the same code as you mentioned above to get the line count. Actually i have 100-200 or more documents which i need to modify and then convert to pdf. I need to calculate line count for every document multiple times and based on that do some changes in doc file. The problem i am facing is the code for calculating no of lines is doing lot of recursive calls and is taking lot of system memory. As i convert to documents to pdf i get OutOfMemory exception.

Also i dont see any IDisposable implemented in RenderedDocument? Also is there any other way to get line count?
Appreciate your quick response.

private void ProcessDocument(Aspose.Words.Document doc)
{
    Int16 noOfLines = 2;
    RenderedDocument layoutDoc = new RenderedDocument(doc);
    foreach (RenderedPage page in layoutDoc.Pages)
    {
        // Logic here calls GetTotalCount frequently, to check no of lines.
        if (GetTotalCount(doc) > noOfLines)
        {
            // Do some processing to modify the document. After this again check no of lines in the document
        }
    }
}
private int GetTotalCount(Aspose.Words.Document doc)
{
    RenderedDocument layoutDoc = new RenderedDocument(doc);
    int linesTotalCount = 0;
    foreach (RenderedPage page in layoutDoc.Pages)
    {
        foreach (RenderedColumn column in page.Columns)
        {
            linesTotalCount += column.Lines.Count;
        }
    }
    return linesTotalCount;
}

Hi there,

Thanks for your inquiry. Could you please attach your input Word document here for testing? I will investigate the issue on my side and provide you more information.