Saving and Exporting Documents
Saving and Exporting Documents
Document.Save writes a document to a file or stream in a target format. The format is either inferred from a file extension, given explicitly as a SaveFormat value, or driven by a SaveOptions subclass — OoxmlSaveOptions, MarkdownSaveOptions, or TxtSaveOptions — for format-specific control. This guide covers the Save overloads, the SaveFormat enum, OOXML save options, Markdown and plain-text export, and detecting a format before converting.
Saving with Document.Save
The simplest call, Document.Save(fileName), infers the target format from the file’s extension. Save(fileName, saveFormat) makes the format explicit regardless of extension, and Save(fileName, saveOptions) (or the matching Stream overloads) accepts a SaveOptions subclass for format-specific settings. SaveOptions.CreateSaveOptions(saveFormat) is a static factory that returns the right options subclass for a given SaveFormat with its defaults already set.
Choosing a Format with SaveFormat
The SaveFormat enum lists every save-format identifier the library recognizes. Document.Save writes fully both to and from this edition’s core document formats — DOCX, DOCM, DOTX, DOTM, and Flat OPC (plus its macro-enabled and template variants) — as well as Markdown and plain text. FileFormatUtil.SaveFormatToExtension(saveFormat) and ExtensionToSaveFormat(extension) convert between the enum and a file extension string.
Some SaveFormat values exist in the enum for API completeness but correspond to formats this reduced, open-source edition does not implement a working reader or writer for — attempting to save to one of those produces output that isn’t equivalent to the source document. Stick to the formats named above for reliable round-tripping.
OOXML Options: DOCX, DOCM, DOTX, DOTM, and Flat OPC
OoxmlSaveOptions configures saving to any of the Open XML formats. Its SaveFormat property selects which specific variant to write; Password sets an open password on the saved file (independent of Document.Protect’s editing restrictions); Compliance selects the OOXML strictness level; and CompressionLevel / Zip64Mode control the underlying ZIP packaging. Construct one with new OoxmlSaveOptions(saveFormat) or via SaveOptions.CreateSaveOptions(saveFormat).
Exporting to Markdown
MarkdownSaveOptions controls a Markdown export: ListExportMode and LinkExportMode govern how lists and hyperlinks are rendered, ImagesFolder / ImagesFolderAlias control where extracted images are written and how they’re referenced, ExportImagesAsBase64 embeds them inline instead, and TableContentAlignment affects generated table markup. Pass a MarkdownSaveOptions instance to Document.Save(fileName, saveOptions) with a .md file name, or set its SaveFormat explicitly.
Exporting to Plain Text
TxtSaveOptions (via its base TxtSaveOptionsBase, shared with MarkdownSaveOptions) controls plain-text export: Encoding sets the character encoding, ParagraphBreak sets the line-ending string, MaxCharactersPerLine wraps long lines, SimplifyListLabels normalizes list bullets/numbers to plain characters, and PreserveTableLayout attempts to keep table columns aligned using spaces.
Detecting a Format Before Converting
Before converting a file whose format you don’t already know, FileFormatUtil.DetectFileFormat(fileName) returns a FileFormatInfo with the detected LoadFormat. Combine this with FileFormatUtil.LoadFormatToSaveFormat(loadFormat) to decide whether a round-trip through this edition’s supported formats is possible before attempting Document.Save.
Tips and Best Practices
- Use
Save(fileName, saveOptions)instead of the plainSave(fileName, saveFormat)overload whenever you need to control format-specific behavior like Markdown list rendering or OOXML compression. - Set
OoxmlSaveOptions.Passwordfor an open-password-protected file; that’s separate fromDocument.Protect, which restricts editing but does not encrypt the file. - Choose
MarkdownSaveOptions.ExportImagesAsBase64for a single self-contained Markdown file, orImagesFolderwhen you want a folder of extracted image files alongside the Markdown. - Call
SaveOptions.CreateSaveOptions(saveFormat)instead of picking the concrete options class manually when the target format is determined at runtime. - Verify a target format with
FileFormatUtilbefore conversion rather than relying on a bare file extension, which can be wrong or misleading.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
Save(fileName) picks the wrong format | The file extension doesn’t match the intended format | Use Save(fileName, saveFormat) or Save(fileName, saveOptions) to make the format explicit |
| Saved OOXML file won’t open with a password | OoxmlSaveOptions.Password wasn’t set before saving | Construct an OoxmlSaveOptions, set Password, and pass it to Save |
| Markdown export scatters image files unexpectedly | Default MarkdownSaveOptions behavior extracts images to files | Set ExportImagesAsBase64 = true for a single self-contained file, or set ImagesFolder to control where images go |
| Plain-text export loses table column alignment | TxtSaveOptions.PreserveTableLayout is left at its default | Set PreserveTableLayout = true before saving |
FAQ
How do I save a document to a specific format regardless of its file extension?
Call Save(fileName, saveFormat) with an explicit SaveFormat value, or Save(fileName, saveOptions) with a matching SaveOptions subclass.
Which formats does this edition read and write both ways?
DOCX, DOCM, DOTX, DOTM, Flat OPC (plus its macro-enabled and template variants), Markdown, and plain text.
How do I password-protect a saved file?
Set Password on an OoxmlSaveOptions instance and pass it to Document.Save(fileName, saveOptions).
How do I control the line endings in an exported text file?
Set ParagraphBreak on a TxtSaveOptions instance before saving.
How do I check what format a file is before converting it?
Call FileFormatUtil.DetectFileFormat(fileName) and read the returned FileFormatInfo.LoadFormat.
API Reference Summary
| Class/Method | Description |
|---|---|
Document.Save | Writes the document to a file, stream, or output target in a chosen format. |
SaveFormat | Enum identifying the target format for a save operation. |
SaveOptions | Abstract base class for format-specific save configuration; CreateSaveOptions(saveFormat) returns the matching subclass. |
OoxmlSaveOptions | Options for DOCX, DOCM, DOTX, DOTM, and Flat OPC — password, compliance, compression. |
MarkdownSaveOptions | Options for Markdown export — list/link rendering, image handling. |
TxtSaveOptions / TxtSaveOptionsBase | Options for plain-text export — encoding, line breaks, table layout. |
FileFormatUtil.DetectFileFormat | Detects a file’s format before loading or converting it. |
FileFormatUtil.SaveFormatToExtension / ExtensionToSaveFormat | Convert between a SaveFormat value and a file extension. |