Create, Load, and Save Documents
- 7 minutes to read
The Word Processing Document API allows you to create documents, load existing documents from streams and byte arrays, and save documents in different formats. The WordDocument class is the document object.
Create a Document
Call the parameterless WordDocument() constructor to create an empty document. A new document contains a single section and no paragraphs. Use the WordDocument.Body property to access the main document content.
The following code snippet creates a document, adds two paragraphs, and saves the document to a DOCX file:
using System.IO;
using DevExpress.Docs.Word;
using var document = new WordDocument();
document.Body.AddParagraph("Quarterly Report");
document.Body.AddParagraph("Sales grew by 12% in the third quarter.");
Directory.CreateDirectory("output");
using var stream = File.Create(Path.Combine("output", "Document.docx"));
document.Save(stream, DocumentFormat.Docx);
Note
The WordDocument class implements IDisposable. Declare a WordDocument instance in a using statement (C#) or a Using block (VB.NET) to release document resources.
Load a Document
Pass a document stream or byte array to a WordDocument constructor. If you do not specify the document format, the API detects the format automatically.
Load from a Stream
The following code snippet loads a document from a file stream:
using System.IO;
using DevExpress.Docs.Word;
using var stream = File.OpenRead("Document.docx");
using var document = new WordDocument(stream);
// Process the document.
To specify the format of the source document, pass a DocumentFormat value to the WordDocument(stream, documentFormat) constructor.
The following code snippet loads a DOCX document from a file stream:
using System.IO;
using DevExpress.Docs.Word;
using var stream = File.OpenRead("Document.docx");
using var document = new WordDocument(stream, DocumentFormat.Docx);
// Process the document.
Load from a Byte Array
Pass a byte array to the WordDocument(buffer) or WordDocument(buffer, documentFormat) constructor.
The following code snippet loads a DOCX document from a byte array:
using System.IO;
using DevExpress.Docs.Word;
var buffer = File.ReadAllBytes("Document.docx");
using var document = new WordDocument(buffer, DocumentFormat.Docx);
// Process the document.
Specify Load Options
Pass a LoadOptions object to the WordDocument(stream, options) or WordDocument(buffer, options) constructor to specify the document format, a password, and format-specific load settings:
| Property | Description |
|---|---|
| DocumentFormat | Gets or sets the format of the document to load. |
| Password | Gets or sets the password that the API uses to open an encrypted document. |
LoadOptions.DocxOptions, LoadOptions.HtmlOptions, LoadOptions.PlainTextOptions, and other <Format>Options properties |
Provide access to settings that the API applies when it loads a document in the corresponding format. |
The following code snippet loads a DOCX document and skips XML parse errors in the source file:
using System.IO;
using DevExpress.Docs.Word;
var loadOptions = new LoadOptions {
DocumentFormat = DocumentFormat.Docx
};
loadOptions.DocxOptions.IgnoreParseErrors = true;
using var stream = File.OpenRead("Document.docx");
using var document = new WordDocument(stream, loadOptions);
// Process the document.
Load an Encrypted Document
To open a password-encrypted document, set the LoadOptions.Password property to the document password. The API clears the Password property value after each load attempt. To load another encrypted document with the same LoadOptions object, specify the password again.
The following code snippet loads a password-encrypted document:
using System.IO;
using DevExpress.Docs.Word;
var loadOptions = new LoadOptions {
Password = "password"
};
using var stream = File.OpenRead("Document.docx");
using var document = new WordDocument(stream, loadOptions);
// Process the document.
If the document is encrypted and you do not specify a password, the WordDocument constructor throws a System.Exception with the “Cannot load the document.” message. The InnerException property contains an InvalidOperationException with the “A password is required to open this file.” message. If the password is incorrect, the InvalidOperationException message is “The specified password is incorrect.”
Tip
To ensure that the API loads an encrypted document in the correct format, set the LoadOptions.DocumentFormat property to the format of the source document.
Refer to the following help topic for additional information: Manage Document Security.
Save a Document
Call one of the following WordDocument.Save methods to save a document:
- WordDocument.Save(stream), WordDocument.Save(stream, documentFormat), and WordDocument.Save(stream, options) methods write the document to a stream.
- WordDocument.Save(), WordDocument.Save(documentFormat), and WordDocument.Save(options) methods return the document as a byte array.
Save to a Stream
The following code snippet loads a DOCX document and saves it to an RTF file:
using System.IO;
using DevExpress.Docs.Word;
using var document = new WordDocument(File.ReadAllBytes("Document.docx"));
Directory.CreateDirectory("output");
using var stream = File.Create(Path.Combine("output", "Document.rtf"));
document.Save(stream, DocumentFormat.Rtf);
If you call the WordDocument.Save(stream) method, pass DocumentFormat.Undefined to a WordDocument.Save method, or set the SaveOptions.DocumentFormat property to DocumentFormat.Undefined, the API determines the output format from the file name extension of a FileStream:
| File Extension | Output Format |
|---|---|
| .docx | DocumentFormat.Docx |
| .docm | DocumentFormat.Docm |
| .dotx | DocumentFormat.Dotx |
| .dotm | DocumentFormat.Dotm |
| .doc | DocumentFormat.Doc |
| .dot | DocumentFormat.Dot |
| .rtf | DocumentFormat.Rtf |
| .txt | DocumentFormat.PlainText |
| .htm, .html | DocumentFormat.Html |
| .mht, .mhtml | DocumentFormat.Mht |
| .odt | DocumentFormat.Odt |
| .xml | DocumentFormat.WordML |
| .epub | DocumentFormat.ePub |
For other extensions, other stream types, and byte arrays, the API saves the document in DOCX format.
Save to a Byte Array
The following code snippet creates a document, saves it to a byte array in DOCX format, and writes the array to a file:
using System.IO;
using DevExpress.Docs.Word;
using var document = new WordDocument();
document.Body.AddParagraph("Quarterly Report");
var buffer = document.Save(DocumentFormat.Docx);
Directory.CreateDirectory("output");
File.WriteAllBytes(Path.Combine("output", "Document.docx"), buffer);
Specify Save Options
Pass a SaveOptions object to a Save method to specify the output format and format-specific save settings:
| Property | Description |
|---|---|
| DocumentFormat | Gets or sets the output document format. |
SaveOptions.DocxOptions, SaveOptions.HtmlOptions, SaveOptions.RtfOptions, and other <Format>Options properties |
Provide access to settings that the API applies when it saves a document in the corresponding format. |
Use the ExportedDocumentProperties property of format-specific save options (for example, OpenXmlSaveOptions.ExportedDocumentProperties for DOCX files) to specify document properties that the API writes to the output document.
The following code snippet saves a document to an HTML file with embedded images:
using System.IO;
using DevExpress.Docs.Word;
using var document = new WordDocument(File.ReadAllBytes("Document.docx"));
var saveOptions = new SaveOptions {
DocumentFormat = DocumentFormat.Html
};
saveOptions.HtmlOptions.EmbedImages = true;
Directory.CreateDirectory("output");
using var stream = File.Create(Path.Combine("output", "Document.html"));
document.Save(stream, saveOptions);
Handle Load and Save Errors
If the API cannot load or save a document, WordDocument constructors and Save methods throw a System.Exception. The exception message is “Cannot load the document.” or “Cannot save the document.” The InnerException property contains the original exception.
If you pass null as a stream, byte array, or options object, the API throws an ArgumentNullException. If you pass a value that is not specified in DocumentFormat (for example, (DocumentFormat)99), the API throws an ArgumentOutOfRangeException.
If a document exceeds a limit specified by the LoadOptions.SecurityLoadingLimits property, the WordDocument constructor throws a SecurityLoadingLimitExceededException.