Is it possible to add a header in my HTML, which will be shown ever page generated when converting it to pdf?
what i’ve tried:
public byte[] ConvertHtmlToPdf(string data)
{
using (var streamProvider = new MemoryStreamProvider())
{
using (var document = new HTMLDocument(data, "."))
{
string headerHtml = @"
<div class='header' style='position: fixed; top: 0; left: 0; bottom: 50px; width: 100%; text-align: center; font-size: 16px;'>
<p>Header Content Goes Here</p>
</div>
";
var body = document.GetElementsByTagName("body").First();
var headerElement = document.CreateElement("div");
headerElement.InnerHTML = headerHtml;
body.InsertBefore(headerElement, body.FirstChild);
var options = new PdfSaveOptions()
{
JpegQuality = 100,
};
options.PageSetup.AnyPage = new Page(
margin: new Margin(left: 20, top: 80, right: 10, bottom: 10)
);
FontsSettings fontsSettings = new FontsSettings();
fontsSettings.SetFontsLookupFolder(Path.Combine(AppContext.BaseDirectory, "Fonts"));
Aspose.Html.Converters.Converter.ConvertHTML(document, options, streamProvider);
var memory = streamProvider.Streams.First();
memory.Seek(0, SeekOrigin.Begin);
return memory.ToArray();
}
}
}
combined with html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Example</title>
<style>
body {
font-family: Arial, sans-serif;
position: relative;
}
.section {
margin-bottom: 20px;
}
.footer {
width: 100%;
text-align: center;
position: fixed;
left: 0;
}
.footer {
bottom: 0;
border-top: 1px solid #ccc;
padding: 10px 0;
}
.header img, .footer img {
max-width: 150px;
}
.disclaimer {
border: 1px solid #ccc;
padding: 150px;
border-radius: 5px;
}
table {
width: 100%;
border-collapse: collapse;
}
table, th, td {
border: 1px solid #ccc;
}
th, td {
padding: 10px;
text-align: left;
}
.page-break {
page-break-before: always;
}
</style>
</head>
<body>
<div class="main-content">
<div class="summary section">
<h2>Summary</h2>
<p>Paragraph</p>
</div>
<div class="standard-table section page-break">
<h2>A table</h2>
<StandardEquipmentTable>
</div>
<div class="disclaimer section page-break">
<p>A disclaimer</p>
<p><Disclaimer></p>
</div>
</div>
<div class="footer">
<p><Reference></p>
<img src="<Logo>">
<p><DocumentDate></p>
</div>
</body>
</html>
however this pushes both the header section i’ve put in my c# and the body down.