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.

Insert HTML and RTF content to Word file 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.

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();

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

        // Create the whole document using streamlike API.
        document.Content.Start.
            LoadText("First paragraph.").
            InsertRange(new Paragraph(document, "Second paragraph.").Content).
            LoadText("\nThird bold paragraph.", bold);

        // Prepend text to second paragraph.
        document.Sections[0].Blocks[1].Content.Start.LoadText("Some prefix (");

        // Append text to second paragraph.
        document.Sections[0].Blocks[1].Content.End.LoadText(") some suffix.");

        // Append text formatted using HTML tags.
        document.Sections[0].Blocks[2].Content.End.LoadText("<p>Fourth paragraph is added as <b>HTML</b> content.</p>", LoadOptions.HtmlDefault);

        document.Save("Insert 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

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

        ' Create the whole document using streamlike API.
        document.Content.Start.
            LoadText("First paragraph.").
            InsertRange(New Paragraph(document, "Second paragraph.").Content).
            LoadText(vbLf & "Third bold paragraph.", bold)

        ' Prepend text to second paragraph.
        document.Sections(0).Blocks(1).Content.Start.LoadText("Some prefix (")

        ' Append text to second paragraph.
        document.Sections(0).Blocks(1).Content.End.LoadText(") some suffix.")

        ' Append text formatted using HTML tags.
        document.Sections(0).Blocks(2).Content.End.LoadText("<p>Fourth paragraph is added as <b>HTML</b> content.</p>", LoadOptions.HtmlDefault)

        document.Save("Insert Content.docx")

    End Sub

End Module

Check next sample.