使用HTML文本在Word文档中显示背景图像

问题描述:

请帮助我使用html文本在Word文件中显示背景图片.使用以下代码,显示了图像,但我无法在Word文件中显示背景图像.


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.


{
        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);
    }

首先将html打印到文件中.

然后使用该文件修改url/html,直到它起作用为止.因此,您只能更改html(而不是上面的代码.)

确定这一点后,再编写创建html的代码.
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.