Aspose PDF header as Html string with CSS file reference.
I’m try in create a PDF header/footer with html string, styles classes are define in css file that is referenced inside html HEAD tag. PDF Document.Page Header is not supporting CSS file reference. Our goal is to insert different header as html template based on business rules
Example:
public static MemoryStream GeneratePdf2(MemoryStream memoryStream)
{
string htmlBody = @"<html>
<head>
<link href=""https://localhost:7067/css/site.css"" rel=""stylesheet"" />
</head>
<body >
<div class =""header-bg"">
<h1>Main Body</h1><br/>
<div>
<p>This is example pharagraph</p>
</div>
<div>
<p>Example content here!!!</p>
</div>
</div>
</body>
</html>";
var byteArray = Encoding.UTF8.GetBytes(htmlBody);
var hmtlStream = new MemoryStream(byteArray);
HtmlLoadOptions loadoptions = new HtmlLoadOptions();
loadoptions.PageInfo.Margin = new MarginInfo(0, 0, 0, 100);
Document pdfDocument = new Document(hmtlStream, loadoptions);
//----------------------------------------------------------------------------------------------------//
string htmlHeader = @"<html>
<head>
<link href=""https://localhost:7067/css/site.css"" rel=""stylesheet"" />
</head>
<body >
<div class =""body-bg"">
<h1>Main Header</h1><br/>
<h3>Sub Header</h3><p> Sub Title</p>
</div>
</body>
</html>";
// Add header and footer on all pages
foreach (Aspose.Pdf.Page page in pdfDocument.Pages)
{
page.Header = new HeaderFooter();
page.Header.Paragraphs.Add(new HtmlFragment(htmlHeader)); ;
page.Header.Margin = new MarginInfo(0,0, 0, 0);
}
//----------------------------------------------------------------------------------------------------//
pdfDocument.Save(memoryStream);
return memoryStream;
}