How can I convert a document with html header into a word document with header
Can you please share your sample HTML file along with expected output Word document? We will look into the possibility of this feature and share our feedback with you accordingly.
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): HTMLNET-5085
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
We have investigated this task. There are 2 possible approaches to get a needed result:
- It’s possible to be done just using currently existing HTML manipulation API: you can insert header/footer elements as statically positioned into DOM hierarchy of the main HTML document. The trick belongs in the static positioning, provided by style such as ‘position: fixed; top: -50;’, and top/bottom page margin setup that prevents from the main content overlapping by header and footer on the each result document page. See the code snippet below.
UPD: See also attached resulting DOCX file.
var fileNameContent = "Anticimex.html";
var fileNameHeader = "AnticimexHeader.html";
var fileNameFooter = "AnticimexFooter.html";
using (var document = new HTMLDocument(Path.Combine(DataDir, fileNameContent)))
using (var docHeader = new HTMLDocument(Path.Combine(DataDir, fileNameHeader)))
using (var docFooter = new HTMLDocument(Path.Combine(DataDir, fileNameFooter)))
{
var body = document.GetElementsByTagName("BODY").First();
var headerToInsert = docHeader.GetElementsByTagName("HEADER").First();
var footerToInsert = docFooter.GetElementsByTagName("FOOTER").First();
var styleHeader = @"width:21cm; text-align: center; position: fixed; color: blue; top: -50;";
var styleFooter = @"width: 21cm; text-align: center; min-height: 32px; position: fixed; bottom: -50;";
var newHeader = document.ImportNode(headerToInsert, true);
var newFooter = document.ImportNode(footerToInsert, true);
Node.NodeHelper.SetParentNode(newHeader, body);
((Element)newHeader).SetAttribute("style", styleHeader);
Node.NodeHelper.SetParentNode(newFooter, body);
((Element)newFooter).SetAttribute("style", styleFooter);
var firstNode = body.ChildNodes[0];
body.InsertBefore(newHeader, firstNode);
body.AppendChild(newFooter);
var options = new DocSaveOptions()
{
PageSetup =
{
AnyPage =
{
Margin = new Drawing.Margin(0, 50, 0, 50)
}
}
};
var resultPath = Path.Combine(OutDir, string.Format("output_{0}.docx", Path.GetFileNameWithoutExtension(fileNameContent)));
Converter.ConvertHTML(document, options, resultPath);
}
- Word’s HeaterFooter object could be used to place the header/footer content. Currently we have no ability to add such objects into Word document. If such feature would be required, we’d consider the possibility to implement it, that obviously will take significant time. But anyway such approach would require you to setup custom margins during conversion, which will make it as difficult to use as the first approach.
output_Anticimex.docx (175.2 KB)