Insert additional HTML when saving as HTML

My goal is to take an existing Word document and save it as HTML, but adding custom HTML when saving. The problem I’m having is when I save the Word doc as HTML I would like to insert my own custom HTML header, divs within the body, and custom footer. Below is the closest I’ve been able to get, but it simply outputs my custom HTML as text. Any help is appreciated!

Document htmlDoc = new Document(file.OpenBinaryStream());

DocumentBuilder builder = new DocumentBuilder(htmlDoc);

builder.MoveToDocumentStart();
builder.Write(headerHTML);

builder.MoveToDocumentEnd();
builder.Write(footerHTML);

string location = @"\server\path";

string filename = location + file.Name.Replace(".doc", "") + ".htm";

htmlDoc.Save(filename, SaveFormat.Html);

Hello

Thanks for your inquiry. In case of inserting HTML into the document you should use InsertHtml method. Please see the following link for more information:
https://reference.aspose.com/words/net/aspose.words/documentbuilder/inserthtml/
Please see the following code:

// Open the document.
Document doc = new Document("in.doc");
DocumentBuilder documentBuilder = new DocumentBuilder(doc);
// Insert HTML
documentBuilder.InsertHtml("Test");
doc.Save("out.html");

Best regards,

Thanks for the response. I’ve tried the InsertHtml method, but it doesn’t produce exactly what I’m looking for. For example, I can use the InsertHtml method to inject my custom header, but it doesn’t seem to persist my Javascript includes, and then how do I specify I want the document contents next, and after that, insert a custom footer? Is there a way to tap into the header and footer, or body tags, that Aspose produces? Thanks!

Hello

Thank you for additional information. I think, I your case, you should convert your document to HTML using Aspose.Words. Then open this HTML as string and add your custom HTML to this string. I this case you will be able to preserve your Javascripts.
Best regards,