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
| Requirement | Detail |
|---|---|
| Java | 8 or later |
| Dependency | The package from Installation |
| Import | import 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
| Format | Loaded |
|---|---|
| DOCX, DOCM, DOTX, DOTM | Yes |
| Flat OPC and its macro-enabled, template, and macro-enabled template variants | Yes |
| Markdown | Yes |
| Plain text | Yes |
| HTML, ODT, RTF | No. 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.
LoadOptionsallows you to specify additional options, such as a password or base URI, when loading a document into aDocumentobject.MarkdownLoadOptionsallows you to specify additional options when loading a Markdown document into aDocumentobject.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
| 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 / Method | Description |
|---|---|
LoadFormat | Indicates the format of the document that is to be loaded. |
LoadOptions | Allows to specify additional options (such as password or base URI) when loading a document into a Document object. |
MarkdownLoadOptions | Allows to specify additional options when loading Markdown document into a Document object. |