Sweety
Sweety

Reputation: 305

Adding header and footer and aslo page number to pdf

In my application I'm exporting datatable data to pdf its working fine now I want to add header and footer to it and also I want to add page number to it and also 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 anyone help me on this?

Upvotes: 0

Views: 5920

Answers (2)

VahidN
VahidN

Reputation: 19156

Create a new page events class:

public class PageEvents : PdfPageEventHelper  
   {        
       public override void OnStartPage(PdfWriter writer, Document document)  
       {  
           base.OnStartPage(writer, document);    
           document.Add(...header...);  
       }  
   }

and now in your codes:

   class Program  
   {  
       static void Main(string[] args)  
       {  
           using (var pdfDoc = new Document(...))  
           {  
               var pdfWriter = PdfWriter.GetInstance(pdfDoc, new FileStream("Test.pdf", FileMode.Create));  
               pdfWriter.PageEvent = new PageEvents();  
               ....

Upvotes: 1

MLS
MLS

Reputation: 895

you will have to create pdftables for header and for footer. Fill the cells with header images /headings. and in footer table u'll have to fill cell with page no.

Upvotes: 0

Related Questions