Word ドキュメントのページ番号は、ページの順序とページ数を示すために各ページに付けられます。ページ番号によって、ドキュメント作成者は文書の内容を管理しやすくなり、ユーザーはドキュメント内の特定の内容をすばやく見つけることができるため、閲覧速度と閲覧体験が向上します。この記事では、Spire.Doc for Java を使って Word ドキュメントにページ番号を追加する方法を紹介します。 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>11.6.0</version> </dependency> </dependencies> Word ドキュメントにページ番号を追加する Word ドキュメントのページ番号は、特定のタイプのフィールドを使用して表示されます。例えば、Page フィールドは現在のページのページ番号を表示し、NumPages フィールドはドキュメント内の総ページ数を表示し、SectionPages フィールドはセクション内の総ページ数を表示します。 Spire.Doc for Java には、Paragraph.appendField(String fieldName, FieldType fieldType) メソッドがあり、Page フィールド (FieldType.Field_Page)、NumPages フィールド (FieldType.Field_Num_Pages)、SectionPages フィールド (FieldType.Field_Section_Pages) など、さまざまなタイプのフィールドをWord文書に追加できます。 以下の手順では、Spire.Doc for Java を使用して、Word ドキュメントのフッターに Page フィールドと NumPages フィールドを追加し、現在のページ番号とドキュメント内の総ページ数を表示する方法を説明します。 Document のオブジェクトを作成します。 Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。 Document.getSections().get() メソッドを使用して最初のセクションを取得します。 Section.getHeadersFooters().getFooter() メソッドを使用して、最初のセクションのフッターを取得します。 フッターに段落を追加し、Paragraph.appendField(String fieldName, FieldType fieldType) メソッドを使用して、段落に Page フィールドと NumPages フィールドを追加します。 Document.saveToFile() メソッドを使用して、ドキュメントを保存します。 Java import com.spire.doc.Document; import com.spire.doc.FieldType; import com.spire.doc.HeaderFooter; import com.spire.doc.Section; import com.spire.doc.documents.HorizontalAlignment; import com.spire.doc.documents.Paragraph; public class addPageNumbersWholeDocument { public static void main(String[] args) { //Documentのオブジェクトを作成する Document document = new Document(); //Wordドキュメントを読み込む document.loadFromFile("サンプル.docx"); //ドキュメントの最初のセクションを取得する Section section = document.getSections().get(0); //最初のセクションのフッターを取得する HeaderFooter footer = section.getHeadersFooters().getFooter(); //フッターにPageフィールドとNumPagesフィールドを追加し、ページ番号フォーマットを設定する Paragraph footerParagraph = footer.addParagraph(); footerParagraph.appendField("ページ番号", FieldType.Field_Page); footerParagraph.appendText(" / "); footerParagraph.appendField("ページ数", FieldType.Field_Num_Pages); footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); footerParagraph.getStyle().getCharacterFormat().setFontSize(16); //ドキュメントを保存する document.saveToFile("ドキュメントのページ番号.docx"); } } 各セクションのページ番号の追加と再配置 ページ番号の再配置を行うと、前のセクションのページ番号から続けるのではなく、各セクションの特定の番号からページ番号を開始することができます。 Word ドキュメントの各セクションでページ番号付けを再配置するには、ドキュメント内のすべてのセクションをループし、各セクションに Page フィールドと SectionPages フィールドを追加する必要があります。そして、Section.getPageSetup().setRestartPageNumbering(true) メソッドを使用してページ番号付けの再配置を有効にし、Section.getPageSetup().setPageStartingNumber(int value) メソッドを使用して各セクションの開始ページ番号を設定します。 Document のオブジェクトを作成します。 Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。 ドキュメント内のセクションをループします。 Paragraph.appendField(String fieldName, FieldType fieldType) メソッドを呼び出して、各セクションに Page フィールドと SectionPages フィールドを追加します。 Section.getPageSetup().setRestartPageNumbering(true) メソッドを呼び出して、ページ番号の再配置を有効にし、Section.getPageSetup().setPageStartingNumber(int value) メソッドを呼び出して、各セクションの開始ページ番号を設定します。 Document.saveToFile() メソッドを使用してドキュメントを保存します。 Java import com.spire.doc.Document; import com.spire.doc.FieldType; import com.spire.doc.HeaderFooter; import com.spire.doc.documents.HorizontalAlignment; import com.spire.doc.documents.Paragraph; public class addPageNumbersEachSection { public static void main(String[] args) { //Documentのオブジェクトを作成する Document document = new Document(); //Wordドキュメントを読み込む document.loadFromFile("サンプル.docx"); //ドキュメントのセクション数を取得する int s = document.getSections().getCount(); //ドキュメントのセクションをループする for (int i = 0; i < s; i++) { //フッターにPageとSectionPagesフィールドを追加し、ページ番号の書式を設定する HeaderFooter footer = document.getSections().get(i).getHeadersFooters().getFooter(); Paragraph footerParagraph = footer.addParagraph(); footerParagraph.appendField("ページ番号", FieldType.Field_Page); footerParagraph.appendText(" / "); footerParagraph.appendField("ページ数", FieldType.Field_Section_Pages); footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); footerParagraph.getStyle().getCharacterFormat().setFontSize(16); //ページ番号の開始ページを再設定し、開始ページ番号を設定する if (i == s-1) break; else { document.getSections().get(i + 1).getPageSetup().setRestartPageNumbering(true); document.getSections().get(i + 1).getPageSetup().setPageStartingNumber(1); } } //ドキュメントを保存する document.saveToFile("各セクションのページ番号.docx"); document.dispose(); } } 特定のセクションにページ番号を追加する デフォルトでは、セクションのフッターにページ番号を挿入すると、後続のセクションは自動的に前のセクションにリンクし、ページ番号を表示し続けます。特定のセクションだけにページ番号を追加したい場合は、後続のセクションと前のセクションのリンクを解除し、後続のセクションのフッターの内容を削除する必要があります。詳しい手順は以下の通りです。 Document のオブジェクトを作成します。 Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。 Document.getSections().get() メソッドを使用して、ドキュメントの 2 番目のセクションを取得します。 Section.getPageSetup().setRestartPageNumbering(true) メソッドを呼び出してページ番号の再配置を有効にし、Section.getPageSetup().setPageStartingNumber(int value) メソッドを呼び出してセクションの開始ページ番号を設定します。 Paragraph.appendField(String fieldName, FieldType fieldType) メソッドを呼び出して、Page フィールドと SectionPages フィールドをセクションに追加します。 Section.getHeadersFooters().getFooter().setLinkToPrevious(false) メソッドを使用して、2 番目のセクションから後続のセクションのリンクを解除します。 後続セクションのフッターの内容を削除する。 Doucment.saveToFile() メソッドを使用してドキュメントを保存します。 Java import com.spire.doc.Document; import com.spire.doc.FieldType; import com.spire.doc.HeaderFooter; import com.spire.doc.Section; import com.spire.doc.documents.HorizontalAlignment; import com.spire.doc.documents.Paragraph; public class addPageNumbersSpecificSection { public static void main(String[] args) { //Documentのオブジェクトを作成する Document document = new Document(); //Wordドキュメントを読み込む document.loadFromFile("サンプル.docx"); //ドキュメントの2番目のセクションを取得する Section section = document.getSections().get(1); //2番目のセクションのフッターを取得する HeaderFooter footer = section.getHeadersFooters().getFooter(); //2番目のセクションからページを再配置し、開始ページ番号を1に設定する section.getPageSetup().setRestartPageNumbering(true); section.getPageSetup().setPageStartingNumber(1); //フッターにPageフィールドとSectionPagesフィールドを追加し、ページ番号の書式を設定する Paragraph footerParagraph = footer.addParagraph(); footerParagraph.appendField("ページ番号", FieldType.Field_Page); footerParagraph.appendText(" / "); footerParagraph.appendField("ページ数", FieldType.Field_Section_Pages); footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); footerParagraph.getStyle().getCharacterFormat().setFontSize(16); //後続のセクションと2番目のセクションのリンクを解除し、後続のセクションのフッターの内容を削除する document.getSections().get(2).getHeadersFooters().getFooter().setLinkToPrevious(false); for (int i = 2; i < document.getSections().getCount(); i++) { document.getSections().get(i).getHeadersFooters().getFooter().getChildObjects().clear(); document.getSections().get(i).getHeadersFooters().getFooter().addParagraph(); } //ドキュメントを保存する document.saveToFile("特定のセクションのページ番号.docx"); } } 一時ライセンスを申請する 結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。