How to Save a Word Document as Text or Markdown in Java
Aspose.Words FOSS for Java saves a Word document in several formats with Document.save. This guide saves one document as plain text and as Markdown and lists the formats that work and the ones this edition rejects, with the library from Maven Central.
Step-by-Step Guide
Step 1: Install the Package
Add the package to your build. The library needs Java 8 or later.
<dependency>
<groupId>org.aspose</groupId>
<artifactId>aspose-words-foss</artifactId>
<version>26.3.0</version>
</dependency>Step 2: Import the Classes
All classes are in the com.aspose.words package, so the example starts with import com.aspose.words.*;.
Step 3: Save as Text and as Markdown
The recipe opens a DOCX, reads its text, and saves the document by giving save a file name with the extension of the target format. That works for plain text and Markdown; the Flat OPC variants were saved by passing a SaveFormat value. All three statements of the recipe run, so the document is saved twice.
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 structureStep 4: Know Which Formats Can Be Saved
DOCX, DOCM, DOTX, DOTM, Flat OPC and its variants, Markdown, and plain text were saved on Java 8, 17 and 21. Saving as PDF, XPS, HTML, MHTML, EPUB, RTF, DOC, ODT, WordML, PNG, SVG, or TIFF throws an UnsupportedOperationException whose message starts with FOSS.
Common Issues and Fixes
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.
The text or Markdown file starts with unexpected bytes. Files saved by this edition in both formats start with a UTF-8 byte order mark (EF BB BF). Strip it if your reader does not expect one.
The compiler reports cannot find symbol for Document. The com.aspose.words package is not imported. Add import com.aspose.words.*;.
Frequently Asked Questions
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 I set Markdown options?
In MarkdownSaveOptions, which specifies additional options when saving a document into the Markdown format.