Loading Documents

Overview

Loading a document means passing its file name to the Document constructor. Document detects the format of the file it opens, except that Markdown is recognised only by the .md file extension: a Markdown file under another name, or one read from a stream, loads as plain text unless you pass load options whose LoadFormat is MARKDOWN. This page shows how to load a Markdown file and save it as DOCX, and lists the formats that were loaded and the ones this edition rejects.


Prerequisites

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

Load Markdown and Save as DOCX

Open a Markdown file with the Document constructor and save the document with a .docx file name:

import com.aspose.words.*;

Document doc = new Document("notes.md");
doc.save("notes.docx");

Formats You Can Load

FormatLoaded
DOCX, DOCM, DOTX, DOTMYes
Flat OPC and its macro-enabled, template, and macro-enabled template variantsYes
MarkdownYes
Plain textYes
HTML, ODT, RTFNo. Loading throws an UnsupportedOperationException whose message starts with FOSS.

These results come from loading each format on Java 8, 17 and 21. Other LoadFormat values were not tested.


Tips and Best Practices

  • Save the converted file under a new name, as the example does.
  • LoadOptions allows you to specify additional options, such as a password or base URI, when loading a document into a Document object.
  • MarkdownLoadOptions allows you to specify additional options when loading a Markdown document into a Document object.

Common Issues

IssueCauseFix
Loading throws an UnsupportedOperationException with a message starting with FOSS.The format is HTML, ODT, or RTF, which this edition rejects.Convert the file to DOCX or Markdown first.
The compiler reports cannot find symbol for Document.The com.aspose.words package is not imported.Add import com.aspose.words.*;.

FAQ

Can I open a Markdown file?

Yes. The example above loads notes.md and saves it as DOCX.

Can I open an HTML file?

No. Loading HTML throws an UnsupportedOperationException. DocumentBuilder.insertHtml is a separate method that ran without an error on a short HTML fragment.

Where do I set a password or a base URI?

In LoadOptions.


API Reference Summary

Class / MethodDescription
LoadFormatIndicates the format of the document that is to be loaded.
LoadOptionsAllows to specify additional options (such as password or base URI) when loading a document into a Document object.
MarkdownLoadOptionsAllows to specify additional options when loading Markdown document into a Document object.

Related Guides

See Also