Word 文書におけるヘッダーとは、ページの上部に位置するテキストや画像のコンテンツのことで、フッターはページの下部に位置するものであるということです。 ヘッダーとフッターは、著作権、著者情報、ページ番号など、文書に関する重要な情報を表示するためによく使われます。また、文書をより専門的で美しいものにするために、文書を装飾するためにも使用されます。 ヘッダーとフッターは、文書の各ページまたは最初のページに挿入することができ、文書の奇数ページと偶数ページに異なるヘッダーとフッターを挿入することができます。 この記事では、Spire.Doc for 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.4</version>
    </dependency>
</dependencies>Word 文書にヘッダーとフッターを挿入する
Word 文書にヘッダーとフッターを挿入するには、まず Section.getHeadersFooters().getHeader() と Section.getHeadersFooters().getFooter() メソッドを使用してヘッダーとフッターを取得しておく必要があります。 そして、それらに段落を追加します。 最後に段落に画像、テキスト、ページ番号などを追加します。
以下に、ヘッダーとフッターの挿入方法の詳細を示します。
- Document クラスのオブジェクトを作成します。
 - Document.loadFromFIle() メソッドを使用して Word ドキュメントを読み込みます。
 - Document.getSections().get() メソッドを使用して、ドキュメントの最初のセクションを取得します。
 - カスタムの insertHeaderAndFooter() メソッドを使用して、セクションにヘッダーとフッターを挿入します。
 - Document.saveToFile() を使ってドキュメントを保存します。
 
- Java
 
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;
public class insertHeaderAndFooter1 {
    public static void main(String[] args) {
        //Documentクラスのオブジェクトを作成する
        Document document = new Document();
        //Wordドキュメントを読み込む
        document.loadFromFile("私たちは織り成す存在である.docx");
        //ドキュメントの最初のセクションを取得する
        Section section = document.getSections().get(0);
        //カスタムのメソッドinsertHeaderAndFooter()を呼び出し、ヘッダーとフッターを挿入する
        insertHeaderAndFooter(section);
        //ドキュメントを保存する
        document.saveToFile("ヘッダーとフッター.docx", FileFormat.Docx);
    }
    private static void insertHeaderAndFooter(Section section) {
        
        //セクションからヘッダーとフッターを取得する
        HeaderFooter header = section.getHeadersFooters().getHeader();
        HeaderFooter footer = section.getHeadersFooters().getFooter();
        //ヘッダーに段落を追加する
        Paragraph headerParagraph = header.addParagraph();
        //ヘッダーの段落にテキストを追加する
        TextRange text = headerParagraph.appendText("哲学\r私たちは織り成す存在である");
        text.getCharacterFormat().setFontName("Yu Gothic UI");
        text.getCharacterFormat().setFontSize(10);
        text.getCharacterFormat().setItalic(true);
        headerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        //ヘッダー段落の下部線のスタイルを設定する
        headerParagraph.getFormat().getBorders().getBottom().setBorderType(BorderStyle.Single);
        headerParagraph.getFormat().getBorders().getBottom().setLineWidth(1f);
        //フッターに段落を追加する
        Paragraph footerParagraph = footer.addParagraph();
        //フッター段落にField_PageとField_Num_Pagesフィールドを追加する
        footerParagraph.appendField("ページ番号", FieldType.Field_Page);
        footerParagraph.appendText(" / ");
        footerParagraph.appendField("ページ数", FieldType.Field_Num_Pages);
        footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        //フッター段落の上部線のスタイルを設定する
        footerParagraph.getFormat().getBorders().getTop().setBorderType(BorderStyle.Single);
        footerParagraph.getFormat().getBorders().getTop().setLineWidth(1f);
    }
}
Word 文書の最初のページにヘッダーとフッターを挿入する
最初のページにヘッダーとフッターを挿入するだけでよい場合もありますが、これも Spire.Doc for Java を使って行うことができます。 Section.getPageSetup().setDifferentFirstPageHeaderFooter() メソッドを使用して、最初のページのヘッダーとフッターを他のページと異なるものにし、最初のページにヘッダーとフッターを挿入することができます。
最初のページのみにヘッダーとフッターを挿入する詳しい手順は、以下の通りです。
- Document クラスのオブジェクトを作成します。
 - Document.loadFromFile() メソッドを使用して Word 文書を読み込みます。
 - Document.getSections().get() メソッドを使用して、ドキュメントの最初のセクションを取得します。
 - Document.getSections().get() メソッドを使用して、最初のページのヘッダーとフッターを他のページと異なるように設定します。
 - カスタムの insertHeaderAndFooterFirst() メソッドを使用して、最初のページにヘッダーとフッターを挿入します。
 - Document.saveToFile() メソッドを使用して、ドキュメントを保存します。
 
