この記事に従って、Java を使用して Word ドキュメントをセクションに分割 します。開発環境を設定するための詳細、アプリケーションの手順の一覧、Java を使用して Word ドキュメントをセクションに分割 するためのサンプル コードが記載されています。すべてのセクションを個別の Word ファイルに変換しながら、セクションをフィルター処理するオプションについて説明します。
Java を使用して Word 文書をセクションに分割する手順
- Aspose.Words for Java を使用して Word ファイルをセクションに分割するための環境を設定します
- ソースWordファイルをDocumentオブジェクトに読み込み、セクションを分離します。
- 読み込まれたWordファイル内のすべてのセクションを反復処理します
- 新しい空のWordファイルを作成し、デフォルトのセクションコレクションをクリアします。
- 反復内の現在のセクションを複製し、新しいWord文書にインポートします。
- Save Wordファイルに一意の名前を付け、残りのセクションに対してこのプロセスを繰り返します。
これらの手順は、Java を使用して Word 文書をセクションに分割する方法 をまとめたものです。ソースの Word ファイルを読み込み、そのすべてのセクションを反復処理し、各セクションをディープクローンして新しい Word ファイルで使用します。新しい Word ファイルを作成し、そのセクション コレクションをクリアして、クローンされたセクションを追加します。
Java を使用して Word 文書をセクションに分割するコード
import com.aspose.words.*; | |
public class Main | |
{ | |
public static void main(String[] args) throws Exception // Divide Word file by sections in Java | |
{ | |
// Set the licenses | |
new License().setLicense("License.lic"); | |
// Load the source Word document | |
Document doc = new Document("Sections.docx"); | |
// Iterate through all the sections | |
for (int iSectionCounter = 0; iSectionCounter < doc.getSections().getCount(); iSectionCounter++) | |
{ | |
// Clone the current section | |
Section section = doc.getSections().get(iSectionCounter).deepClone(); | |
// Create a new empty Word document | |
Document newDoc = new Document(); | |
// Remove the default sections in the Word file | |
newDoc.getSections().clear(); | |
// Import the section to the new document | |
Section newSection = (Section)newDoc.importNode(section, true); | |
newDoc.getSections().add(newSection); | |
// Save the section as a separate Word file | |
newDoc.save("Word_Section_ " + iSectionCounter + ".docx"); | |
} | |
System.out.println("Word file split by sections successfully"); | |
} | |
} |
このコード スニペットは、Java を使用して Word 文書をセクションに分割する方法 を説明しています。本文の内容、ノード タイプ、書式設定などをテストするなど、さまざまなメソッドとプロパティを使用してセクションをフィルターできます。セクションはインデックスを使用してフィルターでき、必要に応じて新しく作成された Word ファイルに複数のセクションを追加できます。
このチュートリアルでは、Word ファイルをセクションに分割する方法を説明しました。Word ファイルに表を挿入する場合は、Java を使って Word 文書に表を作成する方法 の記事を参照してください。