Click here to Skip to main content
15,893,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
Iam genarating Pdf file from GridView Data.I want heading of that genarated file.Iam using this below code.


protected void btnpdf_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", "StudentInfo.pdf"));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
//To Export all pages
gridshow.AllowPaging = false;
if (ViewState["GridPageData"] != null)
{
gridshow.DataSource = ViewState["GridPageData"];
gridshow.DataBind();
}
else
{
ExecuteCheckedColumn();
}
pnlexport.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A2, 7f, 7f, 7f, 0f);
/*pdfDoc.AddTitle(""+ddltables.SelectedItem.Text+"");*/
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
}

Thanks&Regards
Hari
Posted

In your code after pdfDoc.Close() statement paste this code:
It Will Add Header as well as page numbers at bottom

// add page numbers
          Document copyDoc = new Document();
          PdfCopy copyPdf = new PdfCopy(copyDoc, new FileStream(filepath, FileMode.Create));
          copyPdf.SetPageSize(PageSize.A4.Rotate());
          copyDoc.Open();
          Font textFont = FontFactory.GetFont("Helvetica", 9, Font.NORMAL, BaseColor.BLACK);
          Font headerFont = FontFactory.GetFont("Helvetica", 12, Font.BOLD, BaseColor.BLACK);
          // read the initial pdf document
          PdfReader reader = new PdfReader(pdfStream.ToArray());
          int totalPages = reader.NumberOfPages;

          PdfImportedPage copiedPage = null;
          iTextSharp.text.pdf.PdfCopy.PageStamp stamper = null;

          for (int i = 0; i < totalPages; i++)
          {
              // get the page and create a stamper for that page
              copiedPage = copyPdf.GetImportedPage(reader, (i + 1));
              stamper = copyPdf.CreatePageStamp(copiedPage);

              // add a page number to the page
              //ColumnText.ShowTextAligned(stamper.GetUnderContent(), Element.ALIGN_CENTER, new Phrase((i + 1) + "/" + totalPages, textFont), 820f, 15, 0);
              ColumnText.ShowTextAligned(stamper.GetUnderContent(), Element.ALIGN_CENTER, new Phrase(String.Format("Page {0} of {1}", i + 1, reader.NumberOfPages)), 300f, 40, 0);
              ColumnText.ShowTextAligned(stamper.GetOverContent(), Element.ALIGN_CENTER, new Phrase("your page heading name", headerFont), 300f, 780, 0);

              stamper.AlterContents();

              // add the altered page to the new document
              copyPdf.AddPage(copiedPage);
          }

          copyDoc.Close();
          reader.Close();

          // flush and clear the document from memory
          pdfStream.Flush();
          pdfStream.Close();
 
Share this answer
 
v2
Hi Hari,

You could try this :

C#
private void addHeader(pdf iPdf)
         {
             try
             {
                 iPdf.addCell("US President", 14, 1, 3, 0);
                 iPdf.addCell("Wars started", 3);
                 iPdf.addCell("Body count", 3);
                 iPdf.addCell("Weapons used(WMD)", 3);
             }
             catch (Exception ex)
             {
                 lblErrMessage.Text = ex.Message;
             }
         }

Please note :

For iTextSharp version 5+, Header/Footer property has been removed. Now this can be done by us PageEventHandler class. Though it's not straight forward now but the upside is that now you can add more than just plan text in header and footer.

Please check the following links :

http://stackoverflow.com/questions/2321526/pdfptable-as-a-header-in-itextsharp
http://stackoverflow.com/questions/2598917/itextsharp-is-missing-headerfooter-class

Hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900