HTMLヘッダー & フッターを追加

import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.headerfooter.HtmlHeaderFooter;
import java.io.IOException;
import java.nio.file.Paths;

PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");
// Build a footer using HTML
// Merge Fields are: {page} {total-pages} {url} {date} {time} {html-title} & {pdf-title}
HtmlHeaderFooter footer = new HtmlHeaderFooter();
footer.setMaxHeight(15); // millimeters
footer.setHtmlFragment("<center><i>{page} of {total-pages}</i></center>");
footer.setDrawDividerLine(true);
pdf.addHtmlFooter(footer);
List<PdfDocument> pdfs = new ArrayList<>();

// Build a header using an image asset
// Note the use of BaseUrl to set a relative path to the assets
HtmlHeaderFooter header = new HtmlHeaderFooter();
header.setMaxHeight(20); // millimeters
header.setHtmlFragment("<img src=\"logo.png\" />");
header.setBaseUrl("./assets/");
pdf.addHtmlHeader(header);
try {
    pdf.saveAs(Paths.get("assets/html_headers_footers.pdf"));
} catch (IOException e) {
    throw new RuntimeException(e);
}
mvn install
HTMLヘッダー & フッターを追加

HTMLヘッダー & フッターを追加

IronPDFは、開発者がPDFドキュメントをカスタマイズされたヘッダーとフッターを使用して変更することを可能にします。 このコード例は、開発者がHtmlHeaderFooterクラスを使用して、どのようにこれを達成できるかを示しています。

TextHeaderFooterクラスのように単なるテキストを使用する場合とは異なります。 クラスのインスタンスに対してaddHtmlFragmentメソッドを呼び出すことで、ヘッダーまたはフッターのHTMLマークアップを指定します。 このメソッドは、有効なHTML5、CSS3、およびJavaScript(ECMAScript 6)を含む文字列を受け入れます。 IronPDFは、この文字列の内容をリッチハイパーテキストマークアップとして解釈し、ウェブブラウザが行うのと同様に、結果として得られるPDFドキュメントに表示します。

開発者は、画像、色、特殊な書体、その他のより複雑なデザイン要素を組み込んだヘッダーを作成するためにこのクラスを使用することができます。

より伝統的で単純化されたテキストベースのヘッダー/フッターのカスタマイズのためには、代わりにTextHeaderFooterクラスを選択することを検討してください。

Explore the HtmlHeaderFooter Class Documentation
GitHubで見る

準備はできましたか?

バージョン:2026.10リリースされたばかり