Working with Documents
Overview
Document represents a Word document. It is the entry point for most tasks: you create an empty one, open an existing file by passing its name to the constructor, read it, and save it. This page shows how to read the text of a document and how to merge two documents. Every class is in the com.aspose.words package.
Prerequisites
| Requirement | Detail |
|---|---|
| Java | 8 or later |
| Dependency | The package from Installation |
| Import | import com.aspose.words.*; |
Read the Text of a Document
Pass a file name to the Document constructor to open a file. getText, which Document inherits from Node, gets the text of this node and of all its children. The same recipe then saves the document as plain text and as Markdown by giving save a file name with that 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 structureMerge Two Documents
appendDocument appends the second document to the end of the first. Its second argument, an ImportFormatMode, decides how formatting is merged:
| Value | Meaning |
|---|---|
USE_DESTINATION_STYLES | Use the destination document styles and copy new styles. |
KEEP_SOURCE_FORMATTING | Copy all required styles to the destination document, generate unique style names if needed. |
KEEP_DIFFERENT_STYLES | The third mode; this guide does not use it. |
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");Tips and Best Practices
- The
Documentconstructors andDocument.savedeclarethrows Exception, so call them from a method that declares it or catches it. - Use
KEEP_SOURCE_FORMATTINGwhen the appended content must look as it did in its source document. - Open the saved file again with the
Documentconstructor to check the result.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
| The compiler reports cannot find symbol for Document. | The com.aspose.words package is not imported. | Add import com.aspose.words.*;. |
| Saving throws an UnsupportedOperationException with a message starting with FOSS. | The target format is not supported by this edition. | Save as DOCX, Markdown, or plain text. See Saving Documents. |
| The compiler reports an unreported exception. | The method you call declares throws Exception. | Declare it on the calling method or catch it. |
FAQ
Do I need Microsoft Word installed?
No. The examples on this page ran without Word.
Which classes should I start with?
Document, which holds the whole document, and DocumentBuilder, which writes content into it.
How do I combine two documents?
Open both and call appendDocument on the first, passing the second and an ImportFormatMode.
API Reference Summary
| Class / Method | Description |
|---|---|
Document | Represents a Word document. |
Node.getText() | Gets the text of this node and of all its children. |
Document.save(fileName) | Saves the document to a file. |
Document.appendDocument(srcDoc, importFormatMode) | Appends the specified document to the end of this document. |
ImportFormatMode | Specifies how formatting is merged when importing content from another document. |