Click here to Skip to main content
15,895,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I am creating a word doc from an html using vb.net. I have done that successfully with the help of the code similar to the one given in this site. But when I print that page or when I preview it, page number appears twice in the last page. This seems to be because the header and footer is repeating.

Following is the code-

C++
<pre>
<pre lang="vb">Dim UserId As String = &quot;&quot;
        Dim index As Integer
        Dim strPath As String
        Dim strLogin As String = Request.ServerVariables(&quot;LOGON_USER&quot;)
        index = (strLogin.IndexOf(&quot;\&quot;))
        strLogin = strLogin.Substring(index + 1)
        strLogin = EmployeeReviewId &amp; &quot;_&quot; &amp; lblEmployeeName.Text
        'strLogin = strLogin &amp; &quot;_[&quot; &amp; Session.SessionID &amp; &quot;]&quot;
        Dim guidResult As String = System.Guid.NewGuid().ToString()
        strLogin = strLogin &amp; &quot;_[&quot; &amp; guidResult &amp; &quot;]&quot;

        Dim sw As System.IO.StringWriter = New System.IO.StringWriter()
        Dim hw As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(sw)
        hw.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Html)

        strFileName1 = strLogin &amp; &quot;_&quot; &amp; strFileName &amp; &quot;.doc&quot;
        strPath = Server.MapPath(&quot;TempReport&quot;)
        strPath = strPath + &quot;\&quot; &amp; strFileName1
        RenderControlsCurrent(hw)
        hw.RenderEndTag()
        Response.Charset = &quot;&quot;
        Response.ContentType = &quot;application/ms-word&quot;
        HttpContext.Current.Response.ContentType = &quot;application/msword&quot;
        HttpContext.Current.Response.ContentEncoding = System.Text.UnicodeEncoding.UTF8
        Response.AddHeader(&quot;Content-Disposition&quot;, &quot;filename=&quot; &amp; strFileName &amp; &quot;.doc&quot;)
        Response.Write(&quot;&lt;html &quot; &amp; _
               &quot;xmlns:o='urn:schemas-microsoft-com:office:office' &quot; &amp; _
               &quot;xmlns:w='urn:schemas-microsoft-com:office:word'&quot; &amp; _
               &quot;xmlns='http://www.w3.org/TR/REC-html40'&gt;&quot;)
        Response.Write(&quot;&lt;head&gt;&quot;)

        Response.Write(&quot;&lt;!--[if gte mso 9]&gt;&quot; &amp; _
                               &quot;&lt;xml&gt;&quot; &amp; _
                               &quot;&lt;w:WordDocument&gt;&quot; &amp; _
                               &quot;&lt;w:View&gt;Print&lt;/w:View&gt;&quot; &amp; _
                               &quot;&lt;w:Zoom&gt;90&lt;/w:Zoom&gt;&quot; &amp; _
                               &quot;&lt;w:DoNotOptimizeForBrowser/&gt;&quot; &amp; _
                               &quot;&lt;/w:WordDocument&gt;&quot; &amp; _
                               &quot;&lt;/xml&gt;&quot; &amp; _
                               &quot;&lt;![endif]--&gt;&quot;)

        Response.Write(&quot;&lt;META HTTP-EQUIV=&quot;&quot;Content-Type&quot;&quot; CONTENT=&quot;&quot;text/html; charset=UTF-8&quot;&quot;&gt;&quot;)
        Response.Write(&quot;&lt;meta name=ProgId content=Word.Document&gt;&quot;)
        Response.Write(&quot;&lt;meta name=Generator content=&quot;&quot;Microsoft Word 9&quot;&quot;&gt;&quot;)
        Response.Write(&quot;&lt;meta name=Originator content=&quot;&quot;Microsoft Word 9&quot;&quot;&gt;&quot;)
        Response.Write(&quot;&lt;style&gt; body {display: block; page: Section1; counter-Reset: Page 1}&quot;)
        Response.Write(&quot;div.Section1 {page:Section1;page-break-after: avoid;}&quot;)
        Response.Write(&quot;div.Section2 {page:Section2;page-break-after: avoid;}&quot;)
        Response.Write(&quot;@page Section1 {size:841.7pt 595.45pt;mso-page-orientation:landscape;margin:.5in .5in .4in .5in;mso-header-margin:.1in;mso-footer-margin:.1in;mso-paper-source:0;}&quot;)
        Dim head As String = &quot;@page Section1 {mso-footer:f1;mso-header:h1;}&quot; &amp; _
                     &quot;div.Section1{page:Section1; page-break-after: avoid;}tr{page-break-before:always;page-break-inside:avoid;}&quot; &amp; _
         &quot;&lt;/style&gt;&lt;/head&gt;&quot;
        Response.Write(head)
        Response.Write(&quot;&lt;body&gt;&quot;)
        Response.Write(&quot;&lt;div class=Section1&gt;&quot;)
     Dim oStringWriter As New System.IO.StringWriter()
       Dim oHtmlTextWriter As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(oStringWriter)

       oHtmlTextWriter.Write(sw)

       Response.Output.Write(oStringWriter.ToString())

       Response.Write(&quot;&lt;span clear=&quot;&quot;all&quot;&quot; style=&quot;&quot;page-break-before:always&quot;&quot;&gt;&lt;/span&gt;&lt;div  style=&quot;&quot;mso-element:footer;font-family: GEORGIA; font-size: 11pt; text-align:center&quot;&quot; id=&quot;&quot;f1&quot;&quot; &gt;&quot;)
       Response.Write(&quot;&lt;span style='mso-tab-count:2'&gt;&lt;/span&gt;&lt;span style='mso-field-code:&quot;&quot; PAGE &quot;&quot;'&gt;&quot;)
       Response.Write(&quot;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&quot;)
    Response.Write(&quot;&lt;/div&gt;&quot;)
        Response.Write(&quot;&lt;/body&gt;&quot;)
        Response.Write(&quot;&lt;/html&gt;&quot;)
        HttpContext.Current.Response.Flush()
 Response.End()
</pre></pre>



Here RenderControlsCurrent(hw) is the method which contains the html to be converted to word. hw is of type "HtmlTextWriter"

Please help me with this.

Thanks in Advance.
Karthik
Posted
Updated 22-Aug-16 7:44am
v3
Comments
[no name] 4-Jul-11 3:22am    
Where is the code. let me see to help you out.
[no name] 4-Jul-11 3:24am    
Post your code so that we can help you where you were struck up.
Maciej Los 4-Jul-11 12:05pm    
Improve your question adding code in [code block] tag. Remove code from comment.
karthikkshetty 4-Jul-11 5:46am    
Here RenderControlsCurrent(hw) is the method which contains the html content.

1 solution

Please do not repost your question without adding actual information: if you haven't had a reply, then it will only annoy people.
 
Share this answer
 

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