0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Javaで複数のWord文書を1つのWord文書に結合する方法

Last updated at Posted at 2023-04-21

報告書や プレゼンテーションを作成する際に、さまざまなソースからのデータを1つの文書に統合することが有益な場合があります。より包括的で長い文書を作成するためには、複数の別々のWord文書からデータを1つのまとまったファイルに統合する必要がある場合があります。この記事では、無料のFree Spire.Doc for Javaを使用して、2つのWord文書を1つのWord文書に統合する方法を紹介します。

【依存関係の追加】

この方法は、無料のFree Spire.Doc for Javaが必要ですので、先にjarファイルをインポートしてください。

1. Maven

Maven を使用している場合、プロジェクトの pom.xml ファイルに以下のコードを追加することで、簡単にアプリケーションに JAR ファイルをインポートすることができます。

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc.free</artifactId>
        <version>5.2.0</version>
    </dependency>
</dependencies>

2. 公式サイトよりJarファイルをダウンロード

まず、Free Spire.Doc for Javaの公式サイトよりzipファイルをダウンロードします。zipファイルを解凍し、libフォルダの下にあるSpire.Doc.jarファイルを依存関係としてプロジェクトにインポートしてください。

2つのWordドキュメントを1つに統合する

Free Spire.Doc for Javaでは、他の文書のコンテンツ全体を新しいページで目的の文書に挿入する Document.insertTextFromFile() メソッドを用意しています。ファイル全体を挿入して文書を統合する詳細な手順は以下の通りです。

  • Document のオブジェクトを作成する。
  • Document.loadFromFile() メソッドを使用してWord文書を読み込みます。
  • Document.insertTextFromFile() メソッドを使用して、読み込んだ文書に別のWord文書を完全に挿入します。
  • Document.saveToFile() メソッドで結果文書を保存します。

Java

import com.spire.doc.*;

public class merge {
    public static void main(String[] args) {

        //Documentのオブジェクトを作成する
        Document doc = new Document();

        //Word文書を読み込む
        doc.loadFromFile("英語の問題.docx");

        //別のWord文書をその文書に完全に挿入する
        doc.insertTextFromFile( "定義を超えた意味.docx", FileFormat.Auto);

        //結果文書を保存する
        String output = "マージ.docx";
        doc.saveToFile(output, FileFormat.Docx_2010);
    }
}

2つのWordドキュメントを1つに統合する

Word文書の内容をコピーして統合する

同じスタイルを維持したまま、新しいページを始めることなく文書を結合するために、他の文書の内容をクローンして文書の末尾に付加することができます。コンテンツのコピーによる文書の結合の詳しい手順は以下の通りです。

  • Document のオブジェクトを2つ作成します。
  • Document.loadFromFile() メソッドを使用して、2 つのWord文書を読み込みます。
  • 2番目の文書をループして、Document.getSections() メソッドを使用してすべてのセクションを取得します。さらに、Section.getBod().getChildObjects() メソッドを使用して、すべてのセクションをループして、その子オブジェクトを取得します。
  • Document.getLastSection() メソッドを使用して最初の文書の最後のセクションを取得し、Body.getChildObjects().add() メソッドを使用して最初の文書の最終セクションに子オブジェクトを追加します。
  • Document.saveToFile() メソッドを使用して、結果文書を保存します。

Java

import com.spire.doc.*;

public class copy {
    public static void main(String[] args) {

        //Documentのオブジェクトを2つ作成する
        Document document1 = new Document();
        Document document2 = new Document();

        //2つのWord文書を読み込む
        document1 .loadFromFile("英語の問題.docx");
        document2.loadFromFile("定義を超えた意味.docx");

        //2番目の文書をループして、すべてのセクションを取得する
        for (Object sectionObj : (Iterable) document2.getSections()) {
            Section sec=(Section)sectionObj;
            //2番目の文書のセクションをループして、その子オブジェクトを取得する
            for (Object docObj :(Iterable ) sec.getBody().getChildObjects()) {
                DocumentObject obj=(DocumentObject)docObj;

                //最初の文書の最終セクションを取得する
                Section lastSection = document1.getLastSection();

                //最初の文書の最終セクションに子オブジェクトを追加する
                Body body = lastSection.getBody();
                body.getChildObjects().add(obj.deepClone());
            }
        }

        //結果文書を保存する
        document1.saveToFile("マージ結果.docx", FileFormat.Docx_2013);
    }
}

Word文書の内容をコピーして統合する

追加オプションでWord文書を統合する

他の文書からコンテンツを挿入または追加する場合、Document.setKeepSameFormat(true) メソッドを使用して、元の文書と同じ書式を維持します。ここからが手順です。

  • Document のオブジェクトを2つ作成します。
  • Document.loadFromFile() メソッドで2つのWord文書を読み込みます。
  • Document.setKeepSameFormat(true) メソッドを使用して、元文書の書式を同じにするように設定します。
  • 元文書の各セクションをコピー先文書にコピーします。
  • Document.saveToFile() メソッドを使用して、結果文書を保存します。

Java

import com.spire.doc.*;

public class keepFormat {
    public static void main(String[] args) {

        //Documentのオブジェクトを作成する
        Document document1 = new Document();
        //元の文書を読み込む
        document1.loadFromFile("背景色.docx");

        //Documentの別のオブジェクトを作成する
        Document document2 = new Document();
        //目的の文書を読み込む
        document2.loadFromFile("背景画像.docx");

        //元文書の書式を維持する
        document1.setKeepSameFormat(true);

        //元文書のセクションを目的文書にコピーする
        for (Object sectionObj : document1.getSections()) {
            Section section=(Section)sectionObj;
            document2.getSections().add(section.deepClone());
        }

        //結果文書を保存する
        document2.saveToFile("書式を維持したままのマージ.docx", FileFormat.Docx_2013);
    }
}

追加オプションでWord文書を統合する

以上、Word文書を結合する方法について説明しました。 Spire.Doc for Javaは、その他にも多くの文書処理機能をサポートしています。文書処理についてもっと詳しく知りたい方は、Spire.Doc for Javaチュートリアルをご覧ください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?