Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

How to: Count the Lines in the Document

To count the lines in the document (document rows), create an instance of the LayoutVisitor descendant and let it traverse the page layout. When it encounters a new row, it calls the VisitRow method. This method increments the row counter to count the lines on a page.

To run the visitor each time after building the document layout, handle the DocumentLayout.DocumentFormatted event.

View Example

private void DocumentLayout_DocumentFormatted(object sender, EventArgs e) {
    this.BeginInvoke((MethodInvoker)(() =>
    {
        if (this.Visible) {
            MyLayoutVisitor visitor = new MyLayoutVisitor(richEditControl1.Document);
            int pageCount = richEditControl1.DocumentLayout.GetFormattedPageCount();

            for (int i = 0; i < pageCount; i++) {
                visitor.Visit(richEditControl1.DocumentLayout.GetPage(i));
            }
            resultBarStaticItem.Caption = String.Format("Document has {0} lines", visitor.RowIndex);
        }
    }));
}