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.


1using System;
2using System.Text;
3using System.Linq;
4using GemBox.Document;
5
6class Sample
7{
8    [STAThread]
9    static void Main(string[] args)
10    {
11        // If using Professional version, put your serial key below.
12        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
13
14        DocumentModel document = new DocumentModel();
15
16        // Set the content for the whole document
17        document.Content.LoadText("Paragraph 1\nParagraph 2\nParagraph 3\nParagraph 4\nParagraph 5");
18
19        var bold = new CharacterFormat()
20        { 
21            Bold = true
22        };
23
24        // Set the content for the 2nd paragraph
25        document.Sections[0].Blocks[1].Content.LoadText("Bold paragraph 2", bold);
26        
27        // Set the content for 3rd and 4th paragraph to be the same as the content of 1st and 2nd paragraph
28        var para3 = document.Sections[0].Blocks[2];
29        var para4 = document.Sections[0].Blocks[3];            
30        var destinationRange = new ContentRange(para3.Content.Start, para4.Content.End);
31        var para1 = document.Sections[0].Blocks[0];
32        var para2 = document.Sections[0].Blocks[1];
33        var sourceRange = new ContentRange(para1.Content.Start, para2.Content.End);
34        destinationRange.Set(sourceRange);
35
36        // Set content using HTML tags
37        document.Sections[0].Blocks[4].Content.LoadText("Paragraph 5 <b>(part of this paragraph is bold)</b>", LoadOptions.HtmlDefault);
38
39        document.Save("Set Content.docx");
40    }
41}
1Imports System
2Imports System.Text
3Imports System.Linq
4Imports GemBox.Document
5
6Module Samples
7
8    Sub Main()
9
10        ' If using Professional version, put your serial key below.
11        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
12
13        Dim document As DocumentModel = New DocumentModel
14
15        ' Set the content for the whole document
16        document.Content.LoadText("Paragraph 1" & vbLf & "Paragraph 2" & vbLf & "Paragraph 3" & vbLf & "Paragraph 4" & vbLf & "Paragraph 5")
17
18        Dim bold = New CharacterFormat() With {
19            .Bold = True
20        }
21
22        ' Set the content for the 2nd paragraph
23        document.Sections(0).Blocks(1).Content.LoadText("Bold paragraph 2", bold)
24
25        ' Set the content for 3rd and 4th paragraph to be the same as the content of 1st and 2nd paragraph
26        Dim para3 = document.Sections(0).Blocks(2)
27        Dim para4 = document.Sections(0).Blocks(3)
28        Dim destinationRange = New ContentRange(para3.Content.Start, para4.Content.End)
29        Dim para1 = document.Sections(0).Blocks(0)
30        Dim para2 = document.Sections(0).Blocks(1)
31        Dim sourceRange = New ContentRange(para1.Content.Start, para2.Content.End)
32        destinationRange.Set(sourceRange)
33
34        ' Set content using HTML tags
35        document.Sections(0).Blocks(4).Content.LoadText("Paragraph 5 <b>(part of this paragraph is bold)</b>", LoadOptions.HtmlDefault)
36
37        document.Save("Set Content.docx")
38
39    End Sub
40
41End Module

Check next sample or find out more about GemBox.Document and GemBox Software.