Search code examples
htmlms-wordheaderms-officewordml

Making text overlap header on a dynamically generated Word document


I create 2 identical word documents: test1 and test2 They both contain a header.

The only difference between the 2 documents is that the header's lower margin has been moved up so the text can overlap the header.

I then generate xml and compare. I notice one interesting line which is different for both docs, here are the results :

Test1.xml :

<w:pgMar w:top="1417" w:right="1417" w:bottom="1417" w:left="1417" w:header="708" w:footer="708" w:gutter="0" />

Test2.xml :

<w:pgMar w:top="-49" w:right="1417" w:bottom="1417" w:left="1417" w:header="708" w:footer="708" w:gutter="0" />

So the w:pgMar w:top has changed from 1417 to -49.

Now I am dynamically generating Word docs from HTML, here is the main part of the code used :

<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'>
<head><title>Mon document</title>
<meta charset=\"UTF-8\" />
<!--[if gte mso 9]>
<xml><w:WordDocument><w:View>Print</w:View><w:Zoom>100</w:Zoom>    <w:DoNotOptimizeForBrowser/></w:WordDocument></xml>
<![endif]-->
<link rel=File-List href=\"mydocument_files/filelist.xml\">
<style><!-- 
@page
{
    size:21cm 29.7cmt;  /* A4 */
    margin:1cm 1cm 1cm 1cm; /* Margins: 2.5 cm on each side */
    mso-page-orientation: portrait;  
    mso-header: url(\"mydocument_files/headerfooter.htm\") h1;
    mso-footer: url(\"mydocument_files/headerfooter.htm\") f1;  
}
@page Section1 { }
div.Section1 { page:Section1; }
p.MsoHeader, p.MsoFooter { border: none; }
--></style>
</head>
<body>
<div class=Section1>
    My content
</div>
</body>
</html>

Could anyone tell me if I can reproduce the overlapping effect between text and header by integrating the noticed xml parameters somewhere, or by adding style this code ?


Solution

  • Setting a negative top margin for section1 and a fixed margin for mso-header fixes the issue :

    @page Section1 {margin: -1cm 1cm 1cm 1cm; mso-header-margin: 1cm;}
    

    The main text is now at the exact same level than the header, whatever size is the header.