6

I'm successfully generating word documents using html code with header and footer styled in css print mode, here is my code :

<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>

What I would like to do is to display the header and footer only on first page. For that I have tried to apply visibility:hidden to the header and footer for pages different than first this way :

p.MsoHeader, p.MsoFooter { border: none; visibility: hidden;}
p.MsoHeader :first, p.MsoFooter :first { border: none; visibility: visible;}

But the header and footer are still displayed on all pages... Any idea how to do the trick?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Vincent Teyssier
  • 2,146
  • 5
  • 25
  • 62
  • Try creating a simple sample document in Word with the option "First Page" activated for Header/Footer - this is a specific option in Word. Then save it as a "round-trip" HTML file and look at what Word wants you to use. – Cindy Meister Feb 09 '16 at 16:29
  • I have tried this but the difference I get is plenty of mso-list-id tags... I don't see any difference which would indicate something about header footer, despite I used the compare plugin in notepad++ – Vincent Teyssier Feb 09 '16 at 16:31
  • 2
    Headers/Footers are associated with sections. The document probably has only one section, and its definition is usually near the end of the document. Maybe that will help you find it? – Cindy Meister Feb 09 '16 at 18:20
  • Cindy Meister is right. Word handles headers and footers based on section breaks. That is why you run into that error where you cant un-check "link to last section". Go in and make a document with several pages and make sure you make sections, then look at the code and you will see how it handle each section. Then add CSS accordingly. – Blizzardengle Feb 12 '16 at 01:34
  • Yes, I know that but do not find anything that could lead me to a solution. The differences are as follow : mso-title-page:yes; (but I don't find any doc on this one) and ms-list-id/mso-list-template-ids a dozen times. – Vincent Teyssier Feb 12 '16 at 12:32
  • ok, actually I missed one essential point in the middle of the word generated html. By pushing me to redo comparison I finally found the answer. Thanks a lot – Vincent Teyssier Feb 12 '16 at 12:45

2 Answers2

2

Try this:

p.MsoHeader, p.MsoFooter { border: none; display: none;}
p.MsoHeader :first, p.MsoFooter :first { border: none; display: block;}

Or, if this doesn't work, it's better if you don't call the header and the footer in the first play, so just remove these two lines from the pages that you don't want the header and the footer to appear in:

mso-header: url(\"mydocument_files/headerfooter.htm\") h1;
mso-footer: url(\"mydocument_files/headerfooter.htm\") f1; 
Tarek.hms
  • 1,243
  • 1
  • 10
  • 15
1

When comparing word generated html, I have missed one crucial mso css tag :

mso-first-header: url ...

Instead of mso-header.

Together with that, the attribute mso-title-page must also be set to yes.

By combining these two you get the desired effect!

Vincent Teyssier
  • 2,146
  • 5
  • 25
  • 62