Klassische Textköpfe- und -füße hinzufügen

Erfahren Sie, wie Sie Textüberschriften und -fußzeilen zu einem PDF-Dokument hinzufügen, wenn Sie HTML-Inhalte rendern.

Um Textüberschriften und -fußzeilen einzuschließen, müssen Sie diese im Rendering-Optionsobjekt angeben. Definieren Sie den Inhalt der Kopfzeile mit einer Trennlinie, zentriertem Text, der den HTML-Titel anzeigt, Schriftarteinstellungen und Schriftgröße. Ähnlich definieren Sie die Fußzeile mit der textFooter-Eigenschaft. Sie können Felder wie {page}, {total-pages}, {url}, {date}, {time}, {html-title} und {pdf-title} nutzen, um den Inhalt zu personalisieren.

Passen Sie die Ränder an, um die Kopf- und Fußzeile angemessen zu berücksichtigen.

Verwenden Sie die Methode PdfDocument.fromHtml, um Ihre HTML-Inhalte in ein PDF umzuwandeln. Übergeben Sie das renderOptions-Objekt als Option.

Für eine detaillierte Dokumentation zur Erstellung von PDFs aus HTML besuchen Sie die IronPDF-Dokumentation.

Das resultierende PDF-Dokument, komplett mit Kopf- und Fußzeilen, wird unter dem Namen "header_footer.pdf" gespeichert.

// 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

Erkunden Sie Beispielcode für Kopf- und Fußzeilen auf GitHub

Bereit anzufangen?
Version: 2025.11 gerade veröffentlicht