DOC, DOCX, and ODT are among the most common and widely in-use word-processing file formats. Microsoft Word and OpenOffice Writer support these formats and are used for drafting documents. Therefore, as a developer, we often need to programmatically edit Word documents within the applications. In this article, we will discuss how to edit Word documents using the Java API for document editing.
The following topics are covered in this article:
Java API for Word Documents Editing and Automation
GroupDocs.Editor provides Java API for document editing and allows developers to load, edit, and save various document formats using WYSIWYG HTML editors. In addition to the word-processing document formats, the API supports editing spreadsheets, presentations, HTML, XML, TXT, CSV, and many other formats.
Download or Configure
You may download the JAR file from the downloads section, or just get the repository and dependency configurations for the pox.xml of your maven-based Java applications.
<repository> <id>GroupDocsJavaAPI</id> <name>GroupDocs Java API</name> <url>http://repository.groupdocs.com/repo/</url> </repository> <dependency> <groupId>com.groupdocs</groupId> <artifactId>groupdocs-editor</artifactId> <version>20.11</version> </dependency>
Edit Word Documents in Java
After setting up the API, you can quickly move towards editing the Word document. The following steps will let you edit the word-processing documents DOC/DOCX in Java.
- Load the Word document using Editor.
- Fetch the Editable Document using edit().
- Get the embedded HTML of the loaded DOC/DOCX document.
- Edit the content programmatically or by using any WYSIWYG editor.
- Convert the edited content back to an Editable Document.
- Save the updated document using the appropriate save() method.
The following Java code allows editing Word documents within the application.
Load: You can apply additional options while loading the Word document; like providing the password if the document is protected.
Edit: After loading, you can edit the loaded document as per your requirement. The above example replaces all the occurrences of the word “document” with the “edited document” in a Word document.
Save: While saving the edited document, you can set different options. These options include; pagination, setting a password, memory optimization settings, and more.
The following is the output of the above code.
To sum up, we learned to edit Word documents in Java using document editing Java API. You can use the API along with WYSIWYG editors to visually edit your documents. You can build your own document editing Java application. For more details, options, and examples, you can visit the documentation and the GitHub repository. For further queries, contact the support on the forum.