How to Export and Save Word Documents in .NET

How to Export and Save Word Documents in .NET

This guide shows how to export and save a Word document with Aspose.Words FOSS for .NET: saving with an inferred or explicit format, configuring OOXML save options, exporting to Markdown, exporting to plain text, and detecting a file’s format before converting it. Aspose.Words FOSS for .NET is not yet published as a NuGet package, so reference it by building from source first.

Step-by-Step Guide

Step 1: Get the Library

Clone the repository and build Aspose.Words.sln in Release configuration, then add a project reference to Aspose.Words.csproj from your own .NET project, as described in the Installation guide. Add using Aspose.Words; at the top of any file that saves documents.


Step 2: Save with an Inferred or Explicit Format

Document.Save(fileName) infers the target format from the file’s extension. Save(fileName, saveFormat) makes the format explicit regardless of extension by taking a SaveFormat enum value, and Save(fileName, saveOptions) (or the matching Stream overloads) takes a SaveOptions subclass for format-specific control. SaveOptions.CreateSaveOptions(saveFormat) is a static factory that returns the matching options subclass with its defaults already set for a given SaveFormat. This edition saves fully both to and from DOCX, DOCM, DOTX, DOTM, and Flat OPC (plus their macro-enabled and template variants), as well as Markdown and plain text.


Step 3: Configure OOXML Export Options

OoxmlSaveOptions configures saving to any of the Open XML variants. Its SaveFormat property selects DOCX, DOCM, DOTX, DOTM, or Flat OPC specifically; Password sets an open password on the saved file, independent of Document.Protect’s editing restriction; 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).


Step 4: Export to Markdown

MarkdownSaveOptions controls a Markdown export: ListExportMode and LinkExportMode govern how lists and hyperlinks render, ImagesFolder / ImagesFolderAlias control where extracted images go and how they’re referenced, ExportImagesAsBase64 embeds them inline instead of writing separate files, 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.


Step 5: Export to Plain Text

TxtSaveOptions (via its shared base TxtSaveOptionsBase, also used by MarkdownSaveOptions) controls plain-text export: Encoding sets the character encoding, ParagraphBreak sets the line-ending string, MaxCharactersPerLine wraps long lines, SimplifyListLabels normalizes list bullets and numbers to plain characters, and PreserveTableLayout keeps table columns aligned using spaces.


Step 6: Detect a Format Before Converting

Before converting a file of unknown format, FileFormatUtil.DetectFileFormat(fileName) returns a FileFormatInfo with the detected LoadFormat. Pair it with FileFormatUtil.LoadFormatToSaveFormat(loadFormat) to check whether that format can round-trip through this edition’s supported formats before calling Document.Save. Some SaveFormat enum members exist for API completeness but name formats this reduced, open-source edition does not implement a working reader or writer for; stick to the DOCX/DOCM/DOTX/DOTM/Flat OPC family plus Markdown and plain text for reliable output.

Common Issues and Fixes

Save(fileName) writes 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 instead of relying on the extension.

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 separate files. Set ExportImagesAsBase64 = true for one self-contained Markdown 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.

Frequently Asked Questions

How do I save to a specific format regardless of the file extension?

Call Save(fileName, saveFormat) with an explicit SaveFormat value, or Save(fileName, saveOptions) with a matching SaveOptions subclass.

Which formats round-trip both ways in this edition?

DOCX, DOCM, DOTX, DOTM, Flat OPC (plus their macro-enabled and template variants), Markdown, and plain text.

How do I password-protect a saved file so it can’t even be opened?

Set Password on an OoxmlSaveOptions instance and pass it to Document.Save(fileName, saveOptions).

How do I control line endings in an exported text file?

Set ParagraphBreak on a TxtSaveOptions instance before saving.

How do I check a file’s format before converting it?

Call FileFormatUtil.DetectFileFormat(fileName) and read the returned FileFormatInfo.LoadFormat.

See Also