Word ファイル形式は Microsoft 社によって考案され、次第にかけがえのないものとなってきました。私たちが目にした印刷物やデジタル出版物、報告書、パンフレット、履歴書のほとんどは Word で作成されたものです。この記事では、Spire.Doc for Java を使用して、Java で Word ドキュメントをプログラム的に作成する方法を学びます。 Spire.Doc for Java が提供する Document クラスは、ドキュメントモデルを表し、Word ドキュメントをゼロから作成したり、既存の Word ファイルを読み込んでさらに修正するために使用されます。Word ドキュメントは少なくとも1つのセクション(Section クラスで表される)を含んでいなければならず、各セクションは段落、表、ヘッダー、フッターなどの Word の基本要素のためのコンテナとなります。以下は、このチュートリアルで扱う主要なクラスとメソッドのリストです。 メンバー 説明 Document クラス Word ドキュメントモデルを表現する。 Section クラス Wordドキュメント内のセクションを表す。 Paragraph クラス セクション内の段落を表す。 ParagraphStyle クラス 段落に適用可能なフォントのフォーマット情報を定義する。 Section.addParagraph() メソッド セクションに段落を追加する。 Paragraph.appendText() メソッド 段落の末尾にテキストを追加する。 Paragraph.applyStyle() メソッド 段落にスタイルを適用する。 Document.saveToFile() メソッド ドキュメントを拡張子が .doc または .docx の Word ファイルに保存します。この方法は、PDF、XPS、HTML、PLC などへの保存にも対応しています。 Spire.Doc for Java をインストールします まず、Spire. Doc for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。 <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</artifactId> <version>10.7.4</version> </dependency> </dependencies> Java で簡単な Word ドキュメントを作成する 以下は、Spire.Doc for Java を使用して、いくつかの段落を含む簡単な Word ドキュメントを作成する手順です。 Document クラスのオブジェクトを作成します。 Document.addSection() を使用してセクションを追加します。 Section.getPageSetup().setMargins() メソッドを使用して、ページの余白を設定します。 Section.addParagraph() メソッドを使用して、セクションにいくつかの段落を追加します。 Paragraph.appendText() メソッドを使用して、段落にテキストを追加します。 ParagraphStyle クラスのオブジェクトを作成し、Paragraph.applyStyle() を使用して段落に適用します。 Document.saveToFile() メソッドを使用して、Word ドキュメントをファイルに保存します。 Java import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.Section; import com.spire.doc.documents.HorizontalAlignment; import com.spire.doc.documents.Paragraph; import com.spire.doc.documents.ParagraphStyle; import java.awt.*; public class createWordDocument { public static void main(String[] args) { //Documentクラスのオブジェクトを作成する Document doc = new Document(); //セクションを追加する Section section = doc.addSection(); //ページの余白を設定する section.getPageSetup().getMargins().setAll(40f); //タイトルとして段落を追加する Paragraph titleParagraph = section.addParagraph(); titleParagraph.appendText("Spire.Doc for Java の紹介"); //本文として2つの段落を追加する Paragraph bodyParagraph_1 = section.addParagraph(); bodyParagraph_1.appendText("Spire.Doc for Java は、Java アプリケーションが Microsoft Word に依存せずに Word 文書を" + "作成、変換、操作、および印刷できるようにする専門的な Word API です。"); Paragraph bodyParagraph_2 = section.addParagraph(); bodyParagraph_2.appendText("この多機能ライブラリを使用することで、開発者は、画像、ハイパーリンク、デジタル署名、" + "ブックマークと透かしの挿入、ヘッダーとフッターの設定、テーブルの作成、背景画像の" + "設定、脚注と文末脚注の追加など、さまざまなタスクを簡単に処理できます。"); //タイトル段落のスタイルを作成し、適用する ParagraphStyle style1 = new ParagraphStyle(doc); style1.setName("titleStyle"); style1.getCharacterFormat().setBold(true);; style1.getCharacterFormat().setTextColor(Color.BLUE); style1.getCharacterFormat().setFontName("Yu Mincho"); style1.getCharacterFormat().setFontSize(16f); doc.getStyles().add(style1); titleParagraph.applyStyle("titleStyle"); //本文の段落のスタイルを作成し、適用する ParagraphStyle style2 = new ParagraphStyle(doc); style2.setName("paraStyle"); style2.getCharacterFormat().setFontName("Yu Mincho"); style2.getCharacterFormat().setFontSize(12); doc.getStyles().add(style2); bodyParagraph_1.applyStyle("paraStyle"); bodyParagraph_2.applyStyle("paraStyle"); //段落の水平方向の配置を設定する titleParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); bodyParagraph_1.getFormat().setHorizontalAlignment(HorizontalAlignment.Justify); bodyParagraph_2.getFormat().setHorizontalAlignment(HorizontalAlignment.Justify); //最初の行のインデントを設定する bodyParagraph_1.getFormat().setFirstLineIndent(30) ; bodyParagraph_2.getFormat().setFirstLineIndent(30); //段落後の間隔を設定する titleParagraph.getFormat().setAfterSpacing(10); bodyParagraph_1.getFormat().setAfterSpacing(10); //ドキュメントをファイルに保存する doc.saveToFile("Wordドキュメント.docx", FileFormat.Docx_2013); doc.close(); } } 一時ライセンスを申請する 結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。