Set header/footer from any HTML file.

Namespace:  SautinSoft
Assembly:  PdfMetamorphosis (in PdfMetamorphosis.dll) Version: 6.7.12.19

Syntax

      
 C#  Visual Basic 
public void FromHtmlFile(
	string path
)
Public Sub FromHtmlFile ( _
	path As String _
)

Parameters

path
String
Path to local HTML file

Remarks

We don't recommend to use it for complex HTML documents as header or footer. The best is using simple HTML table with text and images as header/footer.

Examples

CopyView ASPX in PDF with specifying page header from HTML file
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;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Create a simple ASP.Net page with table inside

        if (!Page.IsPostBack)
        {
            //Create a simple ASP.Net table
            Table table = new Table();
            TableRow row = null;
            TableCell cell = null;

            int rows = 5;
            int cells = 5;
            for (int x = 0; x < rows; x++)
            {
                row = new TableRow();
                for (int y = 0; y < cells; y++)
                {
                    cell = new TableCell();
                    cell.Width = Unit.Pixel(80);
                    cell.BorderColor = System.Drawing.ColorTranslator.FromHtml("darkgreen");
                    cell.BorderWidth = Unit.Pixel(2);
                    cell.Font.Name = "Helvetica";
                    cell.Font.Size = FontUnit.Point(10);
                    cell.HorizontalAlign = HorizontalAlign.Center;

                    cell.Text = "Row " + ((int)(x + 1)).ToString() + ", Cell " + ((int)(y + 1)).ToString();
                    row.Cells.Add(cell);
                }
                table.Rows.Add(row);
            }

            //Add table to page
            Panel1.Controls.Add(table);
        }
    }

    //Get HTML from ASPX
    protected override void Render(HtmlTextWriter writer)
    {
        // setup a TextWriter to capture the markup
        TextWriter tw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(tw);

        // render the markup into our surrogate TextWriter
        base.Render(htw);

        // get the captured markup as a string
        string currentPage = tw.ToString();

        //Save HTML code to tempory file 
        if (!Page.IsPostBack)
            File.WriteAllText(Path.Combine(Server.MapPath("Temp"), "temp.htm"), currentPage, Encoding.UTF8);

        // render the markup into the output stream verbatim
        writer.Write(currentPage);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Get HTML from temporary file which we've created in the overriden method Render()
        string html = File.ReadAllText(Path.Combine(Server.MapPath("Temp"), "temp.htm"), Encoding.UTF8);

        string url = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;

        if (html.Length > 0)
        {
            SautinSoft.PdfMetamorphosis p = new SautinSoft.PdfMetamorphosis();
            //After purchasing the license, please insert your serial number here to activate the component
            //p.Serial = "XXXXXXXXXXX";

            p.HtmlSettings.BaseUrl = url;

            byte[] pdf = p.HtmlToPdfConvertStringToByte(html);

            //show PDF
            if (pdf != null)
            {
                Response.Buffer = true;
                Response.Clear();
                Response.ContentType = "application/PDF";
                Response.AddHeader("Content-Disposition:", "attachment; filename=Result.pdf");
                Response.BinaryWrite(pdf);
                Response.Flush();
                Response.End();
            }

        }
    }
}
CopyView ASPX in PDF with specifying page header from HTML file
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

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        'Create a simple ASP.Net page with table inside


        If Not Page.IsPostBack Then
            'Create a simple ASP.Net table
            Dim table_Renamed As New Table()
            Dim row As TableRow = Nothing
            Dim cell As TableCell = Nothing

            Dim rows As Integer = 5
            Dim cells As Integer = 5
            For x As Integer = 0 To rows - 1
                row = New TableRow()
                For y As Integer = 0 To cells - 1
                    cell = New TableCell()
                    cell.Width = Unit.Pixel(80)
                    cell.BorderColor = System.Drawing.ColorTranslator.FromHtml("darkgreen")
                    cell.BorderWidth = Unit.Pixel(2)
                    cell.Font.Name = "Helvetica"
                    cell.Font.Size = FontUnit.Point(10)
                    cell.HorizontalAlign = HorizontalAlign.Center

                    cell.Text = "Row " & (CInt(Fix(x + 1))).ToString() & ", Cell " & (CInt(Fix(y + 1))).ToString()
                    row.Cells.Add(cell)
                Next y
                table_Renamed.Rows.Add(row)
            Next x

            'Add table to page
            Panel1.Controls.Add(table_Renamed)
        End If
    End Sub
    'Get HTML from ASPX
    Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
        ' setup a TextWriter to capture the markup
        Dim tw As TextWriter = New StringWriter()
        Dim htw As New HtmlTextWriter(tw)

        ' render the markup into our surrogate TextWriter
        MyBase.Render(htw)

        ' get the captured markup as a string
        Dim currentPage As String = tw.ToString()

        'Save HTML code to tempory file 
        If Not Page.IsPostBack Then
            File.WriteAllText(Path.Combine(Server.MapPath("Temp"), "temp.htm"), currentPage, Encoding.UTF8)
        End If

        ' render the markup into the output stream verbatim
        writer.Write(currentPage)
    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        'Get HTML from temporary file which we've created in the overriden method Render()
        Dim html As String = File.ReadAllText(Path.Combine(Server.MapPath("Temp"), "temp.htm"), Encoding.UTF8)

        Dim url As String = System.Web.HttpContext.Current.Request.Url.AbsoluteUri

        If html.Length > 0 Then
            Dim p As New SautinSoft.PdfMetamorphosis()

            'After purchasing the license, please insert your serial number here to activate the component
            'p.Serial = "XXXXXXXXXXX"


            p.HtmlSettings.BaseUrl = url

            Dim pdf() As Byte = p.HtmlToPdfConvertStringToByte(html)

            'show PDF
            If pdf IsNot Nothing Then
                Response.Buffer = True
                Response.Clear()
                Response.ContentType = "application/PDF"
                Response.AddHeader("Content-Disposition:", "attachment; filename=Result.pdf")
                Response.BinaryWrite(pdf)
                Response.Flush()
                Response.End()
            End If

        End If
    End Sub
End Class

See Also