Set header/footer from any HTML file
Namespace:
SautinSoftAssembly: SautinSoft.HtmlToRtf (in SautinSoft.HtmlToRtf.dll) Version: 4.6.10.19
Syntax
| C# | Visual Basic |
Parameters
- path
- String
Path to local HTML file
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(); //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 string htmlString = GetHtmlFromAspx(Path.Combine(Server.MapPath(""), @"Default.aspx")); h.BaseURL = Server.MapPath(""); string rtf = h.ConvertString(htmlString); //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!"; } } public static string GetHtmlFromAspx(string url) { string contents = ""; if (url.Length > 6) { //open 'http://' file if ((url[0] == 'h' || url[0] == 'H') && (url[1] == 't' || url[1] == 'T') && (url[2] == 't' || url[2] == 'T') && (url[3] == 'p' || url[3] == 'P') && url[4] == ':' && url[5] == '/' && url[6] == '/') { Stream StreamHttp = null; WebResponse resp = null; HttpWebRequest webrequest = null; try { webrequest = (HttpWebRequest)WebRequest.Create(url); resp = webrequest.GetResponse(); StreamHttp = resp.GetResponseStream(); StreamReader sr = new StreamReader(StreamHttp); contents = sr.ReadToEnd(); return contents; } catch { } } //local file else { try { StreamReader sr = new StreamReader(url); contents = sr.ReadToEnd(); sr.Close(); } catch { } } } return contents; } }
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() h.PageStyle.PageSize.Letter() h.PageStyle.PageMarginLeft.mm(25) Dim rtf As String = h.ConvertFileToString(Path.Combine(Server.MapPath(""), "Default.aspx")) 'show Word/rtf If rtf <> "" 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 End Class