Document .Net - Iteration in ElementCollection

Iteration in ElementCollection

Complete code

using System;
using SautinSoft.Document;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            ShowInlines();
        }
        /// <summary>
        /// Iterates through a document and count the amount of Paragraphs and Runs.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/examples/elementcollection-iteration.php
        /// </remarks>
        static void ShowInlines()
        {
            string filePath = @"..\..\example.docx";
            DocumentCore dc = DocumentCore.Load(filePath);
            Console.WriteLine("This document contains from:");
            for (int sect = 0; sect < dc.Sections.Count; sect++)
            {
                Console.WriteLine("Section {0} contains from:", sect);
                int totalParagraphs = 0;
                Section section = dc.Sections[sect];
                for (int blocks = 0; blocks < section.Blocks.Count; blocks++)
                {
                    if (section.Blocks[blocks] is Paragraph)
                    {
                        totalParagraphs++;
                        Paragraph paragraph = section.Blocks[blocks] as Paragraph;
                        Console.Write("\t\t Paragraph {0} contains from: ", totalParagraphs);
                        int totalRuns = 0;
                        for (int i = 0; i < paragraph.Inlines.Count; i++)
                        {
                            if (paragraph.Inlines[i] is Run)
                                totalRuns++;
                        }
                        Console.WriteLine("{0} Run(s).", totalRuns);
                    }
                }
            }
            Console.ReadKey();
        }
    }
}

Download.

        
            Imports System
Imports System.IO
Imports SautinSoft.Document

Module Sample
    Sub Main()
        ShowInlines()
    End Sub

    ''' <summary>
    ''' Iterates through a document and count the amount of Paragraphs and Runs.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/examples/elementcollection-iteration.php
    ''' </remarks>
    Sub ShowInlines()
        Dim filePath As String = "..\example.docx"
        Dim dc As DocumentCore = DocumentCore.Load(filePath)
        Console.WriteLine("This document contains from:")
        For sect As Integer = 0 To dc.Sections.Count - 1
            Console.WriteLine("Section {0} contains from:", sect)
            Dim totalParagraphs As Integer = 0
            Dim section As Section = dc.Sections(sect)
            For blocks As Integer = 0 To section.Blocks.Count - 1
                If TypeOf section.Blocks(blocks) Is Paragraph Then
                    totalParagraphs += 1
                    Dim paragraph As Paragraph = TryCast(section.Blocks(blocks), Paragraph)
                    Console.Write(vbTab & vbTab & " Paragraph {0} contains from: ", totalParagraphs)
                    Dim totalRuns As Integer = 0
                    For i As Integer = 0 To paragraph.Inlines.Count - 1
                        If TypeOf paragraph.Inlines(i) Is Run Then
                            totalRuns += 1
                        End If
                    Next i
                    Console.WriteLine("{0} Run(s).", totalRuns)
                End If
            Next blocks
        Next sect
    End Sub
End Module
            

Download.

© SautinSoft 2002 - 2019