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.

Iterating Screenshot
Upload your file (Drag file here)
using System;
using System.Linq;
using System.Text;
using GemBox.Document;

class Program
    static void Main()
        // If using Professional version, put your serial key below.

        DocumentModel document = DocumentModel.Load("%InputFileName%");

        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");

Imports System
Imports System.Linq
Imports System.Text
Imports GemBox.Document

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.

        Dim document As DocumentModel = DocumentModel.Load("%InputFileName%")

        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")

    End Sub

End Module

Check next example or download examples from GitHub.