Iteration process

  1. Add SautinSoft.Document from Nuget.
  2. Load a DOCX document.
  3. Iterate through all sections, paragraphs, runs and fields.
This example shows how to calculate sections, paragraphs, inlines, runs and fields in DOCX document.

Complete code

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

namespace Sample
{
    class Sample
    {

        static void Main(string[] args)
        {
            // Get your free 100-day key here:   
            // https://sautinsoft.com/start-for-free/

            IterationElement();
        }

		/// <summary>
        /// Calculate sections, paragraphs, inlines, runs and fields in DOCX document.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/iteration-in-element-collection-net-csharp-vb.php
        /// </remarks>
        static void IterationElement()
        {
            DocumentCore dc = DocumentCore.Load(@"..\..\..\Parsing.docx", LoadOptions.DocxDefault);
            int numberOfSections = dc.Sections.Count;
            int numberOfParagraphs = dc.GetChildElements(true, ElementType.Paragraph).Count();
            int numberOfRunsAndFields = dc.GetChildElements(true, ElementType.Run, ElementType.Field).Count();
            int numberOfInlines = dc.GetChildElements(true).OfType<Inline>().Count();
            int elements = dc.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());
            Console.ReadKey();
        }
    }
}

Download

Imports System
Imports SautinSoft.Document
Imports System.IO
Imports System.Linq
Imports System.Text

Module Sample
    Sub Main()
        IterationElement()
    End Sub
    ''' Get your free 100-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' Calculate sections, paragraphs, inlines, runs and fields in DOCX document.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/iteration-in-element-collection-net-csharp-vb.php
    ''' </remarks>
    Sub IterationElement()
        Dim dc As DocumentCore = DocumentCore.Load("..\..\..\Parsing.docx", LoadOptions.DocxDefault)
        Dim numberOfSections As Integer = dc.Sections.Count
        Dim numberOfParagraphs As Integer = dc.GetChildElements(True, ElementType.Paragraph).Count()
        Dim numberOfRunsAndFields As Integer = dc.GetChildElements(True, ElementType.Run, ElementType.Field).Count()
        Dim numberOfInlines As Integer = dc.GetChildElements(True).OfType(Of Inline)().Count()
        Dim elements As Integer = dc.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())
        Console.ReadKey()
    End Sub
End Module

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.