Introduction
Aspose.Words FOSS for Java is the open-source edition of Aspose.Words for Java. It creates, edits and converts Word documents from Java code, and it does not need Microsoft Word. The library is published on Maven Central as org.aspose:aspose-words-foss:26.3.0, its API is in the com.aspose.words package, and it needs Java 8 or later. Your build tool downloads the ten libraries it depends on.
Document holds a whole document and DocumentBuilder writes content into it. DOCX, DOCM, DOTX, DOTM, Flat OPC, Markdown and plain text can be loaded and saved. The sections below show the tasks that were 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. The list is under Supported Formats, and saving as PDF or HTML is not among them.
Key Features
Build a Document with a Table
DocumentBuilder keeps a cursor in the document. The example writes a heading and a paragraph, then builds a table cell by cell with startTable, insertCell, endRow and endTable, and saves the result as DOCX.
import com.aspose.words.*;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);
builder.writeln("Quarterly Report");
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.BODY_TEXT);
builder.writeln("Generated on " + java.time.LocalDate.now());
builder.startTable();
builder.insertCell(); builder.write("Region");
builder.insertCell(); builder.write("Revenue");
builder.endRow(); builder.endTable();
doc.save("report.docx");
Convert Markdown to DOCX
Pass the file name of a Markdown file to 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");
Replace Text in a Template
Range.replace replaces all occurrences of a string in the range and returns the number of replacements. getRange is inherited from Node. The example fills a placeholder in a contract template and saves a new file.
import com.aspose.words.*;
Document doc = new Document("contract.docx");
doc.getRange().replace("{{CLIENT_NAME}}", "Acme Corp");
doc.save("contract-filled.docx");
Accept Tracked Changes
acceptAllRevisions accepts every tracked change in the document, so the saved file contains the final text.
import com.aspose.words.*;
Document doc = new Document("reviewed.docx");
doc.acceptAllRevisions();
doc.save("final.docx");
Merge Two Documents
appendDocument appends one document to another. ImportFormatMode.KEEP_SOURCE_FORMATTING keeps the formatting of the appended document.
import com.aspose.words.*;
Document main = new Document("main.docx");
Document appendix = new Document("appendix.docx");
main.appendDocument(appendix, ImportFormatMode.KEEP_SOURCE_FORMATTING);
main.save("combined.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 creates a document with a heading and a paragraph and saves it as Hello.docx. It then opens Report.docx, which must exist in the directory the program runs in, and saves 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
DOCX, DOCM, DOTX, DOTM, four Flat OPC variants, Markdown and plain text were loaded and saved in both directions.
| Format | Extension | Read | Write |
|---|---|---|---|
| DOCX | .docx | ✓ | ✓ |
| DOCM | .docm | ✓ | ✓ |
| DOTX | .dotx | ✓ | ✓ |
| DOTM | .dotm | ✓ | ✓ |
| Markdown | .md | ✓ | ✓ |
| Plain text | .txt | ✓ | ✓ |
| Flat OPC (four variants) | — | ✓ | ✓ |
Saving as PDF, XPS, HTML, MHTML, EPUB, RTF, DOC, ODT, WordML, PNG, SVG or TIFF throws an UnsupportedOperationException whose message starts with FOSS, and so does loading HTML, ODT or RTF. Signing a document with DigitalSignatureUtil.sign is rejected in the same way.
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.