Skip to main content

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++) {
            resultBarStaticItem.Caption = String.Format("Document has {0} lines", visitor.RowIndex);