Skip to main content

Rich Edit Control: Document Management

  • 4 minutes to read

The Rich Edit control allows you to create, load, and save documents in the following formats:

  • Office OpenXML Document (DOCX)
  • Rich Text Format (RTF)
  • Microsoft Word® Binary Document Format (DOC)
  • HyperText Markup Language (HTML)
  • Plain text (TXT)

HTML Support Limitations

The Rich Edit control has no HTML editor functionality because it does not work with HTML tags directly. HTML import routines interpret tags in a source document and build an internal document model according to the current import settings.

HTML import and export routines support only a limited subset of the modern HTML standard; therefore, imported and exported documents may look different compared to corresponding source documents.

Manage Supported Document Formats

The Rich Edit control supports only RTF and TXT formats out-of-the-box to reduce the size of an application.

Add Support for a Format in Code

In Delphi

Add a document format unit to the uses clause of the module with the control to add support for this format. Add its package to the runtime package list if you build projects with runtime packages.

The following table lists document formats and their corresponding units and packages:

Format Uses Clause Runtime Package
OpenXml dxRichEdit.OpenXML dxRichEditControlOpenXMLFormatRS~
Doc dxRichEdit.Doc dxRichEditControlDocFormatRS~
Html dxRichEdit.Html dxRichEditControlHtmlFormatRS~

In C++Builder

Follow the steps below to add support for a specific document format:

  1. Insert a format header as an #include directive into the header file with the Document Server component in your project.
  2. Add a static library with the required format implementation as a #pragma link directive to the corresponding source file.
  3. Add the format’s LIB file to the AllPackageLibs list in project settings. You can find all LIB files in the VCL/Library folder of your DevExpress products installation.

The following table lists document formats and their corresponding directives and libraries you need to add in a C++Builder project:

Format Header File Source Code File AllPackageLibs List
OpenXml #include "dxRichEdit.OpenXML.hpp" #pragma link "dxRichEdit.OpenXML" dxRichEditControlOpenXMLFormatRS~.lib
Doc #include "dxRichEdit.Doc.hpp" #pragma link "dxRichEdit.Doc.obj" (for a 32-bit application) or #pragma link "dxRichEdit.Doc.o" (for a 64-bit application) dxRichEditControlDocFormatRS~.lib
Html #include "dxRichEdit.Html.hpp" #pragma link "dxRichEdit.Html" dxRichEditControlHtmlFormatRS~.lib

Add Support for a Format at Design Time

Right-click a Rich Edit control to invoke its context menu, select Add Unit to Support, and check all required document formats. Save the project to add units and package names of the checked formats to the uses clause and runtime package list in Delphi, respectively. If you use C++Builder, save the project to add #include and #pragma directives of checked formats to header and source code files. The corresponding library names are added to the project’s AllPackageLibs list automatically.

VCL Rich Edit: Design-Time Format Management

Create a New Document

The control displays an empty document at application startup. You can call the control’s CreateNewDocument function or the Document.CreateNewDocument procedure to replace the current content with a new empty document. A user can execute the NewDocument command to do the same if the control’s Options.Behavior.CreateNew property is set to Default or Enabled.

The CreateNewDocument function, the Document.CreateNewDocument procedure, and the NewDocument command raise the OnDocumentClosing event. You can handle it to display a warning message or save pending user changes when the current document is about to close.

Load Documents

The control has LoadDocument and Document.LoadDocument procedures that allow you to load a document from a file or stream. A user can execute the LoadDocument command to invoke the Open dialog and load a document file if the control’s Options.Behavior.Open property is set to Default or Enabled.

Load Encrypted Documents

If the control loads an encrypted DOCX file, a user should enter a password:

VCL Rich Edit: An Enter Password Dialog

If the password is incorrect, the control cannot decrypt the file and displays the following error message:

VCL Rich Edit: An Incorrect Password Error

Save Documents

The control has SaveDocument and Document.SaveDocument procedures that allow you to save a document to a file or stream. A user can execute SaveDocument and SaveDocumentAs commands to save the current document if the control’s Options.Behavior.Save and Options.Behavior.SaveAs properties are set to Default or Enabled. Both commands invoke the Save As dialog for a new document.

Save Encrypted Documents

The control can save a document as an encrypted DOCX file. Call the control’s Document.SetEncryptionPassword procedure to assign an encryption password to the document. To identify if the opened document has an assigned encryption password, call the Document.HasEncryptionPassword function. A user can execute the EncryptDocument command to invoke the Encrypt File Contents dialog and assign a password to the document:

VCL Rich Edit: An Encrypt File Contents Dialog

If new and re-entered passwords do not match, the control is unable to encrypt the document and displays the following error message:

VCL Rich Edit: Failed Password Confirmation

Limitations

Only the DOCX file format supports document file encryption. If a user saves the document in another format, the control displays the following dialog:

VCL Rich Edit: Password Protection Loss Confirmation