Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please help me to display background image in word file using html text. Using below code, Image is displayed but I am not able to display background image in word file.


XML
{
        StringBuilder strBody = new StringBuilder();
        string siteURL = "http://localhost/images/BG.jpg"; //Put your Image path

        strBody.Append("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");

        strBody.Append("<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'>");

        strBody.Append("<head>");
        strBody.Append("<title>Report</title>");
        strBody.Append("<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/><meta name=ProgId content=Word.Document><meta name=Generator content=\"Microsoft Word 11\"><meta name=Originator content=\"Microsoft Word 11\">");

        strBody.Append("<!--[if gte mso 9]><xml><w:WordDocument><w:View>Print</w:View></w:WordDocument></xml><![endif]-->");

        strBody.Append("<!--[if !mso]><style>v\\:* {behavior:url(#default#VML); position: relative;display:inline-block}</style><![endif]-->");

        strBody.Append("<style>");
        strBody.Append("@page Section1 {size:8.5in 11.0in;margin:0.8in 0.6in 1in 0.4in; mso-header-margin:.5in; mso-footer-margin:.3in; mso-paper-source:0;mso-title-page:yes;}");

        strBody.Append("p.MsoFooter, li.MsoFooter, div.MsoFooter {margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan;font-size:10pt;font-family:Arial;}");

        strBody.Append("p.MsoHeader, li.MsoHeader, div.MsoHeader {margin:0in;margin-bottom:.0001pt; mso-pagination:widow-orphan;tab-stops:center 3.0in right 6.0in;}");

        strBody.Append("div.Section1{page:Section1;font-size:10pt;font-family:Arial}");
        strBody.Append("</style>");
        strBody.Append("</head>");

        strBody.Append("<body bgcolor=white lang=EN-US style=\"tab-interval:.5in\" background='" + siteURL + "'>");

        strBody.Append("<!--[if gte mso 9]><xml><v:background id=\"_x0000_s1025\" o:bwmode=\"white\" o:targetscreensize=\"800,600\"><v:fill o:HRef='" + siteURL + "' o:title=\"BGImage\" type=\"frame\"/></v:background></xml><![endif]-->");

        strBody.Append("<div class=Section1>");
        strBody.Append("<img src='" + siteURL + "' width='500' height='500'>");
        strBody.Append("<br><br><br>ADD BACKGROUNG IMAGE IN WORD FILE USING HTML TEXT");
        strBody.Append("</div>");
        strBody.Append("</body>");
        strBody.Append("</html>");

        Response.AppendHeader("Content-Type", "application/msword");
        Response.AppendHeader("Content-disposition", "attachment; filename=Report.doc");
        Response.Write(strBody);
    }
Posted
Updated 21-Oct-11 19:44pm
v5

1 solution

Start by printing the html to a file.

Then use that file to modify the url/html until it works. So you are ONLY changing html (not code as in the above.)

AFTER you figure that out then write code that creates the html.
 
Share this answer
 
Comments
Naveriya Shital 22-Oct-11 2:52am    
jschell, thanks a lot for solution.
I tried your way and it worked.
Actually word has a tag DisplayBackgroundShape to display background images.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900