Introduction
Text is the part of a Word document that most programs touch: they write a heading and a few paragraphs, read what a file contains, or fill a placeholder. Aspose.Words FOSS for Java does these tasks with the Document and DocumentBuilder classes in the com.aspose.words package. The library is MIT-licensed, published as org.aspose:aspose-words-foss:26.3.0, and needs Java 8 or later.
This post shows how to write a heading and a paragraph, read the text of a document, replace a placeholder, and convert between DOCX, Markdown and plain text. Every example was run against the published library on Java 8, 17 and 21.
This edition shares the com.aspose.words package name with Aspose’s commercial library, but it reads and writes a smaller set of formats, listed under Supported Formats.
Key Features
Read the Text of a Document
getText returns the text of a document. The example also saves the document as plain text and as Markdown by giving save a file name with the target 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
Replace Placeholders
Range.replace replaces all occurrences of a string and returns the number of replacements. The example replaces {{CLIENT_NAME}} in a contract template and saves the filled copy under a new name.
import com.aspose.words.*;
Document doc = new Document("contract.docx");
doc.getRange().replace("{{CLIENT_NAME}}", "Acme Corp");
doc.save("contract-filled.docx");
Turn Markdown Text into a Document
A Markdown file opens with the Document constructor like any other file. Saving the document with a .docx file name writes a Word document.
import com.aspose.words.*;
Document doc = new Document("notes.md");
doc.save("notes.docx");
Quick Start
Add the package to your build.
<dependency>
<groupId>org.aspose</groupId>
<artifactId>aspose-words-foss</artifactId>
<version>26.3.0</version>
</dependency>The example writes a heading and a paragraph with DocumentBuilder. The style of a paragraph comes from getParagraphFormat().setStyleIdentifier(...), using a StyleIdentifier value, and writeln inserts a line of text followed by a paragraph break. The last two statements open Report.docx, which must exist in the directory the program runs in, and save it as Markdown.
import com.aspose.words.*;
// Create a document from scratch.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);
builder.writeln("Hello from Aspose.Words FOSS!");
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.BODY_TEXT);
builder.writeln("This document was created entirely in code, no Word installed.");
doc.save("Hello.docx");
// Convert an existing document to Markdown.
Document report = new Document("Report.docx");
report.save("Report.md");
The Document constructors and Document.save declare throws Exception, so declare it on the calling method or catch it.
Supported Formats
| Format | Extension | Read | Write |
|---|---|---|---|
| DOCX | .docx | ✓ | ✓ |
| Markdown | .md | ✓ | ✓ |
| Plain text | .txt | ✓ | ✓ |
DOCM, DOTX, DOTM and four Flat OPC variants are also loaded and saved in both directions. Files saved as Markdown or plain text start with a UTF-8 byte order mark (EF BB BF), so strip it if your reader does not expect one. Saving as PDF, XPS, HTML, MHTML, EPUB, RTF, DOC, ODT, WordML or an image format throws an UnsupportedOperationException whose message starts with FOSS.
Open Source & Licensing
The library is released under the MIT license, which permits commercial use.
The source code is in the GitHub repository.
The package is on Maven Central as org.aspose:aspose-words-foss:26.3.0.