- Java
 
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class insertHeaderAndFooter {
    public static void main(String[] args) {
        //Documentクラスのオブジェクトを作成する
        Document document = new Document();
        //Wordドキュメントを読み込む
        document.loadFromFile("私たちは織り成す存在である.docx");
        //ドキュメントの最初のセクションを取得する
        Section section = document.getSections().get(0);
        //ドキュメントの最初のページのヘッダーとフッターを他のページと異なるように設定する
        section.getPageSetup().setDifferentFirstPageHeaderFooter(true);
        //カスタム insertHeaderAndFooterFirst() メソッドを呼び出して、最初のページにヘッダとフッタを挿入する
        insertHeaderAndFooterFirst(section);
        //ドキュメントを保存する
        document.saveToFile("最初のページのヘッダーとフッター.docx", FileFormat.Docx);
    }
    private static void insertHeaderAndFooterFirst(Section section) {
        //ドキュメントの最初のページのヘッダーとフッターを取得する
        HeaderFooter header = section.getHeadersFooters().getFirstPageHeader();
        HeaderFooter footer = section.getHeadersFooters().getFirstPageFooter();
        //ヘッダーに段落を追加する
        Paragraph headerParagraph = header.addParagraph();
        //ヘッダーの段落にテキストを追加する
        TextRange text = headerParagraph.appendText("哲学");
        text.getCharacterFormat().setFontName("Yu Gothic UI");
        text.getCharacterFormat().setFontSize(14);
        text.getCharacterFormat().setTextColor(Color.blue);
        text.getCharacterFormat().setItalic(true);
        headerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);
        //ヘッダー段落に画像を挿入し、その位置を設定する
        DocPicture headerPicture = headerParagraph.appendPicture("ヘッダー.png");
        headerPicture.setHorizontalAlignment(ShapeHorizontalAlignment.Left);
        headerPicture.setVerticalAlignment(ShapeVerticalAlignment.Top);
        //ヘッダー段落の下部線のスタイルを設定する
        headerParagraph.getFormat().getBorders().getBottom().setBorderType(BorderStyle.Single);
        headerParagraph.getFormat().getBorders().getBottom().setLineWidth(1f);
        //画像の文字列の折り返しスタイルを背面に設定する
        headerPicture.setTextWrappingStyle(TextWrappingStyle.Behind);
        //フッターに段落を追加する
        Paragraph footerParagraph = footer.addParagraph();
        //フッター段落にテキストを追加する
        TextRange text1 = footerParagraph.appendText("私たちは織り成す存在である");
        text1.getCharacterFormat().setFontName("Yu Gothic UI");
        text1.getCharacterFormat().setFontSize(14);
        text1.getCharacterFormat().setTextColor(Color.blue);
        text1.getCharacterFormat().setItalic(true);
        footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        //フッター段落の上部線のスタイルを設定する
        footerParagraph.getFormat().getBorders().getTop().setBorderType(BorderStyle.Single);
        footerParagraph.getFormat().getBorders().getTop().setLineWidth(1f);
    }
}
Word 文書の奇数ページと偶数ページで異なるヘッダーとフッターを挿入する
また、奇数ページと偶数ページで異なるヘッダーとフッターを挿入する必要がある状況に遭遇することがあります。Spire.Doc for Java では、これらのニーズに対応するために、奇数ページと偶数ページで異なるヘッダーとフッターを作成するメソッド、Section.getPageSetup().setDifferentOddAndEvenPagesHeaderFooter() が用意されています。
奇数ページと偶数ページで異なるヘッダーとフッターを挿入する詳しい手順は、以下のとおりです。
- Document クラスのオブジェクトを作成します。
 - Document.loadFromFile() メソッドを使用して Word 文書を読み込みます。
 - Document.getSections().get() メソッドを使用して、ドキュメントの最初のセクションを取得します。
 - Section.getPageSetup().setDifferentOddAndEvenPagesHeaderFooter() メソッドを使用して、奇数ページと偶数ページのヘッダーとフッターを異なるものに設定します。
 - カスタムの insertHeaderAndFooterOddEven() メソッドを使用すると、奇数ページと偶数ページに異なるヘッダーとフッターを挿入します。
 - Document.saveToFile() メソッドを使用して、ドキュメントを保存します。
 
