Set a header/footer from any HTML string.

Namespace:  SautinSoft
Assembly:  SautinSoft.HtmlToRtf (in SautinSoft.HtmlToRtf.dll) Version: 7.1.4.17

Syntax

      
 C#  Visual Basic 
public void Html(
	string html
)
Public Sub Html ( _
	html As String _
)

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

CopyASP.Net - Convert HTML to DOCX using C#
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();

        // After purchasing the license, please insert your serial number here to activate the component.
        // h.Serial = "XXXXXXXXX";

        //here we specify page numbers
        h.PageStyle.PageNumbers.Appearance = SautinSoft.HtmlToRtf.ePageNumberingAppearence.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("");

        byte[] docxBytes = null;
         if (h.OpenHtml(htmlString))
            docxBytes = h.ToDocx();

        // Show result in the default DOCX viewer app.
        if (docxBytes != null)
        {
            Response.Clear();
            Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
            Response.AddHeader("content-disposition", "inline; filename=\"Result.docx\"");
            Response.BinaryWrite(docxBytes);
            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;
    }
}
CopyASP.Net - Convert HTML to DOCX using VB.Net
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()
        ' After purchasing the license, please insert your serial number here to activate the component.
        'h.Serial = "XXXXXXXXX"


        'here we specify page numbers
        h.PageStyle.PageNumbers.Appearance = SautinSoft.HtmlToRtf.ePageNumberingAppearence.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 docxBytes() As Byte = Nothing
        If h.OpenHtml(htmlString) Then
            docxBytes = h.ToDocx()
        End If

        ' Show result in the default DOCX viewer app.
        If docxBytes IsNot Nothing Then
            Response.Clear()
            Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
            Response.AddHeader("content-disposition", "inline; filename=""Result.docx""")
            Response.BinaryWrite(docxBytes)
            Response.Flush()
            Response.End()
        Else
            Result.Text = "Converting failed!"
        End If
    End Sub
    Public 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

See Also