Trying to add header content dynamically. Our body is a dynamic HTML, and that's working mostly fine using the new DOM approach. I added a fixed footer with the page numbers and that's also fine. When I add a header with an HtmlFragment, however, the content of the header appears at the bottom of the page, rendered upside down and backward. It also appears to push the footer off the page in most cases, and PDFReader reports, "An error exists on this page ...". I tried swapping the header and footer and got no header/footer at all. I tried using my full, complex, HTML as well as a small, simple HTML, and nothing worked. Any way to make this work?
Substance of the code is below, sample HTML and result attached:
//load
HtmlLoadOptions htmloptions = new HtmlLoadOptions();
Document doc = new Document(filepath, htmloptions);
//header
using (OpenFileDialog ofd = new OpenFileDialog() { Title = "Select Header HTML", InitialDirectory = _mypath, Filter = "web doc|*.html;*.htm;*.txt" })
{
if (ofd.ShowDialog().Equals(System.Windows.Forms.DialogResult.OK))
{
using (StreamReader sr = System.IO.File.OpenText(ofd.FileName))
{
Aspose.Pdf.HeaderFooter header = new Aspose.Pdf.HeaderFooter();
header.Paragraphs.Add(new HtmlFragment(sr.ReadToEnd()) { IsInLineParagraph = true });
for (int cnt = 1; cnt <= doc.Pages.Count; cnt++)
{
doc.Pages[cnt].Header = header;
}
sr.Close();
}
}
}
//footer
Aspose.Pdf.HeaderFooter footer = new Aspose.Pdf.HeaderFooter();
footer.Paragraphs.Add(new TextFragment("Page $p of $P ") { HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right });
for (int cnt = 1; cnt <= doc.Pages.Count; cnt++)
{
doc.Pages[cnt].Footer = footer;
}
//output
using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "PDF|*.pdf", InitialDirectory = _mypath })
{
if (sfd.ShowDialog().Equals(DialogResult.OK))
{
_FileName = sfd.FileName;
doc.Save(sfd.FileName);
}
}