クラシックなテキストヘッダー & フッターを追加

HTMLコンテンツをレンダリングする際に、PDFドキュメントにテキストヘッダーとフッターを追加する方法を学びます。

テキストヘッダーとフッターを含めるには、レンダリングオプションオブジェクトで指定する必要があります。 HTMLタイトルを表示する中央揃えのテキスト、フォント設定、フォントサイズを含む分割線でヘッダーコンテンツを定義します。同様に、textFooterプロパティでフッターを定義します。 {page}{total-pages}{url}{date}{time}{html-title}{pdf-title}のようなフィールドを活用して、コンテンツを個別化できます。

ヘッダーとフッターが適切に収まるようにマージンをカスタマイズします。

HTMLコンテンツをPDFに変換するためにPdfDocument.fromHtmlメソッドを利用します。 renderOptionsオブジェクトをオプションとして渡します。

HTMLからPDFを作成する詳細なドキュメントは、IronPDF Documentationを訪問してください。

ヘッダーとフッターを含む最終的なPDFドキュメントは、「header_footer.pdf」という名前で保存されます。

// Import the necessary namespace
using IronPdf;

class Program
{
    static void Main()
    {
        // Define your HTML content
        string htmlContent = @"<html><body><h1>Hello, World!</h1><p>This is a PDF document with headers and footers.</p></body></html>";

        // Create the rendering options for the PDF
        var renderOptions = new PdfPrintOptions()
        {
            // Specify header settings with fields
            Header = new SimpleHeaderFooter()
            {
                CenterText = "{pdf-title}",
                FontFamily = "Arial",
                FontSize = 14,
                DrawDividerLine = true,
                Height = 50 // Customize height based on needs
            },
            // Specify footer settings with fields
            Footer = new SimpleHeaderFooter()
            {
                CenterText = "Page {page} of {total-pages}",
                FontFamily = "Arial",
                FontSize = 12,
                DrawDividerLine = true,
                Height = 50 // Customize height based on needs
            },
            MarginTop = 50, // Adjust top margin for header
            MarginBottom = 50 // Adjust bottom margin for footer
        };

        // Create a PDF document from HTML with the specified options
        var pdfDocument = PdfDocument.FromHtml(htmlContent, renderOptions);

        // Save the PDF document with headers and footers
        pdfDocument.SaveAs("header_footer.pdf");
    }
}
// Import the necessary namespace
using IronPdf;

class Program
{
    static void Main()
    {
        // Define your HTML content
        string htmlContent = @"<html><body><h1>Hello, World!</h1><p>This is a PDF document with headers and footers.</p></body></html>";

        // Create the rendering options for the PDF
        var renderOptions = new PdfPrintOptions()
        {
            // Specify header settings with fields
            Header = new SimpleHeaderFooter()
            {
                CenterText = "{pdf-title}",
                FontFamily = "Arial",
                FontSize = 14,
                DrawDividerLine = true,
                Height = 50 // Customize height based on needs
            },
            // Specify footer settings with fields
            Footer = new SimpleHeaderFooter()
            {
                CenterText = "Page {page} of {total-pages}",
                FontFamily = "Arial",
                FontSize = 12,
                DrawDividerLine = true,
                Height = 50 // Customize height based on needs
            },
            MarginTop = 50, // Adjust top margin for header
            MarginBottom = 50 // Adjust bottom margin for footer
        };

        // Create a PDF document from HTML with the specified options
        var pdfDocument = PdfDocument.FromHtml(htmlContent, renderOptions);

        // Save the PDF document with headers and footers
        pdfDocument.SaveAs("header_footer.pdf");
    }
}
' Import the necessary namespace
Imports IronPdf

Friend Class Program
	Shared Sub Main()
		' Define your HTML content
		Dim htmlContent As String = "<html><body><h1>Hello, World!</h1><p>This is a PDF document with headers and footers.</p></body></html>"

		' Create the rendering options for the PDF
		Dim renderOptions = New PdfPrintOptions() With {
			.Header = New SimpleHeaderFooter() With {
				.CenterText = "{pdf-title}",
				.FontFamily = "Arial",
				.FontSize = 14,
				.DrawDividerLine = True,
				.Height = 50
			},
			.Footer = New SimpleHeaderFooter() With {
				.CenterText = "Page {page} of {total-pages}",
				.FontFamily = "Arial",
				.FontSize = 12,
				.DrawDividerLine = True,
				.Height = 50
			},
			.MarginTop = 50,
			.MarginBottom = 50
		}

		' Create a PDF document from HTML with the specified options
		Dim pdfDocument = PdfDocument.FromHtml(htmlContent, renderOptions)

		' Save the PDF document with headers and footers
		pdfDocument.SaveAs("header_footer.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

GitHubでヘッダーとフッターの例コードを探る

準備はいいですか?
バージョン: 2025.11 ただ今リリースされました