Manipulation with ElementCollection

  1. Add SautinSoft.Document from Nuget.
  2. Load a DOCX document.
  3. Find a first paragraph.
  4. Iterate through all inlines.
  5. Insert the each inline to a new paragraph.

This code example shows us how to separate Runs and insert each Run into a new Paragraph.

Complete code

using System.IO;
using System.Linq;
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/

            Manipulation();
        }
        /// <summary>
        /// Manipulation with ElementCollection. Split 1st Paragraph by separate Runs and insert each Run into a new Paragraph.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/elementcollection-manipulation.php
        /// </remarks>
        static void Manipulation()
        {
            string filePath = @"..\..\..\example.docx";
            DocumentCore dc = DocumentCore.Load(filePath);
            string filePathResult = @"Result-file.pdf";
            Section section = dc.Sections[0];
            Paragraph paragraph = section.Blocks[0] as Paragraph;
            for (int i = 1; i < paragraph.Inlines.Count ; )
            {
                Inline inline = paragraph.Inlines[i];
                paragraph.Inlines.RemoveAt(1);
                section.Blocks.Add(new Paragraph(dc, inline));
            }
            dc.Save(filePathResult);
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(filePath) { UseShellExecute = true });
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(filePathResult) { UseShellExecute = true });
        }
    }
}

Download

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

Module Sample
    Sub Main()
        Manipulation()
    End Sub
    ''' Get your free 100-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' Manipulation with ElementCollection. Split 1st Paragraph by separate Runs and insert each Run into a new Paragraph.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/elementcollection-manipulation.php
    ''' </remarks>
    Sub Manipulation()
        Dim filePath As String = "..\..\..\example.docx"
        Dim dc As DocumentCore = DocumentCore.Load(filePath)
        Dim filePathResult As String = "Result-file.pdf"
        Dim section As Section = dc.Sections(0)
        Dim paragraph As Paragraph = TryCast(section.Blocks(0), Paragraph)
        Dim i As Integer = 1
        Do While i < paragraph.Inlines.Count
            Dim inline As Inline = paragraph.Inlines(i)
            paragraph.Inlines.RemoveAt(1)
            section.Blocks.Add(New Paragraph(dc, inline))
        Loop
        dc.Save(filePathResult)
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(filePath) With {.UseShellExecute = True})
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(filePathResult) With {.UseShellExecute = True})
    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.