Click or drag to resize

ContentPositionInsert(String, CharacterFormat) Method

Inserts the specified text with specific formatting at the current ContentPosition.

Namespace: SautinSoft.Document
Assembly: SautinSoft.Document (in SautinSoft.Document.dll) Version: 2024.10.24
Syntax
public ContentPosition Insert(
	string text,
	CharacterFormat format
)

Parameters

text  String
The text which should be inserted at the current ContentPosition.
format  CharacterFormat
The formatting of the text.

Return Value

ContentPosition
A ContentPosition instance which points to a location just after the inserted text.
Example

See Developer Guide: Create a new document with some content

Creates a new document with some content using C#
using System.Linq;
using SautinSoft.Document;

namespace Sample
{
    class Sample
    {

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

            InsertContent();
        }

        /// <summary>
        /// Create a new document with some content.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/insert-content-net-csharp-vb.php
        /// </remarks>
        public static void InsertContent()
        {
            string documentPath = @"InsertContent.pdf";

            // Let's create a new document.
            DocumentCore dc = new DocumentCore();

            // Add new section.
            Section section = new Section(dc);
            dc.Sections.Add(section);

            // You may add a new paragraph using a classic way:
            section.Blocks.Add(new Paragraph(dc, "This is the first line in 1st paragraph!"));
            Paragraph par1 = section.Blocks[0] as Paragraph;
            par1.ParagraphFormat.Alignment = HorizontalAlignment.Center;

            // But here, let's see how to do it using ContentRange:

            // 1. Insert a content as Text.
            dc.Content.End.Insert("\nThis is a first line in 2nd paragraph.", new CharacterFormat() { Size = 25, FontColor = Color.Blue, Bold = true });

            // 2. Insert a content as HTML (at the start).
            dc.Content.Start.Insert("Hello from HTML!", new HtmlLoadOptions());

            // 3. Insert a content as RTF.
            dc.Content.End.Insert(@"{\rtf1 \b The line from RTF\b0!\par}", new RtfLoadOptions());

            // 4. Insert a content of SpecialCharacter.
            dc.Content.End.Insert(new SpecialCharacter(dc, SpecialCharacterType.LineBreak).Content);

            // 5. Insert a content as ContentRange.
            // Find first content of "HTML" and insert it at the end.
            ContentRange cr = dc.Content.Find("HTML").First();
            dc.Content.End.Insert(cr);

            // 6. Insert the content of Paragraph at the end.
            Paragraph p = new Paragraph(dc);
            Run run = new Run(dc, "As summarize, you can insert content of any class " +
                "derived from Element: Table, Shape, Paragraph, Run, HeaderFooter and even a whole DocumentCore!");
            run.CharacterFormat.FontColor = Color.DarkGreen;
            p.Inlines.Add(run);
            dc.Content.End.Insert(p.Content);

            // Save our document into PDF format.
            dc.Save(documentPath, new PdfSaveOptions() { Compliance = PdfCompliance.PDF_A1a });

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(documentPath) { UseShellExecute = true });
        }
    }
}
Creates a new document with some content using VB.Net
Imports System
Imports System.IO
Imports System.Linq
Imports SautinSoft.Document

Module Sample
    Sub Main()
        InsertContent()
    End Sub
    ''' Get your free 100-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' Creates a new document with some content.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/insert-content-net-csharp-vb.php
    ''' </remarks>
    Sub InsertContent()
        Dim documentPath As String = "InsertContent.pdf"

        ' Let's create a new document.
        Dim dc As New DocumentCore()

        ' Add new section.
        Dim section As New Section(dc)
        dc.Sections.Add(section)

        ' You may add a new paragraph using a classic way:
        section.Blocks.Add(New Paragraph(dc, "This is a first line in 1st paragraph!"))
        Dim par1 As Paragraph = TryCast(section.Blocks(0), Paragraph)
        par1.ParagraphFormat.Alignment = HorizontalAlignment.Center

        ' But here, let's see how to do it using ContentRange:

        ' 1. Insert a content as Text.
        dc.Content.End.Insert(vbLf & "This is the first line in 2nd paragraph.", New CharacterFormat() With {
            .Size = 25,
            .FontColor = Color.Blue,
            .Bold = True
        })

        ' 2. Insert a content as HTML (at the start).
        dc.Content.Start.Insert("Hello from HTML!", New HtmlLoadOptions())

        ' 3. Insert a content as RTF.
        dc.Content.End.Insert("{\rtf1 \b The line from RTF\b0!\par}", New RtfLoadOptions())

        ' 4. Insert a content of SpecialCharacter.
        dc.Content.End.Insert((New SpecialCharacter(dc, SpecialCharacterType.LineBreak)).Content)

        ' 5. Insert a content as ContentRange.
        ' Find first content of "HTML" and insert it at the end.
        Dim cr As ContentRange = dc.Content.Find("HTML").First()
        dc.Content.End.Insert(cr)

        ' 6. Insert the content of Paragraph at the end.
        Dim p As New Paragraph(dc)
        Dim run As New Run(dc, "As summarize, you can insert content of any class " & "derived from Element: Table, Shape, Paragraph, Run, HeaderFooter and even a whole DocumentCore!")
        run.CharacterFormat.FontColor = Color.DarkGreen
        p.Inlines.Add(run)
        dc.Content.End.Insert(p.Content)

        ' Save our document into PDF format.
        dc.Save(documentPath, New PdfSaveOptions() With {.Compliance = PdfCompliance.PDF_A1a})

        ' Open the result for demonstration purposes.
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(documentPath) With {.UseShellExecute = True})
    End Sub
End Module
See Also