Word ドキュメントに改ページを挿入することで、Enter キーを連打することなく、好きな場所でページを終了し、新しいページを一度に開始することができます。今回は、Spire.Doc for Java ライブラリを使用して、Java で Word ドキュメントに改ページを挿入する方法を説明します。 特定の段落後に改ページを挿入する 特定のテキストの後に改ページする 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.12.2</version> </dependency> </dependencies> 特定の段落後に改ページを挿入する 特定の段落後に改ページを挿入する手順は以下の通りです。 Document のインスタンスを作成します。 Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。 Document.getSections().get(sectionIndex) メソッドを使用して、目的のセクションを取得します。 Section.getParagraphs().get(paragraphIndex) メソッドを使用して、目的の段落を取得します。 Paragraph.appendBreak(BreakType.Page_Break) メソッドを使用して、段落に改ページを追加します。 Document.saveToFile() メソッドを使用して、結果ドキュメントを保存します。 Java import com.spire.doc.Document; import com.spire.doc.Section; import com.spire.doc.documents.BreakType; import com.spire.doc.documents.Paragraph; import com.spire.doc.FileFormat; public class InsertPageBreakAfterParagraph { public static void main(String[] args){ //Documentのインスタンスを作成する Document document = new Document(); //Wordドキュメントを読み込む document.loadFromFile("C:/自分を見失う.docx"); //最初のセクションを取得する Section section = document.getSections().get(0); //セクションの3段落目を取得する Paragraph paragraph = section.getParagraphs().get(2); //段落に改ページを付加する paragraph.appendBreak(BreakType.Page_Break); //結果ドキュメントを保存する document.saveToFile("改ページの挿入.docx", FileFormat.Docx_2013); } } 特定のテキストの後に改ページする 特定のテキストの後に改ページを挿入する手順は次のとおりです。 Document のインスタンスを作成します。 Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。 Document.findString() メソッドを使用して、特定のテキストを検索します。 TextSelection.getAsOneRange() メソッドを使用して、検索されたテキストのテキスト範囲にアクセスします。 ParagraphBase.getOwnerParagraph() メソッドを使用して、テキスト範囲がある段落を取得します。 Paragraph.getChildObjects().indexOf() メソッドを使用して、段落内のテキスト範囲の位置インデックスを取得します。 Break のインスタンスを作成し、改ページを作成します。 Paragraph.getChildObjects().insert() メソッドを使用して、検索されたテキストの後に改ページを挿入します。 Document.saveToFile() メソッドを使用して、結果ドキュメントを保存します。 Java import com.spire.doc.Break; import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.documents.BreakType; import com.spire.doc.documents.Paragraph; import com.spire.doc.documents.TextSelection; import com.spire.doc.fields.TextRange; public class InsertPageBreakAfterText { public static void main(String[] args){ //Documentのインスタンスを作成する Document document = new Document(); //Wordドキュメントを読み込む document.loadFromFile("C:/自分を見失う.docx"); //特定のテキストを検索する TextSelection selection = document.findString("精神疾患", true, true); //検索されたテキストのテキスト範囲を取得する TextRange range = selection.getAsOneRange(); //テキスト範囲がある段落を取得する Paragraph paragraph = range.getOwnerParagraph(); //段落内のテキスト範囲の位置インデックスを取得する int index = paragraph.getChildObjects().indexOf(range); //改ページを作成する Break pageBreak = new Break(document, BreakType.Page_Break); //検索されたテキストの後に改ページを挿入する paragraph.getChildObjects().insert(index + 1, pageBreak); //結果ドキュメントを保存する document.saveToFile("テキストの後に改ページを挿入.docx", FileFormat.Docx_2013); } } 一時ライセンスを申請する 結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。