Итерация в ElementCollection с помощью C# и .NET


Полный код

using System;
using SautinSoft.Document;

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

            ShowInlines();
        }
        /// <summary>
        /// Iterates through a document and count the amount of Paragraphs and Runs.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/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
    ''' Get your free 100-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' Iterates through a document and count the amount of Paragraphs and Runs.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/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)
                    Console.ReadKey()
                End If
            Next blocks
        Next sect
    End Sub
End Module

Download


Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу support@sautinsoft.com или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже:



Вопросы и предложения всегда приветствуются!

Мы разрабатываем компоненты .Net с 2002 года. Мы знаем форматы PDF, DOCX, RTF, HTML, XLSX и Images. Если вам нужна помощь в создании, изменении или преобразовании документов в различных форматах, мы можем вам помочь. Мы напишем для вас любой пример кода абсолютно бесплатно.