Support Center

Find & Replace text with Picture or Table

GemBox.Document offers few ways how we can manipulate with our document content in order to import a new data in it.

One way we can do this is by using a Bookmarks inside our document. These Bookmarks will represent our placeholders in the document where we want to import new content. The following link demonstrates how we can achieve this:

Another way would be by using a MergeFields inside our document. These MergeFields can be replaces with a new data from a various .NET's data sources (list of supported mail merge data sources) through a GemBox.Document's mail merge process.
The following link contains a list of mail merge samples:

In the following sample we will search for our custom text tags which are placed inside our document and we will replace them with our new content.
I will start by loading a "Find & Replace - Input.docx" file, which has some custom tags defined inside of it (#IMAGE, #TABLE and #HTML):

Find & Replace - Input.docx

We will use a ContentRange.Find method to retrieve the document content that contains our custom tag. After that we will remove our tag and add our required content in its place.

C# code

DocumentModel doc = DocumentModel.Load("Find & Replace - Input.docx");

// Find #IMAGE text.
ContentRange imagePlaceholder = doc.Content.Find("#IMAGE").First();
// Remove text.
// Create and add new Picture element content.
imagePlaceholder.Start.InsertRange(new Picture(doc, "GS-Logo.png").Content);

// Find #TABLE text.
ContentRange tablePlaceholder = doc.Content.Find("#TABLE").First();
// Remove text.
// Create new Table element.
Table table = new Table(doc,
                    new TableRow(doc,
                        new TableCell(doc, new Paragraph(doc, "Name:")),
                        new TableCell(doc, new Paragraph(doc, "GemBox Software"))),
                    new TableRow(doc,
                        new TableCell(doc, new Paragraph(doc, "About:")),
                        new TableCell(doc, new Paragraph(doc, ".NET component vendor"))),
                    new TableRow(doc,
                        new TableCell(doc, new Paragraph(doc, "Website:")),
                        new TableCell(doc,
                            new Paragraph(doc,
                                new Hyperlink(doc, """")))));
table.TableFormat.Borders.SetBorders(MultipleBorderTypes.All, BorderStyle.Single, Color.Brown, 2);
table.TableFormat.PreferredWidth = new TableWidth(100, TableWidthUnit.Percentage);
// Add new Table element content.

// Find #HTML text.
ContentRange htmlPlaceholder = doc.Content.Find("#HTML").First();
// Remove text.
// Create HTML formated string.
string html = new StringBuilder()
                .Append("<p style='font-family:Times New Roman; font-size:20px;'>")
                        .Append("<font face='consolas' color='green'>GemBox.Document</font>")
                        .Append(" is a .NET component that enables developers to ")
                        .Append("<em>read, write, convert and print document files in a simple and efficient way</em>")
// Add new HTML textual content.
htmlPlaceholder.Start.LoadText(html, LoadOptions.HtmlDefault);

doc.Save("Find & Replace - Output.docx");

VB.NET code

Dim doc As DocumentModel = DocumentModel.Load("Find & Replace - Input.docx")

' Find #IMAGE text.
Dim imagePlaceholder As ContentRange = doc.Content.Find("#IMAGE").First()
' Remove text.
' Create and add new Picture element content.
imagePlaceholder.Start.InsertRange(New Picture(doc, "GS-Logo.png").Content)

' Find #TABLE text.
Dim tablePlaceholder As ContentRange = doc.Content.Find("#TABLE").First()
' Remove text.
' Create new Table element.
Dim table As New Table(doc, _
                New TableRow(doc, _
                    New TableCell(doc, New Paragraph(doc, "Name:")), _
                    New TableCell(doc, New Paragraph(doc, "GemBox Software"))), _
                New TableRow(doc, _
                    New TableCell(doc, New Paragraph(doc, "About:")), _
                    New TableCell(doc, New Paragraph(doc, ".NET component vendor"))), _
                New TableRow(doc, _
                    New TableCell(doc, New Paragraph(doc, "Website:")),
                    New TableCell(doc, _
                        New Paragraph(doc, _
                            New Hyperlink(doc, """")))))
table.TableFormat.Borders.SetBorders(MultipleBorderTypes.All, BorderStyle.[Single], Color.Brown, 2)
table.TableFormat.PreferredWidth = New TableWidth(100, TableWidthUnit.Percentage)
' Add new Table element content.

' Find #HTML text.
Dim htmlPlaceholder As ContentRange = doc.Content.Find("#HTML").First()
' Remove text.
' Create HTML formated string.
Dim html As String = New StringBuilder() _
                        .Append("<h1>GEMBOX.DOCUMENT</h1>") _
                        .Append("<p style='font-family:Times New Roman; font-size:20px;'>") _
                            .Append("<font face='consolas' color='green'>GemBox.Document</font>") _
                            .Append(" is a .NET component that enables developers to ") _
                            .Append("<em>read, write, convert and print document files in a simple and efficient way</em>") _
' Add new HTML textual content.
htmlPlaceholder.Start.LoadText(html, LoadOptions.HtmlDefault)

doc.Save("Find & Replace - Output.docx")

The following is a resulting "Find & Replace - Output.docx" file:

Find & Replace - Output.docx

8 of 9 people found this page helpful
Subscribe to this article to get an email notification when it is updated.


  • There are no comments.