function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PrajnithPrajnith 

VF page Doc conversion - Header is being displayed twice on First page.

Hi 

 

 

I am facing an issue with VF to word doc conversion , header is being disaplyed twice on the first page, and header is displayed only once in the remaining all pages.

 

 

 

Thanks

Prajnith

Best Answer chosen by Admin (Salesforce Developers) 
ParinParin

Hi,

 

Atlast after a lot of trial and error, i have finally managed to overcome the problem of repeating header and footer in the generated ms word file.

 

For this i have applied a style for header and footer and contained both in a table.


This effectively pushed the table off the page and since the table is very thin due to the minimal header/footer information, moving the table off the page did not cause Word to generate another page.


Please note
1)The height of header and footer should not be too much.
2)I have tested this in word 2007 only.
I hope this will work in your case too.

 

Please find the actual code i have used below:

<apex:page sidebar="false" contentType="application/msword" cache="true">
    <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>
            <style>
                p.MsoHeader, li.MsoHeader, div.MsoHeader{
                    margin:0in;
                    margin-top:.0001pt;
                    mso-pagination:widow-orphan;
                    tab-stops:center 3.0in right 6.0in;
                }
                p.MsoFooter, li.MsoFooter, div.MsoFooter{
                    margin:0in;
                    margin-bottom:.0001pt;
                    mso-pagination:widow-orphan;
                    tab-stops:center 3.0in right 6.0in;
                }
                @page Section1{
                    size:8.5in 11.0in; 
                    margin:0.5in 0.5in 0.5in 0.5in;
                    mso-header-margin:0.5in;
                    mso-header:h1;
                    mso-footer:f1; 
                    mso-footer-margin:0.5in;
                    mso-paper-source:0;
                }
                div.Section1{
                    page:Section1;
                }
                /*Below is the style to prevent the repetition of header and Footer.. Cheers!!!*/
                table#hrdftrtbl{
                    margin:0in 0in 0in 9in;
                }        
            </style>
        </head>

        <body>
            <!-- Content -->
            <div class="Section1"><!--Section1 div starts-->
                <!-- Page 1 starts -->
                <br/>
                Page 1 Content

                <br clear="all" style="page-break-before:always" />
                
                
                <!-- Page 2 Starts -->
                <br/>
                Page 2 Content
                
                <br clear="all" style="page-break-before:always" />

                <!-- Page 3 Starts -->
                <br/>
                Page 3 Content
                
                <!--Header and Footer Starts-->
                <table id='hrdftrtbl' border='1' cellspacing='0' cellpadding='0'>
                    <tr>
                        <td>
                            <!--Header-->
                            <div style='mso-element:header' id="h1" >
                                <p class="MsoHeader">
                                    <table border="1" width="100%">
                                        <tr>
                                            <td width="30%">
                                                Left <br/>Header
                                            </td>
                                            <td align="center" width="40%">
                                                Center<br/> Header
                                            </td>
                                            <td align="right" width="30%">
                                                Right<br/>Header
                                            </td>
                                        </tr>
                                    </table>
                                </p>
                            </div>
                        </td>

                        <td>
                            <!--Footer-->
                            <div style='mso-element:footer' id="f1">
                                <p class="MsoFooter">
                                    <table width="100%" border="1" cellspacing="0" cellpadding="0">
                                        <tr>
                                            <td width="30%">
                                                Left<br/>Footer
                                            </td>
                                            <td align="center" width="40%">
                                                Center<br/>Footer
                                            </td>
                                            <td align="right" width="30%">
                                                Page <span style='mso-field-code: PAGE '></span> of <span style='mso-field-code: NUMPAGES '></span>
                                            </td>
                                        </tr>
                                    </table>
                                </p>
                            </div>
                        </td>
                    </tr>
                </table>
            </div><!--Section1 div ends-->
        </body>
    </html>
</apex:page>

 By: Parin

All Answers

cwall_sfdccwall_sfdc

Is the page part of a composition that uses a template?  If so, try setting the template to showHeader=false;

PrajnithPrajnith

Im using the below code to display header in the word doc.

 

 

 

<apex:page standardController="Quote" extensions="testclassCTRL" cache="true" 

contentType="application/vnd.msword#QuoteNumber-{!quoteObj.QuoteNumber}.doc" standardStylesheets="false" >

 

<html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word">

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<Style>
@page Section1 {mso-Header:f1;}
div.Section1{page:Section1;}
p.MsoHeader, li.MsoHeader, div.MsoHeader{
mso-pagination:widow-orphan;
tab-stops:center 216.0pt right 432.0pt;}
</Style>

<body>

<div class="Section1">
<div style="mso-element:Header" id="f1">
<table border="0" width="100%" height="10" id="headertable" >
<tr>
<td width="20%">
<div style="font-family: Arial; font-size: 8pt;text-align:left;">
<apex:outputText value="{!quoteObj.quoteToName}" />
<br/>
<apex:outputText value="Quotation No:{!quoteObj.QuoteNumber}"/>
</div>
</td>
<td width="60%">
<div style="font-family: Arial; font-size: 12pt; font-weight: bold;text-align:center;">
<apex:outputText style="font-family: Arial; font-size: 12pt; font-weight: bold">Test Header 4</apex:outputText>
</div>
</td>
<td width="20%">
<div style="font-family: Arial; font-size: 8pt;text-align:right;">
<apex:outputText >
{!MONTH(TODAY())}/{!DAY(TODAY())}/{!YEAR(TODAY())}
</apex:outputText>
</div>
</td>
</tr>
</table>
</div>
</div>

