Saving Documents

Overview

Document.save writes a document to a file. For Markdown and plain text the format follows from the file extension, and a SaveFormat value can also be passed. This page shows how to save the same document as plain text and as Markdown, and lists the formats that were saved and the ones this edition rejects.


Prerequisites

RequirementDetail
Java8 or later
DependencyThe package from Installation
Importimport com.aspose.words.*;

Save as Text and as Markdown

The recipe opens a DOCX, reads its text, and saves the document as plain text and as Markdown by giving save a file name with that extension:

import com.aspose.words.*;

Document doc = new Document("input.docx");
String text = doc.getText();           // fast, includes control chars
doc.save("input.txt");                 // or: clean plain-text export
doc.save("input.md");                  // or: Markdown, preserves structure

Formats You Can Save

FormatSaved
DOCX, DOCM, DOTX, DOTMYes
Flat OPC and its macro-enabled, template, and macro-enabled template variantsYes
MarkdownYes
Plain textYes
PDF, XPS, HTML, MHTML, EPUB, RTF, DOC, ODT, WordML, PNG, SVG, TIFFNo. Saving throws an UnsupportedOperationException whose message starts with FOSS.

These results come from saving each format with SaveFormat on Java 8, 17 and 21. Other SaveFormat values were not tested.


Tips and Best Practices

  • A Markdown file and a plain-text file saved by this edition both start with a UTF-8 byte order mark (the bytes EF BB BF), also when the format comes from the file name extension. Strip it if the tool that reads the file does not expect one.
  • MarkdownSaveOptions specifies additional options when saving a document into the Markdown format.
  • Check the format first if you need PDF or HTML output: this edition rejects both.

Common Issues

IssueCauseFix
Saving throws an UnsupportedOperationException with a message starting with FOSS.The format is one of the rejected ones, such as PDF or HTML.Save as DOCX, Markdown, or plain text.
An empty file is left behind after a rejected save.Saving as HTML, MHTML, or EPUB throws, but the output file has already been created and stays at 0 bytes.Delete the file, or save to a supported format.
The compiler reports cannot find symbol for Document.The com.aspose.words package is not imported.Add import com.aspose.words.*;.

FAQ

Can I save as PDF?

No. Saving as PDF throws an UnsupportedOperationException whose message starts with FOSS.

Which formats can I save?

DOCX, DOCM, DOTX, DOTM, the four Flat OPC variants, Markdown, and plain text.

Where do Markdown options go?

In MarkdownSaveOptions.


API Reference Summary

Class / MethodDescription
Document.save(fileName)Saves the document to a file.
Document.save(fileName, saveFormat)Saves the document to a file in the specified format.
SaveFormatIndicates the format in which the document is saved.
MarkdownSaveOptionsClass to specify additional options when saving a document into the Markdown format.

Related Guides

See Also