チュートリアル
簡単にライブラリーを使用するためのチュートリアルコレクション
Word ドキュメントのヘッダーとフッターは、ドキュメントページの上部と下部にそれぞれ配置され、ページ番号、ドキュメントタイトル、著者などの情報を表示します。しかし、ドキュメントを印刷したりオンラインで共有したりする場合、プライバシーを保護するためにこれらの情報を削除することが望ましい場合があります。さらに、ヘッダーとフッターは、印刷されたページの貴重なスペースを余分に占め、ドキュメントの全体的な書式を妨げる可能性があります。ヘッダーやフッターを削除することで、ページスペースを確保し、ドキュメントの書式が乱雑にならないようにすることができます。この記事では、Spire.Doc for Java を使用して、Java プログラムで Word ドキュメントからヘッダーとフッターを削除する方法を紹介します。
まず、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.7.0</version>
</dependency>
</dependencies>
Word ドキュメントでは、最初のページ、奇数ページ、偶数ページに異なるヘッダーとフッターを設定することができます。これらのタイプのヘッダーとフッターは HeaderFooter.getByHeaderFooterType(hfType) メソッドで取得でき、 HeaderFooter.getChildObjects().clear() メソッドで削除できます。
以下に、列挙型と、それらが表すヘッダーとフッターのタイプのリストを示します。
列挙型 | 説明 |
HeaderFooterType.Header_First_Page | 1ページ目のヘッダーを表します。 |
HeaderFooterType.Footer_First_Page | 1ページ目のフッターを表します。 |
HeaderFooterType.Header_Odd | 奇数ページのヘッダーを表します。 |
HeaderFooterType.Footer_Odd | 奇数ページのフッターを表します。 |
HeaderFooterType.Header_Even | 偶数ページのヘッダーを表します。 |
HeaderFooterType.Footer_Even | 偶数ページのフッターを表します。 |
詳しい手順は以下の通り。
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.HeaderFooterType;
public class removeHeaderFooter {
public static void main(String[] args) {
//Documentクラスのオブジェクトを作成する
Document doc = new Document();
//Wordドキュメントを読み込む
doc.loadFromFile("サンプル.docx");
//ドキュメントの最初のセクションを取得する
Section section = doc.getSections().get(0);
//1ページ目のヘッダーを取得し、その内容を削除する
HeaderFooter header = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.Header_First_Page);
header.getChildObjects().clear();
//1ページ目のフッターを取得し、その内容を削除する
HeaderFooter footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.Footer_First_Page);
footer.getChildObjects().clear();
//奇数ページのヘッダーとフッターを取得し、その内容を消去する
//HeaderFooter header1 = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.Header_Odd);
//header1.getChildObjects().clear();
//HeaderFooter footer1 = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.Footer_Odd);
//footer1.getChildObjects().clear();
//偶数ページのヘッダーとフッターを取得し、その内容を消去する
//HeaderFooter header2 = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.Header_Even);
//header2.getChildObjects().clear();
//HeaderFooter footer2 = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.Footer_Even);
//footer2.getChildObjects().clear();
//ファイルを保存する
doc.saveToFile("ヘッダーとフッターの種類別に削除.docx", FileFormat.Auto);
doc.dispose();
}
}
セクションによってヘッダーとフッターは異なります。あるセクションのヘッダーとフッターを削除するには、Document.getSections().get() メソッドでそのセクションを取得し、HeaderFooter.getChildObjects().clear() メソッドでその中のヘッダーとフッターを削除します。
注意しなければならないのは、セクション内のヘッダーとフッターの内容を削除すると、自動的に前のセクションの内容に変わってしまうということです。そのため、ヘッダーとフッターを削除した後に空白の段落を追加して、自動的に変更されないようにする必要があります。
詳しい手順は以下の通り。
import com.spire.doc.Document;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
public class removeHeaderFooterSection {
public static void main(String[] args) {
//Documentクラスのオブジェクトを作成する
Document doc = new Document();
//Wordドキュメントを読み込む
doc.loadFromFile("サンプル1.docx");
//ドキュメントの2番目のセクションを取得する
Section section = doc.getSections().get(1);
//セクションのヘッダーを取得し、その内容を削除し、空白の段落を追加する
HeaderFooter header1 = section.getHeadersFooters().getHeader();
header1.getChildObjects().clear();
header1.addParagraph();
//セクションのフッターを取得し、その内容を削除し、空白の段落を追加する。
HeaderFooter footer1 = section.getHeadersFooters().getFooter();
footer1.getChildObjects().clear();
footer1.addParagraph();
//ファイルを保存する
doc.saveToFile("ヘッダーとフッターのセクション別に削除.docx");
doc.dispose();
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
Word 文書におけるヘッダーとは、ページの上部に位置するテキストや画像のコンテンツのことで、フッターはページの下部に位置するものであるということです。 ヘッダーとフッターは、著作権、著者情報、ページ番号など、文書に関する重要な情報を表示するためによく使われます。また、文書をより専門的で美しいものにするために、文書を装飾するためにも使用されます。 ヘッダーとフッターは、文書の各ページまたは最初のページに挿入することができ、文書の奇数ページと偶数ページに異なるヘッダーとフッターを挿入することができます。 この記事では、Spire.Doc for Java を使用してプログラム的に Word 文書にヘッダーとフッターを挿入する方法について説明します。
まず、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 文書にヘッダーとフッターを挿入するには、まず Section.getHeadersFooters().getHeader() と Section.getHeadersFooters().getFooter() メソッドを使用してヘッダーとフッターを取得しておく必要があります。 そして、それらに段落を追加します。 最後に段落に画像、テキスト、ページ番号などを追加します。
以下に、ヘッダーとフッターの挿入方法の詳細を示します。
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);
}
}
最初のページにヘッダーとフッターを挿入するだけでよい場合もありますが、これも Spire.Doc for Java を使って行うことができます。 Section.getPageSetup().setDifferentFirstPageHeaderFooter() メソッドを使用して、最初のページのヘッダーとフッターを他のページと異なるものにし、最初のページにヘッダーとフッターを挿入することができます。
最初のページのみにヘッダーとフッターを挿入する詳しい手順は、以下の通りです。
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);
}
}
また、奇数ページと偶数ページで異なるヘッダーとフッターを挿入する必要がある状況に遭遇することがあります。Spire.Doc for Java では、これらのニーズに対応するために、奇数ページと偶数ページで異なるヘッダーとフッターを作成するメソッド、Section.getPageSetup().setDifferentOddAndEvenPagesHeaderFooter() が用意されています。
奇数ページと偶数ページで異なるヘッダーとフッターを挿入する詳しい手順は、以下のとおりです。
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 日間有効な一時ライセンスを取得してください。