HTMLヘッダー & フッターを追加
using IronPdf;
using System;

// Instantiate Renderer
var renderer = new IronPdf.ChromePdfRenderer();


// Build a footer using html to style the text
// mergeable fields are:
// {page} {total-pages} {url} {date} {time} {html-title} & {pdf-title}
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{
    MaxHeight = 15, //millimeters
    HtmlFragment = "<center><i>{page} of {total-pages}<i></center>",
    DrawDividerLine = true
};

// Use sufficient MarginBottom to ensure that the HtmlFooter does not overlap with the main PDF page content.
renderer.RenderingOptions.MarginBottom = 25; //mm


// Build a header using an image asset
// Note the use of BaseUrl to set a relative path to the assets
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
    MaxHeight = 20, //millimeters
    HtmlFragment = "<img src='logo.png'>",
    BaseUrl = new Uri(@"C:\assets\images\").AbsoluteUri
};

// Use sufficient MarginTop to ensure that the HtmlHeader does not overlap with the main PDF page content.
renderer.RenderingOptions.MarginTop = 25; //mm
NuGet Download
PM > Install-Package IronPdf
HTMLヘッダー & フッターを追加

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

HTMLヘッダーとフッターは、PDFドキュメント用の動的なヘッダーとフッターを作成するための柔軟な方法を提供します。 この方法でヘッダーとフッターを追加することにより、開発者はヘッダーとフッターの外観を完全にコントロールできます。これらは独立したHTMLドキュメントとしてレンダリングされ、それぞれ独自のアセットとスタイルシートを含むことができます。

IronPDFでPDFにカスタムHTMLヘッダーとフッターを追加する手順

まず初めに、ChromePdfRenderer クラスのインスタンスを作成する必要があります。このクラスはHTMLコンテンツをピクセル完璧なPDFドキュメントにレンダリングします。

次に、HtmlHeaderFooter クラスを使用してフッターを定義します。ここでは、MaxHeight、ページ番号が含まれるHTMLコンテンツ、画像解像度の基準URLを指定します。 フッターは中央にページ情報を表示するようにスタイリングされています。

フッターとPDFのメインコンテンツの重なりを避けるために、MarginBottom プロパティを使用して下部余白を設定します。 同様に、HtmlHeaderFooter クラスを使用して画像(ロゴなど)を含むヘッダーを作成します。 ここでは、画像アセットが含まれるディレクトリへのBaseUrl を設定し、レンダリング中に適切な画像解像度を可能にしています。

最後に、MarginTop プロパティを使用して上部余白を設定し、ヘッダーとコンテンツの重なりを防ぎます。 この例は、IronPDFを使用してPDFドキュメントにカスタムHTMLヘッダーとフッターを実装するのがいかに簡単であるかを示しています。

IronPDFでPDFにHTMLヘッダーとフッターを追加する方法を学ぶ
GitHubで見る

準備はできましたか?

Nuget Downloads 21,916,531バージョン:2026.11リリースされたばかり