Insert HTML and RTF content into a Word file in C# and VB.NET
GemBox.Document enables you to easily insert new content at any position in the document using C# and VB.NET code.
The inserted content can be plain text with specified optional formatting or rich formatted text like HTML and RTF. You can insert the text content using one of the ContentPosition.LoadText
methods.
You can also insert arbitrary document content using the ContentPosition.InsertRange
method.
The following example shows how you can insert text content (plain and rich) and more complex content (single or multiple document elements) at a specific document position.

using GemBox.Document;
class Program
{
static void Main()
{
// If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
var document = new DocumentModel();
// Create the whole document using fluent API.
document.Content.Start
.LoadText("First paragraph.")
.InsertRange(new Paragraph(document, "Second paragraph.").Content)
.LoadText("\n")
.LoadText("Paragraph with bold text.", new CharacterFormat() { Bold = true });
var section = document.Sections[0];
// Prepend text to second paragraph.
section.Blocks[1].Content.Start.LoadText(" Some Prefix ", new CharacterFormat() { Subscript = true });
// Append text to second paragraph.
section.Blocks[1].Content.End.LoadText(" Some Suffix ", new CharacterFormat() { Superscript = true });
// Insert HTML paragraph before third paragraph.
section.Blocks[2].Content.Start.LoadText("<p style='font:italic 11pt Calibri;color:royalblue;'>Paragraph from HTML content with blue and italic text.</p>",
new HtmlLoadOptions());
// Insert RTF paragraph after fourth paragraph.
section.Blocks[3].Content.End.LoadText(@"{\rtf1\ansi\deff0{\colortbl ;\red255\green128\blue64;}\cf1 Paragraph from RTF content with orange text.\par\par}",
new RtfLoadOptions());
document.Save("Insert Content.%OutputFileType%");
}
}
Imports GemBox.Document
Module Program
Sub Main()
' If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY")
Dim document As New DocumentModel()
' Create the whole document using fluent API.
document.Content.Start _
.LoadText("First paragraph.") _
.InsertRange(New Paragraph(document, "Second paragraph.").Content) _
.LoadText(vbLf) _
.LoadText("Paragraph with bold text.", New CharacterFormat() With {.Bold = True})
Dim section = document.Sections(0)
' Prepend text to second paragraph.
section.Blocks(1).Content.Start.LoadText(" Some Prefix ", New CharacterFormat() With {.Subscript = True})
' Append text to second paragraph.
section.Blocks(1).Content.End.LoadText(" Some Suffix ", New CharacterFormat() With {.Superscript = True})
' Insert HTML paragraph before third paragraph.
section.Blocks(2).Content.Start.LoadText("<p style='font:italic 11pt Calibri;color:royalblue;'>Paragraph from HTML content with blue and italic text.</p>",
New HtmlLoadOptions())
' Insert RTF paragraph after fourth paragraph.
section.Blocks(3).Content.End.LoadText("{\rtf1\ansi\deff0{\colortbl ;\red255\green128\blue64;}\cf1 Paragraph from RTF content with orange text.\par\par}",
New RtfLoadOptions())
document.Save("Insert Content.%OutputFileType%")
End Sub
End Module
See also
Next steps
Published: December 13, 2018 | Modified: December 19, 2022 | Author: Damir Stipinovic