ElementCollection: Add and Delete


Complete code

using SautinSoft.Document;

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

            AddAndDeleteParagraphs();
        }
        /// <summary>
        /// ElementCollection: Adds 20 paragraphs into document and delete 10 of them.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/elementcollection-add-delete.php
        /// </remarks>
        static void AddAndDeleteParagraphs()
        {
            DocumentCore dc = new DocumentCore();
            Section section = new Section(dc);
            dc.Sections.Add(section);
            for (int i = 0; i < 20; i++)
            {
                Paragraph par = new Paragraph(dc,"Text "+  i.ToString());
                section.Blocks.Add(par);
            }
            dc.Save("ResultFull.docx");
            for (int i = 0; i < section.Blocks.Count; )
            {
                section.Blocks.RemoveAt(i);
                i++;
            }
            dc.Save("ResultShort.docx");
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("ResultFull.docx") { UseShellExecute = true });
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("ResultShort.docx") { UseShellExecute = true });
        }
    }
}

Download

Imports System
Imports System.IO
Imports SautinSoft.Document

Module Sample
    Sub Main()
        AddAndDeleteParagraphs()
    End Sub
    ''' Get your free 30-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' ElementCollection: Adds 20 paragraphs into document and delete 10 of them.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/elementcollection-add-delete.php
    ''' </remarks>
    Sub AddAndDeleteParagraphs()
        Dim dc As New DocumentCore()
        Dim section As New Section(dc)
        dc.Sections.Add(section)
        For i As Integer = 0 To 19
            Dim par As New Paragraph(dc, "Text " & i.ToString())
            section.Blocks.Add(par)
        Next i
        dc.Save("ResultFull.docx")
        Dim j As Integer = 0
        Do While j < section.Blocks.Count
            section.Blocks.RemoveAt(j)
            j += 1
        Loop
        dc.Save("ResultShort.docx")
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("ResultFull.docx") With {.UseShellExecute = True})
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("ResultShort.docx") 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.