Insert HTML and RTF to Word in C# and VB.NET

GemBox.Document enables you to insert arbitrary complex content to any position in the document. Inserted content can be text with optional formatting, HTML text or any content which can span multiple sections, paragraphs, tables or any other elements.

Following example shows how to generate a document by inserting plain text, HTML text and more complex content (paragraph) and how to insert content at the specific document position.

Screenshot

Insert 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        var bold = new CharacterFormat() { Bold = true };
17
18        // Create the whole document using streamlike API.
19        document.Content.Start.
20            LoadText("First paragraph.").
21            InsertRange(new Paragraph(document, "Second paragraph.").Content).
22            LoadText("\nThird bold paragraph.", bold);
23
24        // Prepend text to second paragraph.
25        document.Sections[0].Blocks[1].Content.Start.LoadText("Some prefix (");
26
27        // Append text to second paragraph.
28        document.Sections[0].Blocks[1].Content.End.LoadText(") some suffix.");
29
30        // Append text formatted using HTML tags.
31        document.Sections[0].Blocks[2].Content.End.LoadText("<p>Fourth paragraph is added as <b>HTML</b> content.</p>", LoadOptions.HtmlDefault);
32
33        document.Save("Insert Content.docx");
34    }
35}
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        Dim bold = New CharacterFormat() With {.Bold = True}
16
17        ' Create the whole document using streamlike API.
18        document.Content.Start.
19            LoadText("First paragraph.").
20            InsertRange(New Paragraph(document, "Second paragraph.").Content).
21            LoadText(vbLf & "Third bold paragraph.", bold)
22
23        ' Prepend text to second paragraph.
24        document.Sections(0).Blocks(1).Content.Start.LoadText("Some prefix (")
25
26        ' Append text to second paragraph.
27        document.Sections(0).Blocks(1).Content.End.LoadText(") some suffix.")
28
29        ' Append text formatted using HTML tags.
30        document.Sections(0).Blocks(2).Content.End.LoadText("<p>Fourth paragraph is added as <b>HTML</b> content.</p>", LoadOptions.HtmlDefault)
31
32        document.Save("Insert Content.docx")
33
34    End Sub
35
36End Module

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