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.

using GemBox.Document;
class Program
{
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.%OutputFileType%");
}
}
Imports GemBox.Document
Module Program
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.%OutputFileType%")
End Sub
End Module
Check next example or download examples from GitHub.