Skip to main content
All docs
V24.1

How to: Replace a Placeholder with a Document Element

  • 3 minutes to read

The following example describes how to insert a placeholder and replace it with the document element (table, image or text). You can use one the following elements as placeholders:

  • Keywords
  • Bookmarks
  • Document fields
  • Pictures (to replace with another pictures)

Search and Replace Keywords

Replace Keyword with Plain Text

The SubDocument.Replace method allows you to replace the specified text. The code sample below replaces the “PARTNER” keyword with an actual name:

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;

using (var wordProcessor = new RichEditDocumentServer())
{
    Document document = wordProcessor.Document;
    document.LoadDocument(@"C:\Docs\Doc1.docx");
    document.ReplaceAll("PARTNER", "Nancy Davolio", SearchOptions.WholeWord);
    wordProcessor.SaveDocument("updated.docx", DocumentFormat.OpenXml);
}

Replace Keyword with Formatted Content

Call the SubDocument.FindAll method to search all keyword entries. The method returns an array of DocumentRange objects. Use the DocumentRange.Start property to retrieve the start position of the required entry. The SubDocument.Delete method allows you to remove the keyword. You can use the following methods to insert text at the keyword’s position:

The code sample below searches for the “INSERT HERE” keyword and replaces it with content from another document:

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;


using (var wordProcessor = new RichEditDocumentServer())
{
    Document document = wordProcessor.Document;
    document.LoadDocument(@"C:\Docs\Doc1.docx");
    var keywords = document.FindAll("INSERT HERE",SearchOptions.WholeWord);
    DocumentPosition insertPosition = keywords[0].Start;
    document.Delete(keywords[0]);
    document.InsertDocumentContent(insertPosition, @"C:\Docs\Word (RTF) Document API for NET.docx");
    wordProcessor.SaveDocument("updated.docx", DocumentFormat.OpenXml);
}

Replace a Bookmark with Text

Call the BookmarkCollection.Create method to insert a bookmark at the specified position in a document. The BookmarkCollection.Item property allows you to retrieve a bookmark by its name. Use the Bookmark.Range property to get the start position of the bookmark’s range. Call the BookmarkCollection.Remove method to remove the bookmark. To insert text at the bookmark’s position, use one of the methods mentioned above.

The code sample below shows how to replace a bookmark with rich-formatted text:

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;

using (var wordProcessor = new RichEditDocumentServer())
{
    wordProcessor.LoadDocument(@"C:\\Docs\\Doc_bookmark.docx");
    // Find the bookmark by its name
    Bookmark bookmark = wordProcessor.Document.Bookmarks["replace"];

    // Retrieve the bookmark's start position
    DocumentPosition position = bookmark.Range.Start;

    // Remove the bookmark
    wordProcessor.Document.Bookmarks.Remove(bookmark);

    // Specify the RTF string to insert
    string rtfString = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1049
{\fonttbl{\f0\fswiss\fprq2\fcharset0 Arial;}
{\f1\fswiss\fcharset0 Arial;}}
{\colortbl ;\red0\green0\blue255;}
\viewkind4\uc1\pard\cf1\lang1033\b\f0\fs32 Test.\cf0\b0\f1\fs20\par}";

    // Insert text at the retrieved position
    wordProcessor.Document.InsertRtfText(position, rtfString);

    // Save the resulting document
    wordProcessor.SaveDocument("bookmark-replaced.docx", DocumentFormat.OpenXml);
}

Replace a Field with a Document Element

Use DOCVARIABLE, MERGEFIELD or DOCPROPERTY fields to insert formatted content or plain text. Refer to the following articles for more information:

Replace an Image with Another Image

You can use the PictureFormat.SetPicture method to replace an image with another image. Refer to the following article for more information: How to: Replace a Picture with Another Picture