SWAMY YT

SWAMY YT

  • 1k
  • 639
  • 28.5k

generate a pdf in asp.net c#

Oct 22 2018 3:30 AM
Hi,
 
i have been getting "Thread was being aborted" Exception when a report is being exporting to PDF.
 
i am using the following code to export report into a PDF.
  1. protected void btnPrint_Calculation(object sender, EventArgs e)  
  2. {  
  3. GeneratePrint();  
  4. }  
  5. public void GeneratePrint()  
  6. {  
  7. try  
  8. {  
  9. Document doc = new Document(new RectangleReadOnly(842, 595), 88f, 88f, 10f, 10f);  
  10. System.IO.MemoryStream MyStream = new System.IO.MemoryStream();  
  11. BaseColor Black = new BaseColor(00, 00, 00);  
  12. iTextSharp.text.Font MyFont = FontFactory.GetFont("vardana", 12, iTextSharp.text.Font.BOLD, Black);  
  13. PdfWriter Writer = PdfWriter.GetInstance(doc, MyStream);  
  14. doc.Open();  
  15. string HeaderTitle = "AIR ROTOR TECHNOLOGIES PVT LTD,";  
  16. string SubHeaderTitle = " 303,Center Facility Building 2,Sector 19,Vashi,";  
  17. string SubHeaderTitle1 = "chikmaglur";  
  18. PdfPTable HeaderTable = new PdfPTable(1);  
  19. PdfPCell HeaderCell = new PdfPCell(new Phrase(HeaderTitle, new Font(Font.FontFamily.HELVETICA, 15f, Font.BOLD, BaseColor.RED)));  
  20. PdfPCell SubHeaderCell = new PdfPCell(new Phrase(SubHeaderTitle, new Font(Font.FontFamily.HELVETICA, 14f, Font.BOLD, BaseColor.BLACK)));  
  21. PdfPCell SubHeaderCell1 = new PdfPCell(new Phrase(SubHeaderTitle1, new Font(Font.FontFamily.HELVETICA, 10f, Font.NORMAL, BaseColor.BLACK)));  
  22. HeaderCell.Border = 0;  
  23. HeaderTable.AddCell(HeaderCell);  
  24. HeaderTable.AddCell(SubHeaderCell);  
  25. HeaderTable.AddCell(SubHeaderCell1);  
  26. PdfPCell text = new PdfPCell(HeaderTable);  
  27. Response.Clear();  
  28. // Response.AddHeader("content-disposition", "attachment;filename=SaleRegisterReport.pdf");  
  29. Response.ContentType = "application/pdf";  
  30. Response.BinaryWrite(MyStream.ToArray());  
  31. Response.End();  
  32. doc.Close();  
  33. }  
  34. catch (Exception Ex)  
  35. {  
  36. Response.Write("While printing Caught an Error" + Ex.Message);  
  37. }  
  38. }  
 

Answers (7)