Skip to main content

Document Server: Document Management

  • 4 minutes to read

The Document Server component 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 Document Server component 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 – imported and exported documents may look different compared to corresponding source documents.

Manage Supported Document Formats

The Document Server component supports only the plain text format 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 Document Server component to add support for this format. You can also 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
Rtf dxRichEdit.Rtf dxRichEditControlRtfFormatRS~
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
Rtf #include "dxRichEdit.Rtf.hpp" #pragma link "dxRichEdit.Rtf" dxRichEditControlRtfFormatRS~.lib
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 Document Server component 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 the checked formats to header and source code files. All corresponding library names are added to the project’s AllPackageLibs list automatically.

VCL Rich Edit: Design-Time Format Management

Create a New Document

The Document Server component contains an empty document at application startup. You can call the component’s CreateNewDocument function or the Document.CreateNewDocument procedure to replace the current content with a new empty document.

The CreateNewDocument function and the Document.CreateNewDocument procedure raise the OnDocumentClosing event. You can handle it to save pending document changes when the current document is about to close.

Load Documents

The Document Server component has LoadDocument and Document.LoadDocument methods that allow you to load a document from a file or stream.

The following code loads the Example.txt file from the application folder:

dxRichEditDocumentServer1.LoadDocument('Example.txt');

Load Encrypted Documents

The OnGetEncryptionPassword event occurs when the Document Server loads an encrypted DOCX file. You can handle this event to specify the password required to open an encrypted document. If the password is incorrect, the Document Server raises the EdxOLECryptoContainerError event and cancels the file load operation.

The following code shows an OnGetEncryptionPassword event handler that specifies a password:

procedure MyForm.dxRichEditDocumentServer1GetEncryptionPassword(Sender: TObject;
  var APassword: string; var AHandled: Boolean);
begin
  APassword := 'Password';
  AHandled := True;
end;

Save Documents

The Document Server component has SaveDocument and Document.SaveDocument procedures that allow you to save a document to a file or stream.

The following code saves the current document to a stream in TXT format:

var AStream: TStream;
// ...
AStream := TStream.Create;
dxRichEditDocumentServer1.SaveDocument(AStream, TdxRichEditDocumentFormat.PlainText);

Save Encrypted Documents

The Document Server component can save a document as an encrypted DOCX file. Call the component’s Document.SetEncryptionPassword procedure to assign an encryption password to the document. If you save the document with a password in another format, the Document Server component rewrites or creates a file without protection. Call the component’s Document.HasEncryptionPassword function to identify if the opened document has an assigned encryption password.

The following code assigns a password to the current document:

dxRichEditControl1.Document.SetEncryptionPassword('Password');