JavaでGroupDocs.Editorを使用してDOCXをDOCMに変換する
今日のスピーディなビジネス環境では、Javaコードから直接 convert docx to docm ができることにより、手作業の手間を大幅に削減し、互換性の問題を解消できます。四半期レポートの更新、契約書テンプレートのカスタマイズ、個別レターの生成など、プログラムによる編集は、ポイント&クリックツールが持つ速度と信頼性の欠如を補います。このガイドでは、DOCX ファイルの読み込み、コンテンツのプログラム的な変更、そして GroupDocs.Editor for Java を使用して DOCM を含む複数の一般的な形式で保存する手順を解説します。
Quick Answers
- What library lets me edit Word docs in Java? GroupDocs.Editor for Java.
- Can I replace text automatically? Yes – use the HTML markup API to search and replace strings.
- Which formats can I export to? DOCM, RTF, plain‑text, and more.
- Do I need a license for development? A free trial works for testing; a commercial license is required for production.
- Is it compatible with Maven projects? Absolutely – just add the repository and dependency.
What is “edit word document java”?
Editing a Word document from Java means loading a .docx file into memory, manipulating its content (text, images, tables, etc.) via the API, and then writing the updated file back to disk or a stream. GroupDocs.Editor abstracts the complex Office Open XML format, exposing a simple HTML‑based editing model.
Why use GroupDocs.Editor to edit word document java?
- No Microsoft Office dependency – works on any server or container.
- High fidelity – retains original layout, styles, and embedded objects.
- Multiple output formats – switch between DOCX, DOCM, RTF, TXT with a single call.
- Scalable – suitable for batch processing of large document sets.
Prerequisites
- Java 8+ and a build tool (Maven or Gradle).
- Access to the GroupDocs.Editor for Java library (version 25.3 or later).
- Basic familiarity with Java and Maven dependency management.
Setting Up GroupDocs.Editor for Java
Installing via Maven
Add the GroupDocs repository and dependency to your pom.xml:
<repositories>
<repository>
<id>repository.groupdocs.com</id>
<name>GroupDocs Repository</name>
<url>https://releases.groupdocs.com/editor/java/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-editor</artifactId>
<version>25.3</version>
</dependency>
</dependencies>
Direct Download
Alternatively, download the latest JAR from the GroupDocs.Editor for Java releases page.
License Acquisition
Start with a free trial to explore the API. For production workloads, obtain a temporary or full license from the GroupDocs portal.
Basic Initialization and Setup
Create an Editor instance that points to your source DOCX file:
import com.groupdocs.editor.Editor;
import com.groupdocs.editor.options.WordProcessingLoadOptions;
String inputFilePath = "YOUR_DOCUMENT_DIRECTORY/sample.docx";
Editor editor = new Editor(inputFilePath, new WordProcessingLoadOptions());
Now you’re ready to load, edit, and save documents.
How to convert docx to docm using GroupDocs.Editor
The conversion process is straightforward: load the DOCX, edit the HTML if needed, and then save using the Docm format. The steps below reuse the code blocks already introduced, so you won’t need to write any additional code.
Step 1: Load the Document
Overview: Loading gives you an EditableDocument object that you can manipulate.
import com.groupdocs.editor.Editor;
import com.groupdocs.editor.EditableDocument;
String inputFilePath = "YOUR_DOCUMENT_DIRECTORY/sample.docx";
Editor editor = new Editor(inputFilePath, new WordProcessingLoadOptions());
EditableDocument defaultWordProcessingDoc = editor.edit();
Step 2: (Optional) Edit the Content
If you need to replace placeholders or customize the template, modify the embedded HTML.
String allEmbeddedInsideString = defaultWordProcessingDoc.getEmbeddedHtml();
String modifiedContent = allEmbeddedInsideString.replace("Subtitle", "Edited subtitle");
Step 3: Save as DOCM
Configure the save options for the DOCM format and write the result to a file or a stream.
import com.groupdocs.editor.options.WordProcessingSaveOptions;
import com.groupdocs.editor.formats.WordProcessingFormats;
WordProcessingSaveOptions docmSaveOptions = new WordProcessingSaveOptions(WordProcessingFormats.Docm);
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
String outputDocmPath = "YOUR_OUTPUT_DIRECTORY/editedDoc.docm";
try (OutputStream outputStream = new ByteArrayOutputStream()) {
// Create a new EditableDocument from the (possibly) modified HTML
EditableDocument editedDocDocm = EditableDocument.fromMarkup(modifiedContent, null);
editor.save(editedDocDocm, outputStream, docmSaveOptions);
// If you need a physical file, write the stream to disk here
}
Pro tip: Dispose of
EditableDocumentandEditorobjects as soon as you’re done to free native resources.
Save Document as RTF
Overview: After editing, you can export to Rich Text Format.
import com.groupdocs.editor.options.WordProcessingSaveOptions;
import com.groupdocs.editor.formats.WordProcessingFormats;
String outputRtfPath = "YOUR_OUTPUT_DIRECTORY/editedDoc.rtf";
WordProcessingSaveOptions rtfSaveOptions = new WordProcessingSaveOptions(WordProcessingFormats.Rtf);
EditableDocument editedDocRtf = EditableDocument.fromMarkup(modifiedContent, null);
editor.save(editedDocRtf, outputRtfPath, rtfSaveOptions);
editedDocRtf.dispose();
editor.dispose();
Save Document as Plain Text
Overview: Exporting to plain text is useful for content indexing or simple data extraction.
import com.groupdocs.editor.options.TextSaveOptions;
import java.nio.charset.StandardCharsets;
TextSaveOptions textSaveOptions = new TextSaveOptions();
textSaveOptions.setEncoding(StandardCharsets.UTF_8);
textSaveOptions.setPreserveTableLayout(true);
String outputTxtPath = "YOUR_OUTPUT_DIRECTORY/editedDoc.txt";
editor.save(editedDocTxt, outputTxtPath, textSaveOptions);
Practical Applications
- Automate report generation – Pull data from databases, replace placeholders, and output a polished DOCX, DOCM, or RTF report.
- Customize word template – Dynamically fill marketing or legal templates based on user input.
- Export word to txt – Extract raw text for search indexing, analytics, or further processing.
- Replace text docx java – Use the HTML markup API to perform bulk find‑and‑replace operations across many documents.
Performance Considerations
- Dispose of
EditableDocumentandEditorobjects as soon as you’re done to free native resources. - For very large files, process sections in chunks or use streaming APIs to keep memory usage low.
- Prefer
StringBuilderor efficient regex when performing bulk text replacements.
Common Issues and Solutions
| Issue | Solution |
|---|---|
| File not found / access denied | Verify the absolute path and ensure the Java process has read/write permissions. |
| Out‑of‑memory errors on big docs | Increase JVM heap (-Xmx) or split the document into smaller parts before editing. |
| Formatting lost after replace | Use the HTML markup API carefully; avoid replacing markup tags themselves. |
| License not applied | Call License license = new License(); license.setLicense("path/to/license.file"); before creating Editor. |
Frequently Asked Questions
Q: Can I edit password‑protected Word files?
A: Yes. Load the document with WordProcessingLoadOptions that include the password, then proceed as usual.
Q: Does GroupDocs.Editor support macros in DOCM files?
A: The library preserves macros but does not execute them. You can save a DOCM file with existing macros intact.
Q: How do I handle images embedded in the document?
A: Images are kept as part of the HTML markup. You can replace the <img> tags or add new ones using standard HTML.
Q: Is it possible to convert directly to PDF?
A: GroupDocs.Editor focuses on editing; for PDF conversion, combine it with GroupDocs.Conversion after saving the edited DOCX.
Q: What versions of Java are supported?
A: Java 8 and newer are fully supported.
Conclusion
You now have a complete, end‑to‑end workflow for convert docx to docm using GroupDocs.Editor. By loading a DOCX, programmatically modifying its HTML, and exporting to formats like RTF, DOCM, or plain text, you can automate countless document‑centric tasks in your Java applications. Explore additional features such as spell‑checking, track changes, or integration with GroupDocs.Conversion to further extend your solution.
Last Updated: 2026-03-20
Tested With: GroupDocs.Editor 25.3 for Java
Author: GroupDocs