views:

152

answers:

3

I am creating a Word format .doc using the following code, then cfheader and cfcontent to serve. All is good but I need to be able to place dynamic information in the header (or footer), or automatic pagenumbering would be a second best option.

How should I modify the code?

<cfsavecontent variable="myDocument">
<html xmlns:w="urn:schemas-microsoft-com:office:word">
<!--- Head tag instructs Word to start up a certain way, specifically in
print view. --->
    <head>
        <xml>
         <w:WordDocument>
            <w:View>Print</w:View>
            <w:SpellingState>Clean</w:SpellingState>
            <w:GrammarState>Clean</w:GrammarState>
            <w:Compatibility>
             <w:BreakWrappedTables/>
             <w:SnapToGridInCell/>
             <w:WrapTextWithPunct/>
             <w:UseAsianBreakRules/>
            </w:Compatibility>
            <w:DoNotOptimizeForBrowser/>
         </w:WordDocument>
        </xml>
    </head>
<body>
    Regular HTML document goes here
    <!--- Create a page break microsoft style (took hours to find this) 
--->
    <br clear="all"
style="page-break-before:always;mso-break-type:page-break" />
    Next page goes here
</body>
</html>
</cfsavecontent> 
+1  A: 

Doesn't seem easy to add page number using a WordprocessingML

http://openxmldeveloper.org/archive/2006/08/03/443.aspx

If you can serve PDF instead of DOC, here's a solution for page numbering.

http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7c21.html

See example 2:

<cfdocument format="pdf"> 
<cfdocumentitem type="header" evalatprint="true"> 
    <table width="100%" border="0" cellpadding="0" cellspacing="0"> 
        <tr><td align="right"><cfoutput>#cfdocument.currentsectionpagenumber# of 
            #cfdocument.totalsectionpagecount#</cfoutput></td></tr> 
    </table> 
</cfdocumentitem> 

<cfdocumentitem type="footer" evalatprint="true"> 
    <table width="100%" border="0" cellpadding="0" cellspacing="0"> 
        <tr><td align="center"><cfoutput>#cfdocument.currentpagenumber# of 
            #cfdocument.totalpagecount#</cfoutput></td></tr> 
    </table> 
</cfdocumentitem> 

...    

</cfdocument>
Henry
Useful link thanks Henry, the client needs it to be .doc or .rtf though.
Saul
A: 

I do not think it is possible using the html/xml model. The header/footer information is stored separately, as html does not really support the concept. So it would require downloading multiple files, which is not possible unless you are serving the contents as a .zip file.

Per the Office HTML and XML Reference

Header and footer information is stored in a separate file named Header.htm. Inside the file, the header and footer information is stored as fields, using Span elements with the mso-element:field-begin, mso-element:field-separator, and mso-element:field-end attributes in conjunction with the MsoHeader and MsoPageNumber styles.

EDIT: Technically, a url can be used to specify the header file location. But I do not think that is a great option, as it creates dependencies. It also seems to trigger an MS Word security warning of sorts "Some of the files in this Web page aren't in the expected location. Do you want to download them anyway ...". However, to demonstrate, I think something like this should be the minimum needed to produce headers.

<!---
    Download
--->
<cfheader name="Content-Disposition" value="inline; filename=myDoc.doc">
<cfcontent type="application/msword">
<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"&gt;
<head>
<!--[if gte mso 9]> 
    <xml>
      <w:WordDocument>
          <w:View>Print</w:View>
          <w:Compatibility>
              <w:UseAsianBreakRules/>
          </w:Compatibility>
          <w:DoNotOptimizeForBrowser/>
        </w:WordDocument>
    </xml>
    <![endif]-->
    <style>
    <!--    
    p.MsoNormal { font-family: Arial; }
    @page Section1  {
        mso-header-margin:.5in;
        mso-footer-margin:.5in;
        mso-header:url(http://mysite.com/myDoc_files/header.html") h1;
    }
    div.Section1 {page:Section1;}
    -->
    </style>            
</head>
<body>
    <div class="Section1">  
        Regular HTML document goes here
        <br clear="all" style="page-break-before:always;mso-break-type:page-break" />
        Next page goes here
    </div>
</body>
</html>

<!---
    myDoc_files/Header.html
--->

<html xmlns:v="urn:schemas-microsoft-com:vml"
    xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:w="urn:schemas-microsoft-com:office:word"
    xmlns="http://www.w3.org/TR/REC-html40"&gt;
<head></head>
<body>
<div style='mso-element:header' id="h1">
    <p class="MsoHeader">Starting header at Page 
        <span style='mso-field-code:" PAGE "'>
            <span style='mso-no-proof:yes'>1</span>
        </span> of 
        <span style='mso-field-code:" NUMPAGES "'>
            <span style='mso-no-proof:yes'>1</span>
        </span>
    </p>
</div>
</body>
</html>
Leigh
+3  A: 

Please have a look at this: Header & Footer I have successfully created custom header and footer with only one html file using this article. (Word 2003)

Hope this helps!

Impulse
Exactly what I needed, many thanks
Saul