JavaでGroupDocs.Editorを使用してdocxをdocmに変換する

最新のエンタープライズワークフローでは、convert docx to docm をプログラムで実行し、レポート生成、契約書のパーソナライズ、テンプレート駆動のコミュニケーションを自動化できます。GroupDocs.Editor for Java を使用すれば、サーバーに Microsoft Office をインストールする必要がなく、レイアウトの忠実性を保ちつつ、docx のテキスト置換、Word を txt にエクスポート、または word を rtf に変換する機能を単一の軽量 API で利用できます。本ガイドでは、DOCX ファイルの読み込み、必要に応じた HTML 編集、そして結果を DOCM やその他の一般的な形式で保存する手順を解説します。

クイック回答

  • JavaでWordドキュメントを編集できるライブラリは何ですか? GroupDocs.Editor for Java.
  • テキストを自動的に置換できますか? はい – HTML マークアップ API を使用してドキュメント全体の文字列を検索・置換できます。
  • どの形式にエクスポートできますか? DOCM、RTF、プレーンテキスト(TXT)など。
  • 開発にライセンスは必要ですか? 無料トライアルでテスト可能です。商用環境では有償ライセンスが必要です。
  • Maven プロジェクトと互換性がありますか? 完全に対応しています – リポジトリと依存関係を追加するだけです。

「edit word document java」とは何ですか?

メモリに .docx ファイルをロードし、API を介してコンテンツ(テキスト、画像、テーブル、マクロ)を変更し、更新されたファイルをディスクまたはストリームに書き戻すことが「edit word document java」の意味です。GroupDocs.Editor は Office Open XML 形式を抽象化し、シンプルな HTML ベースの編集モデルを提供するため、ドキュメントをウェブページのように扱えます。

なぜ GroupDocs.Editor を使って word document java を編集するのか?

GroupDocs.Editor を使用すれば、convert docx to docm を実行し、Microsoft Office をインストールせずに大量処理が可能です。30 以上の入力および出力形式 をサポートし、数百ページのファイルでもヒープメモリ 200 MB 未満で処理でき、典型的な 8 コアサーバー上で 1 分間に 150 ドキュメントという速度で batch process word docs を実行できます。このライブラリは DOCM ファイルのマクロを保持し、元のスタイルを維持し、あらゆる Java 互換プラットフォームで動作します。

前提条件

  • Java 8 以降とビルドツール(Maven または Gradle)。
  • GroupDocs.Editor for Java ライブラリへのアクセス(バージョン 25.3 以降)。
  • Java と Maven の依存関係管理に関する基本的な知識。

GroupDocs.Editor for Java の設定

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>

直接ダウンロード

Alternatively, download the latest JAR from the GroupDocs.Editor for Java リリースページ.

ライセンス取得

Start with a free trial to explore the API. For production workloads, obtain a temporary or full license from the GroupDocs portal.

基本的な初期化と設定

Editor is the core class that provides loading, editing, and saving capabilities for Word documents. 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.

GroupDocs.Editor を使用して docx を docm に変換する方法

Load the DOCX, optionally modify its HTML, and then save the result as a DOCM file. The conversion requires only three API calls: instantiate Editor, load the document into an EditableDocument, and invoke save with Docm options. After saving, you can further process the DOCM, such as uploading it to a document management system or attaching it to an email, without losing any embedded macros or formatting.

手順 1: ドキュメントをロードする

EditableDocument represents a Word file that can be edited as HTML. Loading returns this object, which you can then 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();

手順 2: (オプション)コンテンツを編集する

If you need to replace placeholders, update the embedded HTML using standard string‑replace or regex techniques.

String allEmbeddedInsideString = defaultWordProcessingDoc.getEmbeddedHtml();
String modifiedContent = allEmbeddedInsideString.replace("Subtitle", "Edited subtitle");

手順 3: 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 EditableDocument and Editor objects as soon as you’re done to free native resources and keep memory usage low.

ドキュメントを RTF として保存

Exporting to Rich Text Format is useful when downstream systems only understand RTF. The same EditableDocument can be saved with RTF options.

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();

ドキュメントをプレーンテキストとして保存

Plain‑text output is ideal for indexing, analytics, or feeding content into search engines.

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);

実用的な活用例

  1. レポート生成の自動化 – データベースからデータを取得し、プレースホルダーを置換して、洗練された DOCX、DOCM、または RTF レポートを出力します。
  2. Word テンプレートのカスタマイズ – ユーザー入力に基づいてマーケティングや法務テンプレートを動的に埋め込みます。
  3. Word を txt にエクスポート – 検索インデックス、分析、またはさらなる処理のために生テキストを抽出します。
  4. docx のテキスト置換 – HTML マークアップ API を使用して、単一のバッチジョブで多数のドキュメントに対して大量の検索置換を実行します。

パフォーマンス上の考慮点

  • Dispose of EditableDocument and Editor objects promptly to free native resources.
  • For very large files, process sections in chunks or use streaming APIs to keep memory usage under 250 MB.
  • Prefer StringBuilder or compiled regular expressions when performing bulk text replacements to minimise CPU overhead.

よくある問題と解決策

The License class applies your GroupDocs.Editor license file to enable full functionality.

問題解決策
ファイルが見つからない / アクセスが拒否されました絶対パスを確認し、Java プロセスに読み書き権限があることを確認してください。
大きなドキュメントでのメモリ不足エラーJVM ヒープを増やす(-Xmx2g)か、編集前にドキュメントを小さな部分に分割してください。
置換後に書式が失われるHTML マークアップ API を慎重に使用し、マークアップタグ自体を置換しないでください。
ライセンスが適用されていないLicense license = new License(); license.setLicense("path/to/license.file"); を Editor 作成前に呼び出してください。

よくある質問

Q: パスワードで保護された Word ファイルを編集できますか?
A: はい。パスワードを含む WordProcessingLoadOptions でドキュメントをロードし、通常通り操作できます。

Q: GroupDocs.Editor は DOCM ファイルのマクロをサポートしていますか?
A: ライブラリはマクロを保持しますが、実行はしません。既存のマクロをそのまま保持した DOCM ファイルを保存できます。

Q: ドキュメントに埋め込まれた画像はどう扱いますか?
A: 画像は HTML マークアップの一部として保持されます。標準的な HTML を使用して <img> タグを置換または新規追加できます。

Q: 直接 PDF に変換することは可能ですか?
A: GroupDocs.Editor は編集に特化しています。PDF 変換が必要な場合は、編集後の DOCX を GroupDocs.Conversion と組み合わせて実行してください。

Q: サポートされている Java のバージョンは何ですか?
A: Java 8 以降が完全にサポートされています。

結論

You now have a complete, end‑to‑end workflow to convert docx to docm using GroupDocs.Editor. By loading a DOCX, optionally editing its HTML, and exporting to DOCM, RTF, or plain‑text, you can automate countless document‑centric tasks in Java applications. Explore additional features such as spell‑checking, track changes, or integration with GroupDocs.Conversion to further extend your solution.


最終更新日: 2026-09-16
テスト環境: GroupDocs.Editor 25.3 for Java
作者: GroupDocs

関連チュートリアル