Get line count of specific page of document using C#

Hi, I am researching for a component that can count lines from the last page on an RTF document, currently I am converting it or saving it as docx, but counting lines have been so painful, is there a way to do it with aspose.words? I have tried many things but nothing accurate so far. It is critical to take a decision on which product to buy right now.

Thanks in advance.

@maik1

Please check “DocumentLayoutHelper” utility in Aspose.Words for .NET examples repository at GitHub. With the help of this utility, you can achieve your requirement. Please check the following code example. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
// Create a new RenderedDocument class from a Document object.
RenderedDocument layoutDoc = new RenderedDocument(doc);

Console.WriteLine(layoutDoc.Pages[doc.PageCount - 1].Columns[0].Lines.Count);

You can also achieve your requirement using PageSplitter utility. You can extract the last page of document using this utility and use BuiltInDocumentProperties.Lines property to get the estimated number of lines in the document.

Hope this helps you.

If you are using Aspose.Words for Java, please check the code from Aspose.Words for Java example repository at Github.

Thanks I tried that code first and it didn’t work because I was using trial version. After requesting a temporary license it worked perfectly fine. I will do more testing and let you know if anything is wrong.

Thanks a lot

@maik1

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

Hello @tahir.manzoor I was trying to count lines on an RTF document but somehow the RenderedDocument gets a misterious Section Break (Continuos) as line which sometime is ok but sometimes is not ok, do you know how can I get exact real lines visualized in MS word for example? my goal is to count real lines as they are visible when you open an RTF document on MS word.

Thanks in advance

@maik1

In your case, we suggest you please clone the document and remove the section break and then get the line count. Hope this helps you.

thanks @tahir.manzoor Now I am facing another question/problem … is it possible to do this line count considering font styling? currently I have a document with font size 18 and font name “Arial Black” and it is not counting lines properly.

Thanks in advance

@maik1

Please make sure that the fonts used in your document are installed on the system where you are getting line count.

If you still face problem, please ZIP and attach your input document along with fonts here for testing. We will investigate the issue and provide you more information on it.

Thanks @tahir.manzoor please find attached the file, it has set up a font size 18 and font name Arial Black the last page has 7 lines in it but the count I got is 11 lines. Please let me know if you need the code that I am using to calculate it.6 (1).zip (1.6 KB)

@maik1

We have tested the scenario using the latest version of Aspose.Words for .NET 20.2 with following code example and have not found the shared issue. So, please use Aspose.Words for .NET 20.2.

Document doc = new Document(MyDir + "6.rtf");
// Create a new RenderedDocument class from a Document object.
RenderedDocument layoutDoc = new RenderedDocument(doc);

for (int i = 0; i < doc.PageCount; i++)
{
    Console.WriteLine(layoutDoc.Pages[i].Columns[0].Lines.Count);
}

Hi @tahir.manzoor, what is the count you get on that Console.Writeline for the last page? Like this layoutDoc1.Pages.Last.Columns[0].Lines.Count. I am receiving 11 and if you open the document on the last page there are just 7 lines, so the count is wrong. Can you confirm please?
Thanks

@maik1

Please check the attached image of test result at our end. The line count for last page is 7.
line numbers.png (69.4 KB)

Thanks @tahir.manzoor, I had another case with different margin and font size. Could you please try? I am getting 25 lines where there are 17.8.zip (1.9 KB)

@maik1

We have tested the scenario and noticed that the text position is changed in layout model. It is moved to next line. So, you are getting incorrect line count. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-20050. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Thank you @tahir.manzoor , in case you need my license information I can provide it over an email or something more private.

Hi @tahir.manzoor, it this command: layoutDoc.Pages.Last.Columns[0].Lines.Count considering lines on the header and the footer? Also, I have been encountering more issues with the line counting with different font sizesm including footer and header and different font families. Please let me know if you want me to share those files with you to solve the issue.

@maik1

Please make sure that you have installed all fonts used in your documents on the machine where you are using layout API (getting page count). You can also check this issue by converting document to PDF. If output PDF is incorrect, you can share your document here for testing. We will investigate the issue and provide you more information on it.

sure, let me try that. is there any paid support to speed up the fix for this issue for example?

@maik1

You reported this issue in free support forum and it will be treated with normal priority. To speed up the progress of issue’s resolution, we suggest you please check our paid support policies from following link.
Paid Support Policies

Hi @tahir.manzoor, I already tested converting files to PDF and the output PDF is incorrect as well, actually the line counter is returning the count as the file was a PDF. I am attaching the files for you to compare them and test them. Please let me know if you need me to do more testing.
testing.zip (115.9 KB)

Thanks.