1
Reply

Adding header and footer and aslo page number to pdf

Sweety

Sweety

Jul 2 2011 5:49 AM
11.3k

In my application i'm exporting datatable data to pdf using itextsharp its working fine now i want to add header and footer to it and aslo i want to add page number to it and aslo i want the header to contain name as report in all the page.How can i do that
i have written the code like this:
public void ExportToPdf(DataTable ExDataTable)

 
    //Here set page size as A4 
 
    Document pdfDoc = new Document(PageSize.A4, 10, 10, 10, 10); 
 
 
    try 
 
    { 
 
        PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream); 
 
        pdfDoc.Open(); 
 
 
        //Set Font Properties for PDF File 
 
        Font fnt = FontFactory.GetFont("Times New Roman", 6); 
 
 
        DataTable dt = ExDataTable; 
 
 
        if (dt != null) 
 
        { 
 
            PdfPTable PdfTable = new PdfPTable(dt.Columns.Count); 
 
 
            PdfPCell PdfPCell = null; 
 
 
            //Here we create PDF file tables 
 
            for (int rows = 0; rows < dt.Rows.Count; rows++) 
            { 
 
                if (rows == 0) 
 
                { 
 
                    for (int column = 0; column < dt.Columns.Count; column++) 
 
                    { 
 
                        PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Columns[column].ColumnName.ToString(), fnt))); 
 
                        PdfTable.AddCell(PdfPCell); 
 
                    } 
 
                } 
 
                for (int column = 0; column < dt.Columns.Count; column++) 
 
                { 
 
                    PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows][column].ToString(), fnt))); 
 
                    PdfTable.AddCell(PdfPCell); 
 
                } 
 
            } 
 
 
            // Finally Add pdf table to the document  
            pdfDoc.Add(PdfTable); 
 
        } 
 
 
        pdfDoc.Close(); 
 
 
        Response.ContentType = "application/pdf"; 
 
 
        //Set default file Name as current datetime 
        Response.AddHeader("content-disposition", "attachment; filename=" + "Line-Shift Report" + ".pdf"); 
 
 
        System.Web.HttpContext.Current.Response.Write(pdfDoc); 
 
 
        Response.Flush(); 
 
        Response.End(); 
 
 
    } 
 
    catch (Exception ex) 
 
    { 
 
        Response.Write(ex.ToString()); 
 
    } 
 

protected void Button1_Click1(object sender, EventArgs e) {
    if (Session["source_table"] != null) 
 
    { 
 
        ExportToPdf(Session["source_table"] as DataTable); 
 
    } 
 
    else 
    { 
 
        Response.Write("No data to Export!"); 
 
    } 
 

Can any1 help me on this


Answers (1)