Ajouter des en-têtes et pieds de page texte classiques

Apprenez à ajouter des en-têtes et pieds de page à un document PDF lors du rendu de contenu HTML.

Pour inclure des en-têtes et pieds de page, vous devez les spécifier dans l'objet des options de rendu. Définissez le contenu de l'en-tête avec une ligne de division, un texte centré affichant le titre HTML, les paramètres de police et la taille de la police. De même, définissez le pied de page avec la propriété textFooter. Vous pouvez utiliser des champs comme {page}, {total-pages}, {url}, {date}, {time}, {html-title}, et {pdf-title} pour personnaliser le contenu.

Personnalisez les marges pour accommoder correctement l'en-tête et le pied de page.

Utilisez la méthode PdfDocument.fromHtml pour convertir votre contenu HTML en PDF. Passez l'objet renderOptions comme option.

Pour une documentation détaillée sur la création de PDFs à partir de HTML, visitez la Documentation d'IronPDF.

Le document PDF résultant, complet avec en-têtes et pieds de page, est enregistré sous le nom "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

Explorez le code exemple d'En-têtes & Pieds de page sur GitHub

Prêt à commencer?
Version : 2025.11 vient de sortir