Hi Mudassar,
I have an ASP.NET Panel with various controls along with Gridview. I have to send the panel along with the gridview as pdf to users email on button click. Everything is working fine but the Gridview is missing its headers and footers in pdf.
Code:
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
pnlContents.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
// Sets the document to A4 size and rotates it so that the orientation of the page is Landscape.
Document pdfDoc = new Document(PageSize.A4.Rotate(), 0, 0, 15, 5);
//Document pdfDoc = new Document(new Rectangle(841f, 594f));
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
using (MemoryStream memoryStream = new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
MailMessage mail = new MailMessage();
mail.From = new MailAddress("");
mail.To.Add("");
mail.Subject = "Test";
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
mail.Body = "iTextSharp PDF Attachment";
mail.Attachments.Add(new Attachment(new MemoryStream(bytes), "CMOF.pdf"));
SmtpClient smtp = new SmtpClient("");
NetworkCredential NetworkCred = new NetworkCredential("", "");
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
}
}