- Java
 
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class insertHeaderAndFooter {
    public static void main(String[] args) {
        //Documentクラスのオブジェクトを作成する
        Document document = new Document();
        //Wordドキュメントを読み込む
        document.loadFromFile("私たちは織り成す存在である.docx");
        //ドキュメントの最初のセクションを取得する
        Section section = document.getSections().get(0);
        //奇数ページと偶数ページのヘッダーとフッターを異なるものに設定する
        section.getPageSetup().setDifferentOddAndEvenPagesHeaderFooter(true);
        //カスタム insertHeaderAndFooterOddEven() メソッドを呼び出し、奇数ページと偶数ページに異なるヘッダーとフッターを挿入する
        insertHeaderAndFooterOddEven(section);
        //ドキュメントを保存する
        document.saveToFile("奇数ページと偶数ページのヘッダーとフッター.docx", FileFormat.Docx);
    }
    private static void insertHeaderAndFooterOddEven(Section section) {
        //奇数ページにヘッダーを挿入する
        Paragraph P1 = section.getHeadersFooters().getOddHeader().addParagraph();
        TextRange OH = P1.appendText("奇数ページのヘッダー");
        P1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        OH.getCharacterFormat().setFontName("Yu Gothic UI");
        OH.getCharacterFormat().setFontSize(16);
        OH.getCharacterFormat().setTextColor(Color.BLUE);
        P1.getFormat().getBorders().getBottom().setBorderType(BorderStyle.Single);
        P1.getFormat().getBorders().getBottom().setLineWidth(1f);
        //偶数ページにヘッダーを挿入
        Paragraph P2 = section.getHeadersFooters().getEvenHeader().addParagraph();
        TextRange EH = P2.appendText("偶数ページのヘッダー");
        P2.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        EH.getCharacterFormat().setFontName("Yu Gothic UI");
        EH.getCharacterFormat().setFontSize(16);
        EH.getCharacterFormat().setTextColor(Color.BLUE);
        P2.getFormat().getBorders().getBottom().setBorderType(BorderStyle.Single);
        P2.getFormat().getBorders().getBottom().setLineWidth(1f);
        //奇数ページにフッターを挿入
        Paragraph P3 = section.getHeadersFooters().getOddFooter().addParagraph();
        TextRange OF = P3.appendText("奇数ページのフッター");
        P3.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        OF.getCharacterFormat().setFontName("Yu Gothic UI");
        OF.getCharacterFormat().setFontSize(16);
        OF.getCharacterFormat().setTextColor(Color.BLUE);
        P3.getFormat().getBorders().getTop().setBorderType(BorderStyle.Single);
        P3.getFormat().getBorders().getTop().setLineWidth(1f);
        //偶数ページにフッターを挿入する
        Paragraph P4 = section.getHeadersFooters().getEvenFooter().addParagraph();
        TextRange EF = P4.appendText("偶数ページのフッター");
        EF.getCharacterFormat().setFontName("Yu Gothic UI");
        EF.getCharacterFormat().setFontSize(16);
        P4.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        EF.getCharacterFormat().setTextColor(Color.BLUE);
        P4.getFormat().getBorders().getTop().setBorderType(BorderStyle.Single);
        P4.getFormat().getBorders().getTop().setLineWidth(1f);
    }
}
一時ライセンスを申請する
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
	    
	  	  





