This example demonstrates how to edit a Word (DOCX) document programmatically using GroupDocs.Editor for Java. The document is converted to an editable HTML representation, modified directly in Java code, and then saved back to DOCX without any UI or WYSIWYG editor.
- Java Development Kit (JDK) 8 or higher
- Apache Maven 3.6+
- Internet connection (to download Maven dependencies)
GroupDocs libraries require a license file. You can obtain a 30‑day temporary license for free:
- Visit: https://purchase.groupdocs.com/temporary-license/
- Download the license file (e.g.,
GroupDocs.Editor.lic). - Place the license file in the project root directory (same level as
pom.xml).
Without a license the library runs in evaluation mode, adding watermarks and imposing usage limits.
project-root/
├─ pom.xml
├─ GroupDocs.Editor.lic # (optional) place your license here
├─ src/
│ └─ main/java/com/groupdocs/viewer/examples/EditWordDocumentProgrammaticallyExample.java
├─ resources/
│ ├─ input/
│ │ └─ sample.docx # sample input file (replace with your own)
│ └─ output/ # generated files will be saved here
└─ README.md
- Input files must be placed in
resources/input/. - Output files are written to
resources/output/(the folder will be created automatically).
- Clone the repository (or copy the files into a new directory).
git clone <repository‑url> groupdocs-editor-java-edit-word-document-programmatically cd groupdocs-editor-java-edit-word-document-programmatically
- Place your license file (
GroupDocs.Editor.lic) in the root directory (optional but recommended). - Prepare the input DOCX
-
Edit
resources/input/sample.docxor put your own DOCX file there. -
This document contains:
- The placeholder text
SAMPLE DOCUMENTfor programmatic replacement. - A paragraph
Confidentialto demonstrate content removal. - A new paragraph
<p>Inserted by GroupDocs.Editor</p>will be added during editing.
- The placeholder text
-
You can replace it with your own DOCX file, but ensure:
- It contains placeholder text (like
SAMPLE DOCUMENT) if you want to see the replacement effect. - Any paragraph you want to remove or edit programmatically is clearly identifiable.
- It contains placeholder text (like
-
- Tip: Keep the input file simple for testing purposes; formatting, images, and tables will be preserved during editing.
- Build the project
mvn clean compile
- Run the example
mvn exec:java
When you run the application:
- The text "SAMPLE DOCUMENT" is replaced with "Updated programmatically using GroupDocs.Editor".
- A new file named
edited_sample.docxis created. - The document layout, fonts, and images remain unchanged.
- The showcase uses relative paths (
resources/input/,resources/output/) so it works regardless of where the project is cloned. - Feel free to extend the example: add more replacements, work with other document types, or use advanced
EditOptions. - For full API reference, visit the official documentation: https://docs.groupdocs.com/editor/java/
This is a showcase project intended to illustrate a specific capability of the GroupDocs.Editor library. It is not a production‑ready application but follows Maven best practices and clean coding standards.