Show footer in PDF file using iTextSharp in ASP.Net

Last Reply 4 months ago By pandeyism

Posted 4 months ago

I WANT TO SHOW FOOTER LINE IN THE PDF HOW TO DO IT?

GGHGDSHG

            PdfPTable tableb = new PdfPTable(2);
            tableb.TotalWidth = 500f;
            tableb.LockedWidth = true;
            float[] widthsss = new float[] { 0.1f, 0.1f };
            tableb.SetWidths(widthsss);
            tableb.DefaultCell.Padding = 10f;
            tableb.DefaultCell.BackgroundColor = Color.WHITE;
            tableb.DefaultCell.BorderColor = new iTextSharp.text.Color(191, 208, 247);
            tableb.HorizontalAlignment = Element.ALIGN_CENTER;
            tableb.SpacingBefore = 25f;
            tableb.SpacingAfter = 25f;
            PdfPCell headerb = new PdfPCell(new Phrase("Designation Information on " + DateTime.Now.ToString("dd.MM.yyyy"), FontFactory.GetFont("Calibri", 14)));
            headerb.Colspan = 9;
            headerb.Padding = 10;
            headerb.BackgroundColor = new iTextSharp.text.Color(191, 208, 247);
            headerb.BorderColor = new iTextSharp.text.Color(191, 208, 247);
            headerb.Indent = 10;
            headerb.HorizontalAlignment = 1;
            tableb.AddCell(headerb);
            tableb.AddCell(new Phrase("Designation ID", FontFactory.GetFont("Calibri", 10)));
            tableb.AddCell(new Phrase("Designation Name", FontFactory.GetFont("Calibri", 10)));

            con = new SqlDbConnect();
            con.SqlQuery("select DesignationID,DesignationName from tblDesignation order by DesignationID");
            con.RdrEx();
            while (con.Rdr.Read())
            {

                tableb.AddCell(new Phrase(con.Rdr[0].ToString(), FontFactory.GetFont("Calibri", 8)));
                tableb.AddCell(new Phrase(con.Rdr[1].ToString(), FontFactory.GetFont("Calibri", 8)));
            }
            con.conClose();
            doc.Add(tableb);
Posted 4 months ago

Hi smile,

Please refer below sample.

HTML

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="ContactName" HeaderText="Contact Name" />
        <asp:BoundField DataField="City" HeaderText="City" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
    </Columns>
</asp:GridView>
<br />
<asp:Button ID="btnExport" runat="server" Text="Export To PDF" OnClick="ExportToPDF" />

Namespaces

C#

using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        this.BindGrid();
    }
}

private void BindGrid()
{
    string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(strConnString))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT TOP 10* FROM Customers"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }
    }
}

protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    this.BindGrid();
}

protected void ExportToPDF(object sender, EventArgs e)
{
    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter hw = new HtmlTextWriter(sw))
        {
            GridView1.RenderControl(hw);
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A2, 20, 20, 25, 25);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter write = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();

            htmlparser.Parse(sr);
            write.PageEvent = new Footer();
            pdfDoc.Close();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Write(pdfDoc);
            Response.End();
        }
    }
}

public partial class Footer : PdfPageEventHelper
{
    public override void OnEndPage(PdfWriter writer, Document doc)
    {
        Paragraph footer = new Paragraph("Computer Generated Report", FontFactory.GetFont(FontFactory.TIMES_BOLD, 15, iTextSharp.text.Font.BOLD));
        footer.Alignment = Element.ALIGN_MIDDLE;
        PdfPTable footerTbl = new PdfPTable(1);
        footerTbl.TotalWidth = 250;
        footerTbl.HorizontalAlignment = Element.ALIGN_LEFT;
        footerTbl.AddCell(footer);
        footerTbl.WriteSelectedRows(0, -1, 400, 150, writer.DirectContent);
    }
}

public override void VerifyRenderingInServerForm(Control control)
{
    /* Verifies that the control is rendered */
}

Screenshot