目次は、Word 文書内のフィールドで、文書内のすべてのレベルの見出しとそれに対応するページ番号を表示します。さらに、目次は文書の内容に変更が加えられた後に更新することができ、新しい文書の内容に従って新しい見出しとページ番号を生成することができます。したがって、目次は文書を編集するときにも、読むときにも大変便利であり、また、文書をよりプロフェッショナルなものにすることができます。この記事では、Spire.Doc for Java を使用して Word で目次を作成する方法を示します。 Java で Word 文書に既定の形式での目次を作成する 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>11.10.3</version> </dependency> </dependencies> Java で Word 文書に既定の形式での目次を作成する 目次を作成する前に、各見出しのレベルを設定する必要があります。Spire.Doc for Java では、Paragraph.appendTOC() メソッドを使用して段落に目次を挿入することができます。詳細な手順は以下の通りです。 Document クラスのオブジェクトを作成します。 Document.loadFromFile() メソッドを使用して、Word ドキュメントをロードします。 Document.addSection() メソッドを使用して、ドキュメントにセクションを追加します。 そして、Document.getSections().insert() メソッドを使用して、表紙セクションの後にそのセクションを挿入します。 Section.addParagraph() メソッドを使用して、セクションに段落を追加します。 Paragraph.appendTOC() メソッドを使用して、段落に目次を作成します。 Document.updateTableOfContents() メソッドを使用して、目次を更新して、ドキュメント内の見出しとそのページ番号を表示します。 Document.saveToFile() メソッドを使用して結果文書を保存します。 Java import com.spire.doc.Document; import com.spire.doc.Section; import com.spire.doc.documents.Paragraph; import com.spire.doc.fields.TableOfContent; public class insertTableOfContents { public static void main(String[] args) { //Documentクラスのオブジェクトを作成する Document doc = new Document(); //Word文書を読み込む doc.loadFromFile("Sample.docx"); //セクションを追加し、文書の表紙セクションの後に挿入する Section section = doc.addSection(); doc.getSections().insert(1, section); //セクションに段落を追加する Paragraph paragraph = section.addParagraph(); paragraph.appendText("目次rn"); paragraph.getStyle().getCharacterFormat().setFontSize(12f); //段落内に目次を作成する paragraph.appendTOC(2, 3); //目次を更新する doc.updateTableOfContents(); //結果文書を保存する doc.saveToFile("CreateTableOfContents.docx"); } } Java で Word 文書にカスタマ形式での目次を作成する Spire.Doc for Java では、TableOfContent クラスは Word 文書の目次を表します。目次に表示される部分は、TableOfContent クラスのインスタンスを初期化する際にスイッチを使用してカスタマイズすることができます。例えば、スイッチ "{o "1-3" n 1-2}" は、1から3レベルの見出しを表示し、1と2レベルの見出しのページ番号を省略することを意味します。詳細な手順は以下の通りです。 Document クラスのオブジェクトを作成します。 Document.loadFromFile() メソッドを使用して、Wordドキュメントをロードします。 Document.getSections().insert() メソッドを使用して、表紙セクションの後にセクションを挿入します。 Section.addParagraph() メソッドを使用して、セクションに段落を追加します。 TableOfContent クラスのインスタンスを作成して、1から3レベルの見出しを表示し、1と2レベルの見出しのページ番号を省略する目次を作成します。 Paragraph.getItems().add() メソッドを使用して、段落に目次を挿入します。 Paragraph.appendFieldMark() メソッドを使用して、目次フィールドを終了するためにフィールド区切りマークとフィールド終了マークを段落に挿入します。 Document.setTOC() メソッドを使用して、インスタンスを文書の目次として設定します。 Document.updateTableOfContents() メソッドを使用して、文書の現在の内容を表示するために目次を更新します。 Document.saveToFile() メソッドを使用して、結果文書を保存します。 Java *import com.spire.doc.Document; import com.spire.doc.Section; import com.spire.doc.documents.FieldMarkType; import com.spire.doc.documents.Paragraph; import com.spire.doc.fields.TableOfContent; public class insertTableOfContentsWithCustomizedStyle { public static void main(String[] args) { //Documentクラスのオブジェクトを作成する Document doc = new Document(); //Word文書を読み込む doc.loadFromFile("Sample.docx"); //最初のセクションの後にセクションを挿入し、そのセクションに段落を追加する Section section = doc.addSection(); doc.getSections().insert(1, section); Paragraph paragraph = section.addParagraph(); paragraph.getStyle().getCharacterFormat().setFontSize(12f); //TableOfContent クラスのインスタンスを初期化する TableOfContent toc = new TableOfContent(doc, "{o "1-3" n 1-2}"); //目次を挿入する paragraph.getItems().add(toc); //フィールドマークを挿入してフィールドを終了する paragraph.appendFieldMark(FieldMarkType.Field_Separator); paragraph.appendFieldMark(FieldMarkType.Field_End); //作成した目次を文書の目次として設定する doc.setTOC(toc); //目次を更新する doc.updateTableOfContents(); //結果文書を保存する doc.saveToFile("TableOfContentsWithCustomizedStyle.docx"); doc.dispose(); } } 一時ライセンスを申請する 結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。