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
charliemanicharliemani 

Word Document : Print Layout

Hi,

 

I have generated a word document in visualforce. The generated document get displayed in "Web Layout". Is it possible to display that in "Print Layout".

 

Suggestions please.

 

Regards,

Manikandan

sekharasekhara

@ Manikandan

 

Hi can u send code for print a word document from visual force , by clicking print button.


charliemani wrote:

Hi,

 

I have generated a word document in visualforce. The generated document get displayed in "Web Layout". Is it possible to display that in "Print Layout".

 

Suggestions please.

 

Regards,

Manikandan


 

M.sfM.sf

By Default MS word will take the contents in Text/Html Format.

 

Include the following code in VF Page.

<apex:page controller="TestMSWord" contentType="application/msWord#msword.doc" >
<html xmlns:w="urn:schemas-microsoft-com:office:word">
<apex:outputText value="{!PrintView}" escape="false"/>
<body>
//All your code Goes Here
</body>
</html>
</apex:page>


In class Add the following

public class TestMSWord{
    public String getPrintView()
    {
        return
        '<!--[if gte mso 9]>' +
            '<xml>' +
            '<w:WordDocument>' +
            '<w:View>Print</w:View>' +
            '<w:Zoom>100</w:Zoom>' +
            '<w:DoNotOptimizeForBrowser/>' +
            '</w:WordDocument>' +
            '</xml>' +
            '<![endif]>';
        }
    }

 

Explanation:

Add <html> tag with attributes since it should understand the schema is of word
Add <body> tag so that it will allow html tags like <h1>, <ul>,<li> etc
Add <Outputext> from class since directly vf page will not allow to save the html content with <w:> tag.This will enable print view by default.