添加经典文本页眉和页脚
学习如何在渲染HTML内容时向PDF文档添加文本页眉和页脚。
要包含文本页眉和页脚,您需要在渲染选项对象中指定它们。 Define the header content with a divider line, centered text displaying the HTML title, font settings, and font size. Similarly, define the footer with the textFooter property. You can utilize fields like {page}, {total-pages}, {url}, {date}, {time}, {html-title}, and {pdf-title} to personalize the content.
自定义边距以适当容纳页眉和页脚。
Utilize the PdfDocument.fromHtml method to convert your HTML content into a PDF. Pass the renderOptions object as an option.
有关从HTML创建PDF的详细文档,请访问IronPDF文档。
生成的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




