Convert HTML to PDF- including HTML header and footer

I am converting a HTML document to PDF. This was simple enough to do. Open the html document. Save the html document with a PDF extension. Very cool, very easy, very straight forward.

I need to add a per-page header and footer to these documents. This header and footer is encoded in HTML also. In fact, it lives as separate HTML files. Does Aspose.Words have the ability to meet this need? I am hoping so, this product would save my project a ton of time and money.

Hello!
Thank you for your request.
Yes, it’s possible to insert headers and footers from HTML sources. Just try the following:

  1. Import the main document as usually:
Document doc = new Document("main_story.html");
  1. Create a DocumentBuilder associated with your document:
DocumentBuilder builder = new DocumentBuilder(doc);
  1. Position it to the appropriate header or footer:
builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst); // for instance first page header
  1. Insert another HTML (loaded into a string) to this header/footer:
builder.InsertHtml(htmlString);

See this documentation article about DocumentBuilder class:
https://reference.aspose.com/words/net/aspose.words/documentbuilder/
Regards,

This seems to make since, and I have tried the above steps before with no success. Here is some sample code that does NOT work. This was taken from the documentation link you sent me, and then combined with what you posted here as well. The result is a document with no header. Can you verify this on your end?

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.InsertHtml(
    "<P align='right'>Paragraph right</P>" +
    "<b>Implicit paragraph left</b>" +
    "<div align='center'>Div center</div>" +
    "<h1 align='left'>Heading 1 left.</h1>");

builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);  // for instance first page header
builder.InsertHtml("<p>this is a <b>test</b> header</p>");

doc.Save("d:\\DocumentBuilder.InsertHtml Out.doc");

Hello!
Please try setting these properties for the current section:
https://reference.aspose.com/words/net/aspose.words/pagesetup/differentfirstpageheaderfooter/
https://reference.aspose.com/words/net/aspose.words/pagesetup/oddandevenpagesheaderfooter/
They are reset by default. That’s why corresponding headers and footers are not visible. If your document has only one section just set them on Document.FirstSection.
Regards,

That gets it working. Thanks!