0

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?

svick
  • 236,525
  • 50
  • 385
  • 514
Sweety
  • 215
  • 1
  • 7
  • 15
  • What library are you using to generate the PDF? The documentation should indicate how to do what you are looking to do. In any event, you probably have a better chance of getting an answer if you state what library (e.g., ActivePDF, etc) you're using. – Tim Jul 02 '11 at 09:37
  • @Tim-Im using itextsharp library – Sweety Jul 02 '11 at 09:40
  • I'd recommend doing a search on ITextSharp and adding header/footers, page numbers, etc - there's a number of posts here on SO about these topics. You also might want to add a tag for ITextSharp to your question. Unfortunately I haven't used ITextSharp. – Tim Jul 02 '11 at 09:49
  • @Sweety you got solution? actually i'm trying the same using itextsharp. while searching i found your post. if you got solution can you please tell me how should i achieve the same? – OnkarDhane Nov 19 '12 at 09:31
  • I recently added an answer that does not use pageEvents - http://stackoverflow.com/questions/18996323/add-header-and-footer-for-pdf-using-itextsharp#answer-20059613 – Dom Nov 18 '13 at 22:51

2 Answers2

1

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();  
               ....
VahidN
  • 18,457
  • 8
  • 73
  • 117
0

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.

MLS
  • 885
  • 1
  • 9
  • 16