Manipulation with Element(s)

  1. Add SautinSoft.Document from Nuget.
  2. Create a new document.
  3. Add paragraphs as content and as element.
  4. Save the document.

Complete code

using SautinSoft.Document;
using SautinSoft.Document.Drawing;
using SautinSoft.Document.Tables;
using System.Linq;

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

            ElementManipulation();
        }
        /// <summary>
        /// Create a document and add paragraphs as content and as element.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/element-manipulation.php
        /// </remarks>
        static void ElementManipulation()
        {
            string filePath = @"Result.docx";

            // Let's create a new document.
            DocumentCore dc = new DocumentCore();
            Paragraph par = new Paragraph(dc, "This is the first paragraph.");

            // Insert the clone of our Paragraph using ContentRange.
            dc.Content.End.Insert(par.Content);

            // Add our Paragraph in Block collection as Element.
            dc.Sections[0].Blocks.Add(par);

            // Again, insert the clone of our Paragraph using ContentRange.
            dc.Content.End.Insert(par.Content);

            // Change text in our Paragraph
            (par.Inlines[0] as Run).Text = "Now we are in the second paragraph.";

            // Find 3rd paragraph and change text in it.
            ((par.NextSibling as Paragraph).Inlines[0] as Run).Text = "This is the third paragraph.";

            // Save our document.
            dc.Save(filePath);

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(filePath) { UseShellExecute = true });
        }
    }
}

Download

Imports SautinSoft.Document
Imports SautinSoft.Document.Drawing
Imports SautinSoft.Document.Tables
Imports System.Linq

Module Sample
    Sub Main()
        ElementManipulation()
    End Sub
    ''' Get your free 100-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' Create a document and add paragraphs as content and as element.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/element-manipulation.php
    ''' </remarks>
    Sub ElementManipulation()
        Dim filePath As String = "Result.docx"

        ' Let's create a new document.
        Dim dc As New DocumentCore()
        Dim par As New Paragraph(dc, "This is the first paragraph.")

        ' Insert the clone of our Paragraph using ContentRange.
        dc.Content.End.Insert(par.Content)

        ' Add our Paragraph in Block collection as Element.
        dc.Sections(0).Blocks.Add(par)

        ' Again, insert the clone of our Paragraph using ContentRange.
        dc.Content.End.Insert(par.Content)

        ' Change text in our Paragraph
        TryCast(par.Inlines(0), Run).Text = "Now we are in the second paragraph."

        ' Find 3rd paragraph and change text in it.
        TryCast((TryCast(par.NextSibling, Paragraph)).Inlines(0), Run).Text = "This is the third paragraph."

        ' Save our document.
        dc.Save(filePath)

        ' Open the result for demonstration purposes.
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(filePath) 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.