This example demonstrates how you can teach the RichEditControl in your application to open and save files in a custom format. Thanks to the IImporter and IExporter interfaces, this task is rather straightforward, and can be accomplished with ease.
As an example, we'll add support for a well-known format .eml. Files in this format are plain text in MIME standard, so the XtraRichEdit's built-in save and load methods for MHT content can be used. This fact simplifies the implementation, but you can write your own import/export helper classes if required.
First, provide a unique identifier for a new format. It should be a new DocumentFormat enumeration member. Take a value that is large enough to exceed the used range, e.g. 444. Then, create two classes - importer and exporter, and two classes that represent options for import and export.
The class containing options for import should be derived from the DocumentImporterOptions class or its descendants. In our case, it is the EmlDocumentImporterOptions derived from the MhtDocumentImporterOptions.
The class EmlDocumentImporter implements the IImporter<DocumentFormat, bool> interface. The basic functionality is encapsulated in its LoadDocument method. To load a document in .eml format, we simply call the DocumentModel.InternalAPI.LoadDocumentMhtContent method, but for other formats you can use your custom procedures.
The class containing options for export should be derived from the DocumentExporterOptions class or its descendants. In our case, it is the EmlDocumentExporterOptions derived from the MhtDocumentExporterOptions.
The class EmlDocumentImporter implements the IExporter<DocumentFormat, bool> interface. In its SaveDocument method, the DocumentModel.InternalAPI.SaveDocumentMhtContent method is used, but you are free to use a custom procedure instead.
We have all the required objects capable of performing export-import operations. To teach the XtraRichEdit to use our objects for handling .eml files, consider the XtraRichEdit services. Loose coupling provided by the service pattern enables us to easily incorporate the desired functionality into the XtraRichEdit behavior models. The IDocumentImportManagerService has the RegisterImporter method, and the IDocumentExportManagerService has the RegisterExporter method, designed for this purpose. Register our importer and exporter classes, and the RichEditControl is capable of loading and saving files in .eml format.
Note that this example uses objects from the DevExpress.XtraRichEdit.Model and DevExpress.XtraRichEdit.Internal namespaces, not included in the documentation. The DevExpress team does not encourage the use of objects which are not listed in our documentation (help reference), and does not guarantee that these objects will be retained in future versions.
Question Comments
Added By: Luis Sagasta at: 1/12/2016 3:26:26 AM
Hello.
Please, could you show me how to write my own import/export helper class?
I need to import from a PDF file (the method to convert PDF to RTF is provided by us) and I don't know how to approach into RTF editor.
Thank you and regards.
Added By: Oleg (DevExpress Support) at: 1/12/2016 4:15:44 AMHello Luis,
To process your recent post more efficiently, I created a separate ticket on your behalf: T333104: How to adapt the approach demonstrated in E2291 example to convert a PDF document to RTF format. This ticket is currently in our processing queue. Our team will address it as soon as we have any updates.