GemBox.Document is a .NET component that enables you to read, write, edit, convert, and print document files from your .NET applications using one simple API.

Set Content

GemBox.Document allows you to easily set content of any part of the document, either by setting new textual content with optional formatting or HTML textual content or by setting a more complex content specified with ContentRange parameter of a ContentRange.Set method.

Following example demonstrates how to set textual content with and without formatting, by using HTML formatting and set a more complex content (two paragraphs) to a part of document content.

Screenshot
Set Content Screenshot

See the full code below, use Run Example to execute.

using System;
using System.Text;
using System.Linq;
using GemBox.Document;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        DocumentModel document = new DocumentModel();

        // Set the content for the whole document
        document.Content.LoadText("Paragraph 1\nParagraph 2\nParagraph 3\nParagraph 4\nParagraph 5");

        var bold = new CharacterFormat()
        { 
            Bold = true
        };

        // Set the content for the 2nd paragraph
        document.Sections[0].Blocks[1].Content.LoadText("Bold paragraph 2", bold);
        
        // Set the content for 3rd and 4th paragraph to be the same as the content of 1st and 2nd paragraph
        var para3 = document.Sections[0].Blocks[2];
        var para4 = document.Sections[0].Blocks[3];            
        var destinationRange = new ContentRange(para3.Content.Start, para4.Content.End);
        var para1 = document.Sections[0].Blocks[0];
        var para2 = document.Sections[0].Blocks[1];
        var sourceRange = new ContentRange(para1.Content.Start, para2.Content.End);
        destinationRange.Set(sourceRange);

        // Set content using HTML tags
        document.Sections[0].Blocks[4].Content.LoadText("Paragraph 5 <b>(part of this paragraph is bold)</b>", LoadOptions.HtmlDefault);

        document.Save("Set Content.docx");
    }
}
Imports System
Imports System.Text
Imports System.Linq
Imports GemBox.Document

Module Samples

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document As DocumentModel = New DocumentModel

        ' Set the content for the whole document
        document.Content.LoadText("Paragraph 1" & vbLf & "Paragraph 2" & vbLf & "Paragraph 3" & vbLf & "Paragraph 4" & vbLf & "Paragraph 5")

        Dim bold = New CharacterFormat() With {
            .Bold = True
        }

        ' Set the content for the 2nd paragraph
        document.Sections(0).Blocks(1).Content.LoadText("Bold paragraph 2", bold)

        ' Set the content for 3rd and 4th paragraph to be the same as the content of 1st and 2nd paragraph
        Dim para3 = document.Sections(0).Blocks(2)
        Dim para4 = document.Sections(0).Blocks(3)
        Dim destinationRange = New ContentRange(para3.Content.Start, para4.Content.End)
        Dim para1 = document.Sections(0).Blocks(0)
        Dim para2 = document.Sections(0).Blocks(1)
        Dim sourceRange = New ContentRange(para1.Content.Start, para2.Content.End)
        destinationRange.Set(sourceRange)

        ' Set content using HTML tags
        document.Sections(0).Blocks(4).Content.LoadText("Paragraph 5 <b>(part of this paragraph is bold)</b>", LoadOptions.HtmlDefault)

        document.Save("Set Content.docx")

    End Sub

End Module

Check next sample.