Set header/footer from any HTML string
Namespace:
SautinSoftAssembly: SautinSoft.HtmlToRtf (in SautinSoft.HtmlToRtf.dll) Version: 4.6.10.19
Syntax
Parameters
- html
- String
String in HTML format
Remarks
We don't recommend to use complex HTML documents as header or footer. The best is using simple HTML table with text and images as header/footer.


Examples
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Net; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Result.Text = ""; } protected void Button1_Click(object sender, EventArgs e) { SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf(); h.PageStyle.PageSize.Letter(); h.PageStyle.PageMarginLeft.mm(25); string rtf = h.ConvertFileToString(Path.Combine(Server.MapPath(""), @"Default.aspx")); //show Word/rtf if (rtf != "") { Response.Buffer = true; Response.Clear(); Response.ContentType = "application/msword"; Response.AddHeader("Content-Disposition:", "attachment; filename=Test.doc"); Response.Write(rtf); Response.Flush(); Response.End(); } else { Result.Text = "Converting failed!"; } } }
Imports System Imports System.Data Imports System.Configuration Imports System.Web Imports System.Web.Security Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Web.UI.WebControls.WebParts Imports System.Web.UI.HtmlControls Imports System.IO Imports System.Net Partial Public Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Result.Text = "" End Sub Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Dim h As New SautinSoft.HtmlToRtf() 'here we specify page numbers h.PageNumbers = SautinSoft.HtmlToRtf.ePageNumbers.PageNumFirst 'specify HTML format as string h.PageStyle.PageHeader.Html("<table border=""1""><tr><td>We added this header using the property ""Header.Html""</td></tr></table>") 'add footer from HTML file h.PageStyle.PageFooter.FromHtmlFile((Path.Combine(Server.MapPath(""), "footer.htm"))) 'get content of the ASPX page in HTML format Dim htmlString As String = GetHtmlFromAspx(Path.Combine(Server.MapPath(""), "Default.aspx")) h.BaseURL = Server.MapPath("") Dim rtf As String = h.ConvertString(htmlString) 'show Word/RTF If rtf IsNot Nothing Then Response.Buffer = True Response.Clear() Response.ContentType = "application/msword" Response.AddHeader("Content-Disposition:", "attachment; filename=Test.doc") Response.Write(rtf) Response.Flush() Response.End() Else Result.Text = "Converting failed!" End If End Sub Public Shared Function GetHtmlFromAspx(ByVal url As String) As String Dim contents As String = "" If url.Length > 6 Then 'open 'http://' file If (url.Chars(0) = "h"c OrElse url.Chars(0) = "H"c) AndAlso (url.Chars(1) = "t"c OrElse url.Chars(1) = "T"c) AndAlso (url.Chars(2) = "t"c OrElse url.Chars(2) = "T"c) AndAlso (url.Chars(3) = "p"c OrElse url.Chars(3) = "P"c) AndAlso url.Chars(4) = ":"c AndAlso url.Chars(5) = "/"c AndAlso url.Chars(6) = "/"c Then Dim StreamHttp As Stream = Nothing Dim resp As WebResponse = Nothing Dim webrequest As HttpWebRequest = Nothing Try webrequest = CType(WebRequest.Create(url), HttpWebRequest) resp = webrequest.GetResponse() StreamHttp = resp.GetResponseStream() Dim sr As New StreamReader(StreamHttp) contents = sr.ReadToEnd() Return contents Catch End Try 'local file Else Try Dim sr As New StreamReader(url) contents = sr.ReadToEnd() sr.Close() Catch End Try End If End If Return contents End Function End Class