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

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

Upload your file(Drag files here)

Download an input file

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

class Sample
    static void Main(string[] args)
        // If using Professional version, put your serial key below.

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

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.

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

    End Sub

End Module