Microsoft Word では、段落のテキスト配置を左・中央・右・両端揃え・分散に設定することができます。この記事では、Spire.Doc for Java を使用して段落の配置を設定する方法について説明します。 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>http://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>10.7.10</version> </dependency> </dependencies> Word ドキュメントに段落のアライメントを設定する Spire.Doc for Java では、段落書式を扱うために ParagraphFormat クラスが用意されています。Paragraph.getFormat() メソッドを使用して ParagraphFormat クラスのオブジェクトを取得し、ParagraphFormat.setHorizontalAlignment() メソッドを使用して段落のテキスト配置を設定することができます。 Word ドキュメントで段落のテキスト配置を設定する手順は次のとおりです。 Document クラスのインスタンスを作成します。 Document.addSection() メソッドを使用して、ドキュメントにセクションを追加します。 Section.addParagraph() メソッドを使用してセクションに段落を追加し、Paragraph.appendText() メソッドを使用して段落にテキストを付加します。 Paragraph.getFormat() メソッドを使用して、ParagraphFormat クラスのオブジェクトを取得します。 ParagraphFormat.setHorizontalAlignment() メソッドを使用して、段落のテキストアライメントを設定します。 Document.saveToFile() メソッドを使用して、ドキュメントを保存します。 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.formatting.ParagraphFormat; public class setParagraphAlignment { public static void main(String[] args) { //Documentクラスのインスタンスを作成する Document document = new Document(); //セクションを追加する Section section = document.addSection(); //段落を追加し、左揃えに設定する Paragraph para = section.addParagraph(); para.appendText("この段落の配置は、左揃えに設定されています。"); ParagraphFormat format = para.getFormat(); format.setHorizontalAlignment(HorizontalAlignment.Left); //段落を追加し、中央揃えに設定する para = section.addParagraph(); para.appendText("この段落の配置は、中央揃えに設定されています。"); format = para.getFormat(); format.setHorizontalAlignment(HorizontalAlignment.Center); //段落を追加し、右揃えに設定する para = section.addParagraph(); para.appendText("この段落の配置は、右揃えに設定されています。"); format = para.getFormat(); format.setHorizontalAlignment(HorizontalAlignment.Right); //段落を追加し、両端揃えに設定する para = section.addParagraph(); para.appendText("この段落の配置は、両端揃えに設定されています。"); format = para.getFormat(); format.setHorizontalAlignment(HorizontalAlignment.Justify); //段落を追加し、分散に設定する。 para = section.addParagraph(); para.appendText("この段落の配置は、分散に設定されています。"); format = para.getFormat(); format.setHorizontalAlignment(HorizontalAlignment.Distribute); //ドキュメントを保存する document.saveToFile("段落の配置.docx", FileFormat.Docx_2013); } } 一時ライセンスを申請する 結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。