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

1using System;
2using System.IO;
3using System.Linq;
4using System.Text;
5using GemBox.Document;
6
7class Sample
8{
9    [STAThread]
10    static void Main(string[] args)
11    {
12        // If using Professional version, put your serial key below.
13        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
14
15        DocumentModel document = DocumentModel.Load("Reading.docx");
16
17        int numberOfSections = document.Sections.Count;
18        int numberOfParagraphs = document.GetChildElements(true, ElementType.Paragraph).Count();
19        int numberOfRunsAndFields = document.GetChildElements(true, ElementType.Run, ElementType.Field).Count();
20        int numberOfInlines = document.GetChildElements(true).OfType<Inline>().Count();
21        
22        int elements = document.Sections[0].GetChildElements(true).Count();
23
24        StringBuilder sb = new StringBuilder();
25        sb.AppendLine("File has:");
26        sb.AppendLine(numberOfSections + " section");
27        sb.AppendLine(numberOfParagraphs + " paragraphs");
28        sb.AppendLine(numberOfRunsAndFields + " runs and fields");
29        sb.AppendLine(numberOfInlines + " inlines");
30        sb.AppendLine("First section contains " + elements + " elements");
31
32        Console.WriteLine(sb.ToString());
33    }
34}
1Imports System
2Imports System.IO
3Imports System.Linq
4Imports System.Text
5Imports GemBox.Document
6
7Module Samples
8
9    Sub Main()
10
11        ' If using Professional version, put your serial key below.
12        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
13
14        Dim document As DocumentModel = DocumentModel.Load("Reading.docx")
15
16        Dim numberOfSections As Integer = document.Sections.Count
17        Dim numberOfParagraphs As Integer = document.GetChildElements(True, ElementType.Paragraph).Count()
18        Dim numberOfRunsAndFields As Integer = document.GetChildElements(True, ElementType.Run, ElementType.Field).Count()
19        Dim numberOfInlines As Integer = document.GetChildElements(True).OfType(Of Inline)().Count()
20
21        Dim elements As Integer = document.Sections(0).GetChildElements(True).Count()
22
23        Dim sb As New StringBuilder()
24        sb.AppendLine("File has:")
25        sb.AppendLine(numberOfSections & " section")
26        sb.AppendLine(numberOfParagraphs & " paragraphs")
27        sb.AppendLine(numberOfRunsAndFields & " runs and fields")
28        sb.AppendLine(numberOfInlines & " inlines")
29        sb.AppendLine("First section contains " & elements & " elements")
30        Console.WriteLine(sb.ToString())
31
32    End Sub
33
34End Module

Check next sample or find out more about GemBox.Document and GemBox Software.