</body>
</html>
</apex:page>

ParinParin

Hi,

 

Atlast after a lot of trial and error, i have finally managed to overcome the problem of repeating header and footer in the generated ms word file.

 

For this i have applied a style for header and footer and contained both in a table.


This effectively pushed the table off the page and since the table is very thin due to the minimal header/footer information, moving the table off the page did not cause Word to generate another page.


Please note
1)The height of header and footer should not be too much.
2)I have tested this in word 2007 only.
I hope this will work in your case too.

 

Please find the actual code i have used below:

<apex:page sidebar="false" contentType="application/msword" cache="true">
    <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>
            <style>
                p.MsoHeader, li.MsoHeader, div.MsoHeader{
                    margin:0in;
                    margin-top:.0001pt;
                    mso-pagination:widow-orphan;
                    tab-stops:center 3.0in right 6.0in;
                }
                p.MsoFooter, li.MsoFooter, div.MsoFooter{
                    margin:0in;
                    margin-bottom:.0001pt;
                    mso-pagination:widow-orphan;
                    tab-stops:center 3.0in right 6.0in;
                }
                @page Section1{
                    size:8.5in 11.0in; 
                    margin:0.5in 0.5in 0.5in 0.5in;
                    mso-header-margin:0.5in;
                    mso-header:h1;
                    mso-footer:f1; 
                    mso-footer-margin:0.5in;
                    mso-paper-source:0;
                }
                div.Section1{
                    page:Section1;
                }
                /*Below is the style to prevent the repetition of header and Footer.. Cheers!!!*/
                table#hrdftrtbl{
                    margin:0in 0in 0in 9in;
                }        
            </style>
        </head>

        <body>
            <!-- Content -->
            <div class="Section1"><!--Section1 div starts-->
                <!-- Page 1 starts -->
                <br/>
                Page 1 Content

                <br clear="all" style="page-break-before:always" />
                
                
                <!-- Page 2 Starts -->
                <br/>
                Page 2 Content
                
                <br clear="all" style="page-break-before:always" />

                <!-- Page 3 Starts -->
                <br/>
                Page 3 Content
                
                <!--Header and Footer Starts-->
                <table id='hrdftrtbl' border='1' cellspacing='0' cellpadding='0'>
                    <tr>
                        <td>
                            <!--Header-->
                            <div style='mso-element:header' id="h1" >
                                <p class="MsoHeader">
                                    <table border="1" width="100%">
                                        <tr>
                                            <td width="30%">
                                                Left <br/>Header
                                            </td>
                                            <td align="center" width="40%">
                                                Center<br/> Header
                                            </td>
                                            <td align="right" width="30%">
                                                Right<br/>Header
                                            </td>
                                        </tr>
                                    </table>
                                </p>
                            </div>
                        </td>

                        <td>
                            <!--Footer-->
                            <div style='mso-element:footer' id="f1">
                                <p class="MsoFooter">
                                    <table width="100%" border="1" cellspacing="0" cellpadding="0">
                                        <tr>
                                            <td width="30%">
                                                Left<br/>Footer
                                            </td>
                                            <td align="center" width="40%">
                                                Center<br/>Footer
                                            </td>
                                            <td align="right" width="30%">
                                                Page <span style='mso-field-code: PAGE '></span> of <span style='mso-field-code: NUMPAGES '></span>
                                            </td>
                                        </tr>
                                    </table>
                                </p>
                            </div>
                        </td>
                    </tr>
                </table>
            </div><!--Section1 div ends-->
        </body>
    </html>
</apex:page>

 By: Parin

This was selected as the best answer
PrajnithPrajnith

Thanks a lot :)

 

 

 

Prajnith.

__sandra____sandra__

Thank you! You just saved my life! :D

miteshsuramiteshsura
Parin,

Probably I am missing something here? Can you explain what your post is doing. I tried and it pushed header and footer to both of the page.

I was hoping to generate a word doc with header and footer on every new page, just like renderas='pdf' works.

Thanks.
RepsGRepsG
Hi,

Brilliant piece of code, helped me heaps.

I have a question, how can I not display the header and footer on Page 1?


Georgi AtanasovGeorgi Atanasov
Hi RepsG,

I know that it passed lot of time, but if someone else needs answer for hiding Header and Footer on Page 1, you can just add "mso-title-page: yes;". Below is an example:

@page Section1{
                    size:8.5in 11.0in;
                    margin:1.2cm 0.5in 1.2cm 0.5in;
                    mso-header-margin:0.1in;
                    mso-title-page: yes;
                    mso-header:h1;
                    mso-footer:f1;
                    mso-footer-margin:0.2in;
                    mso-paper-source:0;
                }