GemBox.Document is a .NET component that enables you to read, write, edit, convert, and print document files from your .NET applications using one simple API.

Iterating

GemBox.Document represents a document as a tree-like structure consisting of various document elements with a DocumentModel element as a root element.

GemBox.Document provides a way to flatten document representation, so it can be iterated.

Following example demonstrates how to iterate over a document content tree and filter an element sequence to a specific document element types.

Screenshot
Iterating Screenshot

See the full code below, use Run Example to execute.

Upload your file(Drag files here)

Download a sample file

using System;
using System.IO;
using System.Linq;
using System.Text;
using GemBox.Document;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        DocumentModel document = DocumentModel.Load("Reading.docx");

        int numberOfSections = document.Sections.Count;
        int numberOfParagraphs = document.GetChildElements(true, ElementType.Paragraph).Count();
        int numberOfRunsAndFields = document.GetChildElements(true, ElementType.Run, ElementType.Field).Count();
        int numberOfInlines = document.GetChildElements(true).OfType<Inline>().Count();
        
        int elements = document.Sections[0].GetChildElements(true).Count();

        StringBuilder sb = new StringBuilder();
        sb.AppendLine("File has:");
        sb.AppendLine(numberOfSections + " section");
        sb.AppendLine(numberOfParagraphs + " paragraphs");
        sb.AppendLine(numberOfRunsAndFields + " runs and fields");
        sb.AppendLine(numberOfInlines + " inlines");
        sb.AppendLine("First section contains " + elements + " elements");

        Console.WriteLine(sb.ToString());
    }
}
Imports System
Imports System.IO
Imports System.Linq
Imports System.Text
Imports GemBox.Document

Module Samples

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document As DocumentModel = DocumentModel.Load("Reading.docx")

        Dim numberOfSections As Integer = document.Sections.Count
        Dim numberOfParagraphs As Integer = document.GetChildElements(True, ElementType.Paragraph).Count()
        Dim numberOfRunsAndFields As Integer = document.GetChildElements(True, ElementType.Run, ElementType.Field).Count()
        Dim numberOfInlines As Integer = document.GetChildElements(True).OfType(Of Inline)().Count()

        Dim elements As Integer = document.Sections(0).GetChildElements(True).Count()

        Dim sb As New StringBuilder()
        sb.AppendLine("File has:")
        sb.AppendLine(numberOfSections & " section")
        sb.AppendLine(numberOfParagraphs & " paragraphs")
        sb.AppendLine(numberOfRunsAndFields & " runs and fields")
        sb.AppendLine(numberOfInlines & " inlines")
        sb.AppendLine("First section contains " & elements & " elements")
        Console.WriteLine(sb.ToString())

    End Sub

End Module

Check next sample.