Guest User

Untitled

a guest
Nov 17th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. protected void GenerateReport(object sender, EventArgs e)
  2. {
  3. Document document = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
  4. Font NormalFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, Color.BLACK);
  5. using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
  6. {
  7. PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
  8. Phrase phrase = null;
  9. PdfPCell cell = null;
  10. PdfPTable table = null;
  11. Color color = null;
  12.  
  13. document.Open();
  14.  
  15. //Header Table
  16. table = new PdfPTable(2);
  17. table.TotalWidth = 500f;
  18. table.LockedWidth = true;
  19. table.SetWidths(new float[] { 0.3f, 0.7f });
  20.  
  21.  
  22. //Company Name and Address
  23. phrase = new Phrase();
  24. phrase.Add(new Chunk(" pdf demo nn", FontFactory.GetFont("Arial", 16, Font.BOLD, Color.RED)));
  25. phrase.Add(new Chunk("Aurangabad, Maharashtra 431116,n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
  26. phrase.Add(new Chunk("Indian", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
  27. // phrase.Add(new Chunk("Seattle, USA", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
  28. cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
  29. cell.VerticalAlignment = PdfCell.ALIGN_TOP;
  30. table.AddCell(cell);
  31.  
  32. //Separater Line
  33. color = new Color(System.Drawing.ColorTranslator.FromHtml("#A9A9A9"));
  34. DrawLine(writer, 25f, document.Top - 79f, document.PageSize.Width - 25f, document.Top - 79f, color);
  35. DrawLine(writer, 25f, document.Top - 80f, document.PageSize.Width - 25f, document.Top - 80f, color);
  36. document.Add(table);
  37.  
  38. table = new PdfPTable(2);
  39. table.HorizontalAlignment = Element.ALIGN_LEFT;
  40. table.SetWidths(new float[] { 0.3f, 1f });
  41. table.SpacingBefore = 20f;
  42.  
  43. //Employee Details
  44. cell = PhraseCell(new Phrase("Certificate Record", FontFactory.GetFont("Arial", 12, Font.UNDERLINE, Color.BLACK)), PdfPCell.ALIGN_CENTER);
  45. cell.Colspan = 2;
  46. cell.PaddingTop = 30f;
  47. table.AddCell(cell);
  48. cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
  49. cell.Colspan = 2;
  50. cell.PaddingBottom = 30f;
  51.  
  52. table.AddCell(cell);
  53.  
  54. ////Photo
  55. //cell = ImageCell(string.Format("~/photos/{0}.jpg", dr["id"]), 25f, PdfPCell.ALIGN_CENTER);
  56. //table.AddCell(cell);
  57.  
  58.  
  59. //String tgender = dr["gender"].ToString();
  60. //String tchild_no = dr["child_no"].ToString();
  61.  
  62.  
  63. phrase = new Phrase();
  64. phrase.Add(new Chunk("प्रिया", FontFactory.GetFont("Arial", 10, Font.BOLD, Color.BLACK)));
  65. phrase.Add(new Chunk("name", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)));
  66. cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
  67. cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
  68. table.AddCell(cell);
  69. document.Add(table);
  70.  
  71. //DrawLine(writer, 160f, 80f, 160f, 690f, Color.WHITE);
  72. //DrawLine(writer, 115f, document.Top - 200f, document.PageSize.Width - 100f, document.Top - 200f, Color.WHITE);
  73.  
  74. table = new PdfPTable(2);
  75. table.SetWidths(new float[] { 0.5f, 2f });
  76. table.TotalWidth = 340f;
  77. table.LockedWidth = true;
  78. table.SpacingBefore = 20f;
  79. table.HorizontalAlignment = Element.ALIGN_RIGHT;
  80.  
  81.  
  82.  
  83. //name
  84. table.AddCell(PhraseCell(new Phrase("Name:", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
  85. table.AddCell(PhraseCell(new Phrase("" + "प्रिया", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)), PdfPCell.ALIGN_LEFT));
  86. cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
  87. cell.Colspan = 2;
  88. cell.PaddingBottom = 10f;
  89. table.AddCell(cell);
  90.  
  91.  
  92. document.Add(table);
  93. document.Close();
  94. byte[] bytes = memoryStream.ToArray();
  95. memoryStream.Close();
  96. Response.Clear();
  97. Response.ContentType = "application/pdf";
  98. Response.AddHeader("Content-Disposition", "attachment; filename=Certificate.pdf");
  99. Response.ContentType = "application/pdf";
  100. Response.Buffer = true;
  101. Response.Cache.SetCacheability(HttpCacheability.NoCache);
  102. Response.BinaryWrite(bytes);
  103. Response.End();
  104. Response.Close();
  105. }
  106. }
  107.  
  108. private static void DrawLine(PdfWriter writer, float x1, float y1, float x2, float y2, Color color)
  109. {
  110.  
  111. PdfContentByte contentByte = writer.DirectContent;
  112. contentByte.SetColorStroke(color);
  113. contentByte.MoveTo(x1, y1);
  114. contentByte.LineTo(x2, y2);
  115. contentByte.Stroke();
  116. }
  117. private static PdfPCell PhraseCell(Phrase phrase, int align)
  118. {
  119. PdfPCell cell = new PdfPCell(phrase);
  120. cell.BorderColor = Color.WHITE;
  121. cell.VerticalAlignment = PdfCell.ALIGN_TOP;
  122. cell.HorizontalAlignment = align;
  123. cell.PaddingBottom = 2f;
  124. cell.PaddingTop = 0f;
  125. return cell;
  126. }
  127.  
  128. private static PdfPCell ImageCell(string path, float scale, int align)
  129. {
  130. iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath(path));
  131. image.ScalePercent(scale);
  132. PdfPCell cell = new PdfPCell(image);
  133. cell.BorderColor = Color.WHITE;
  134. cell.VerticalAlignment = PdfCell.ALIGN_TOP;
  135. cell.HorizontalAlignment = align;
  136. cell.PaddingBottom = 0f;
  137. cell.PaddingTop = 0f;
  138. return cell;
  139. }
Add Comment
Please, Sign In to add comment