officefileapi-15441-word-processing-document-api-import-and-export.md
This document describes how to load and save documents in different formats.
The Word Processing Document API supports the following document formats:
TXT
RTF
DOCX
DOC
DOCM
DOT
DOTM
DOTX
WordML
OpenDocument
HTML
MHTML
XML
PDF (export only)
The RichEditDocumentServer ships with the following methods to load or save the document and specify its options. Refer to the following examples section for code samples: Files.
| Member | Description |
|---|---|
| RichEditDocumentServer.LoadDocument | Loads a document from a file or stream. The document format can be specified by one of the DocumentFormat enumeration values or detected automatically. |
| RichEditDocumentServer.LoadDocumentTemplate | Loads a document template from the specified file or stream so that it cannot be overwritten automatically. |
| Document.LoadDocument | Loads a document from the specified file or stream. Its content can be specified by one of the DocumentFormat enumeration values or is automatically determined based on the file’s format. |
| SubDocument.InsertDocumentContent | Inserts content from the selected range into the current document at the specified position. |
| SubDocument.AppendDocumentContent | Appends content from the selected range. |
| RichEditDocumentServer.SaveDocument | Saves the server’s document to a file or stream and specifies the document’s format. |
| Document.SaveDocument | Saves the document to a file or stream and specifies the document’s format. |
The following code snippet loads the document from a stream and saves the result to the file.
using DevExpress.XtraRichEdit;
using System.Diagnostics;
using System.Reflection;
using (var wordProcessor = new RichEditDocumentServer())
{
using (Stream stream = GetResourceStream("MyApplication.Document1.rtf"))
{
stream.Seek(0, SeekOrigin.Begin);
wordProcessor.LoadDocument(stream, DocumentFormat.Rtf);
stream.Close();
}
wordProcessor.SaveDocument("Result.docx", DocumentFormat.Docx);
}
Process.Start(new ProcessStartInfo("explorer.exe", "/select," + "Result.docx") { UseShellExecute = true });
static Stream GetResourceStream(string resourceName)
{
return Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
}
Imports DevExpress.XtraRichEdit
Imports System.Diagnostics
Imports System.Reflection
Using wordProcessor = New RichEditDocumentServer()
Using stream As Stream = GetResourceStream("MyApplication.Document1.rtf")
stream.Seek(0, SeekOrigin.Begin)
wordProcessor.LoadDocument(stream, DocumentFormat.Rtf)
stream.Close()
End Using
wordProcessor.SaveDocument("Result.docx", DocumentFormat.Docx)
End Using
Process.Start(New ProcessStartInfo("explorer.exe", "/select," & "Result.docx") With {.UseShellExecute = True})
Shared Function GetResourceStream(ByVal resourceName As String) As Stream
Return Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)
End Function
Refer to the following examples for more code samples:
Note
We do not recommend that you use the DocumentFormat.Undefined field as the SaveDocument method parameter. Otherwise, the document is saved with invalid format.
The table below lists the document formats the RichEditDocumentServer supports, and the API used to set format-specific import and export options. You can specify these options in the RichEditDocumentServer.BeforeImport or RichEditDocumentServer.BeforeExport event handlers.
Tip
Use the SubDocument‘s Get... methods to retrieve and format a part of the document. Refer to the following topic for details: Positions and Ranges.
Check the following examples section for more information on how to accomplish export-related tasks: Export.
The following code snippet handles the RichEditDocumentServer.BeforeImport event for different document formats:
using DevExpress.XtraRichEdit.Import;
//...
private void RichEditDocumentServer_BeforeImport(object sender, BeforeImportEventArgs e)
{
if (e.DocumentFormat == DocumentFormat.PlainText)
{
//Detect plain text encoding automatically:
((PlainTextDocumentImporterOptions)e.Options).AutoDetectEncoding = true;
}
if (e.DocumentFormat == DocumentFormat.Doc)
{
//Disable loading comments added to removed ranges in DOC documents
((DocDocumentImporterOptions)e.Options).KeepCommentsForRemovedRanges = false;
}
if (e.DocumentFormat == DocumentFormat.Html)
{
//Load images synchronously with HTML documents
((HtmlDocumentImporterOptions)e.Options).AsyncImageLoading = false;
}
}
Imports DevExpress.XtraRichEdit.Import
Private Sub RichEditDocumentServer_BeforeImport(ByVal sender As Object, ByVal e As BeforeImportEventArgs)
If e.DocumentFormat = DocumentFormat.PlainText Then
'Detect plain text encoding automatically:
(CType(e.Options, PlainTextDocumentImporterOptions)).AutoDetectEncoding = True
End If
If e.DocumentFormat = DocumentFormat.Doc Then
'Disable loading comments added to removed ranges in DOC documents
(CType(e.Options, DocDocumentImporterOptions)).KeepCommentsForRemovedRanges = False
End If
If e.DocumentFormat = DocumentFormat.Html Then
'Load images synchronously with HTML documents
(CType(e.Options, HtmlDocumentImporterOptions)).AsyncImageLoading = False
End If
End Sub
The following code snippet specifies export options for different formats in the RichEditDocumentServer.BeforeExport event handler.
private void RichEditDocumentServer_BeforeExport(object sender, BeforeExportEventArgs e)
{
if (e.DocumentFormat == DocumentFormat.PlainText)
{
//Include document fields in the exported plain text:
PlainTextDocumentExporterOptions plainTextOptions = e.Options as PlainTextDocumentExporterOptions;
plainTextOptions.ExportHiddenText = true;
plainTextOptions.FieldCodeEndMarker = ">";
plainTextOptions.FieldCodeStartMarker = "[<";
plainTextOptions.FieldResultEndMarker = "]";
}
if (e.DocumentFormat == DocumentFormat.Docx)
{
//Specify what DOCX document properties to export:
OpenXmlDocumentExporterOptions docxOptions = e.Options as OpenXmlDocumentExporterOptions;
docxOptions.ExportedDocumentProperties = DocumentPropertyNames.Title | DocumentPropertyNames.LastModifiedBy | DocumentPropertyNames.Modified;
}
if (e.DocumentFormat == DocumentFormat.Html)
{
//Specify HTML export options:
HtmlDocumentExporterOptions htmlOptions = e.Options as HtmlDocumentExporterOptions;
htmlOptions.EmbedImages = true;
htmlOptions.CssPropertiesExportType = CssPropertiesExportType.Style;
htmlOptions.UseFontSubstitution = false;
}
}
Private Sub RichEditDocumentServer_BeforeExport(ByVal sender As Object, ByVal e As BeforeExportEventArgs)
If e.DocumentFormat = DocumentFormat.PlainText Then
'Include document fields in the exported plain text:
Dim plainTextOptions As PlainTextDocumentExporterOptions = TryCast(e.Options, PlainTextDocumentExporterOptions)
plainTextOptions.ExportHiddenText = True
plainTextOptions.FieldCodeEndMarker = ">"
plainTextOptions.FieldCodeStartMarker = "[<"
plainTextOptions.FieldResultEndMarker = "]"
End If
If e.DocumentFormat = DocumentFormat.Docx Then
'Specify what DOCX document properties to export:
Dim docxOptions As OpenXmlDocumentExporterOptions = TryCast(e.Options, OpenXmlDocumentExporterOptions)
docxOptions.ExportedDocumentProperties = DocumentPropertyNames.Title Or DocumentPropertyNames.LastModifiedBy Or DocumentPropertyNames.Modified
End If
If e.DocumentFormat = DocumentFormat.Html Then
'Specify HTML export options:
Dim htmlOptions As HtmlDocumentExporterOptions = TryCast(e.Options, HtmlDocumentExporterOptions)
htmlOptions.EmbedImages = True
htmlOptions.CssPropertiesExportType = CssPropertiesExportType.Style
htmlOptions.UseFontSubstitution = False
End If
End Sub
Use the RichEditControlOptionsBase.DocumentCapabilities property to access the DocumentCapabilitiesOptions properties to control what document elements to import. Specify the options in the RichEditDocumentServer.BeforeImport event handler, as shown below. The restricted elements are lost when the document is saved.
static void RichEditDocumentServer_BeforeImport(object sender, BeforeImportEventArgs e) {
var wordProcessor = sender as RichEditDocumentServer;
DocumentCapabilitiesOptions capabilityOptions = wordProcessor.Options.DocumentCapabilities;
capabilityOptions.CharacterFormatting = DocumentCapability.Disabled;
capabilityOptions.Comments = DocumentCapability.Disabled;
capabilityOptions.InlineShapes = DocumentCapability.Disabled;
}
Shared Sub RichEditDocumentServer_BeforeImport(ByVal sender As Object, ByVal e As BeforeImportEventArgs)
Dim wordProcessor = TryCast(sender, RichEditDocumentServer)
Dim capabilityOptions As DocumentCapabilitiesOptions = wordProcessor.Options.DocumentCapabilities
capabilityOptions.CharacterFormatting = DocumentCapability.Disabled
capabilityOptions.Comments = DocumentCapability.Disabled
capabilityOptions.InlineShapes = DocumentCapability.Disabled
End Sub
Word Processing Document API supports right-to-left direction in main document body and complex elements (tables, comments, and shapes). To process the text direction in the RTF files correctly, make sure that the required content’s text direction is explicitly specified (that is, it is enclosed in the \rtlch tag).
The RichEditDocumentServer parses and transforms loaded HTML documents into the internal document model. However, not every HTML tag can be converted into a corresponding document model element. Refer to the HTML Tag Support topic for a list of supported tags.
See Also
Use Word Processing Document API to Load HTML Files or Export Documents to HTML