Creating PDFs on Fly with ASP.Net for Free of Cost

Thursday, October 16, 2008
Here I am going to show you how we can create PDFs with ASP.Net with Free of cost. There is tool called “iTextSharp” using this dll we can create pdfs. Not only pdfs we can able to crate rtf also. iTextSharp provides various classes to make pdf with different options.
In the generated pdf file we can add tables, Images, Links etc. Here in I am explaining with some examples.
First add the ‘iTextSharp” dll to your applications.

  1. using sharp=iTextSharp.text;

  2. using iTextSharp.text.pdf;

  3. using iTextSharp.text.xml;



Creating Sample PDF

  1. sharp.Document document = new sharp.Document();

  2. string path = Server.MapPath(".") + "\\" + "Sample.pdf";

  3. PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));

  4. document.Open();

  5. //Here I am adding a Paragraph to the document

  6. document.Add(new sharp.Paragraph("Its a Sample Example for Creating PDF File Using iTextSharp. Its For Free of Cost."));

  7. document.Close();



PDF with Different Fonts

  1. sharp.Document document = new sharp.Document();

  2. string path = Server.MapPath(".") + "\\" + "Fonts.pdf";

  3. PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));

  4. document.Open();


  5. sharp.Phrase myPhrase = new sharp.Phrase("Hello 1! ", new sharp.Font(sharp.Font.TIMES_ROMAN, 8, sharp.Font.BOLD));

  6. myPhrase.Add(new sharp.Phrase("some other font ", new sharp.Font(sharp.Font.HELVETICA, 8)));

  7. myPhrase.Add(new sharp.Phrase("This is the end of the sentence.\n", new sharp.Font(sharp.Font.TIMES_ROMAN, 8, sharp.Font.ITALIC)));

  8. document.Add(myPhrase);


  9. myPhrase = new sharp.Phrase("Hello 1bis! ", sharp.FontFactory.GetFont(sharp.FontFactory.TIMES_ROMAN, 8, sharp.Font.BOLD));

  10. myPhrase.Add(new sharp.Phrase("some other font ", sharp.FontFactory.GetFont(sharp.FontFactory.HELVETICA, 8)));

  11. myPhrase.Add(new sharp.Phrase("This is the end of the sentence.\n", sharp.FontFactory.GetFont(sharp.FontFactory.TIMES_ROMAN, 8, sharp.Font.ITALIC)));

  12. document.Add(myPhrase);


  13. sharp.Paragraph myParagraph = new sharp.Paragraph("Hello 2! ", new sharp.Font(sharp.Font.TIMES_ROMAN, 8, sharp.Font.BOLD));

  14. myParagraph.Add(new sharp.Paragraph("This is the end of the sentence.", new sharp.Font(sharp.Font.TIMES_ROMAN, 8, sharp.Font.ITALIC)));

  15. document.Add(myParagraph);


  16. myParagraph = new sharp.Paragraph(12);

  17. myParagraph.Add(new sharp.Paragraph("Hello 3! ", new sharp.Font(sharp.Font.TIMES_ROMAN, 8, sharp.Font.BOLD)));

  18. myParagraph.Add(new sharp.Paragraph("This is the end of the sentence.", new sharp.Font(sharp.Font.TIMES_ROMAN, 8, sharp.Font.ITALIC)));

  19. document.Add(myParagraph);


  20. myPhrase = new sharp.Phrase(12);

  21. myPhrase.Add(new sharp.Phrase("Hello 4! ", new sharp.Font(sharp.Font.TIMES_ROMAN, 8, sharp.Font.BOLD)));

  22. myPhrase.Add(new sharp.Phrase("This is the end of the sentence.\n", new sharp.Font(sharp.Font.TIMES_ROMAN, 8, sharp.Font.ITALIC)));

  23. document.Add(myPhrase);


  24. myPhrase = new sharp.Phrase(12);

  25. myPhrase.Add(new sharp.Phrase("Hello 5! ", new sharp.Font(sharp.Font.COURIER, 12, sharp.Font.BOLD, sharp.Color.RED)));

  26. myPhrase.Add(new sharp.Phrase("This Font in Blue Color.\n", new sharp.Font(sharp.Font.COURIER, 12, sharp.Font.ITALIC, sharp.Color.BLUE)));

  27. document.Add(myPhrase);


  28. document.Close();



Creating Links, Lists, Annotations

  1. // Here I am Creating Multiple Pages.

  2. sharp.Document document = new sharp.Document(sharp.PageSize.A4, 50, 50, 50, 50);

  3. string path = Server.MapPath(".") + "\\" + "Links.pdf";

  4. PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));


  5. document.Open();


  6. //Page 1 Links

  7. document.Add(new sharp.Paragraph("Sample Link: \n"));

  8. sharp.Paragraph para = new sharp.Paragraph("Here is link for my blog:");

  9. sharp.Anchor anchor = new sharp.Anchor("Ramcrishna.Blogspot.com", sharp.FontFactory.GetFont(sharp.FontFactory.HELVETICA_BOLD, 12,sharp.Font.UNDERLINE, new sharp.Color(0,0,255)));

  10. anchor.Reference = "http://ramcrishna.blogspot.com";

  11. anchor.Name = "top";

  12. para.Add(anchor);

  13. document.Add(para);


  14. //Page 2 Lists

  15. document.NewPage();

  16. document.Add(new sharp.Paragraph("Sample List: \n"));

  17. sharp.List lst = new sharp.List(true, 20);

  18. lst.Add(new sharp.ListItem("Fist Item"));

  19. lst.Add(new sharp.ListItem("Second Item"));

  20. lst.Add(new sharp.ListItem("Third Item"));

  21. document.Add(lst);


  22. sharp.Paragraph para1 = new sharp.Paragraph("Different List Type:");

  23. lst = new sharp.List(false,10);

  24. lst.Add("ASP.Net");

  25. lst.Add("VB.Net");

  26. lst.Add("C#.Net");

  27. para1.Add(lst);

  28. document.Add(para1);



  29. //Page 3 Annotations

  30. document.NewPage();

  31. document.Add(new sharp.Paragraph("Sample Annotations: \n"));

  32. PdfContentByte cb = writer.DirectContent;


  33. cb.SetRGBColorStroke(0x00, 0x00, 0xFF);

  34. cb.Rectangle(100, 700, 100, 100);

  35. cb.Stroke();

  36. sharp.Annotation annot = new sharp.Annotation(100f, 700f, 200f, 800f, "http://ramcrishna.blogspot.com");

  37. document.Add(annot);

  38. cb.SetRGBColorStroke(0xFF, 0x00, 0x00);

  39. cb.Rectangle(200, 700, 100, 100);

  40. cb.Stroke();

  41. try

  42. {

  43. document.Add(new sharp.Annotation(200f, 700f, 300f, 800f, new Uri("http://www.asp.net")));

  44. }

  45. catch

  46. {

  47. }

  48. cb.SetRGBColorStroke(0x00, 0xFF, 0x00);

  49. cb.Rectangle(300, 700, 100, 100);

  50. cb.Stroke();

  51. document.Add(new sharp.Annotation(300f, 700f, 400f, 800f, "http://ramcrishna.blogspot.com"));

  52. cb.SetRGBColorStroke(0x00, 0x00, 0xFF);

  53. cb.Rectangle(100, 500, 100, 100);

  54. cb.Stroke();



  55. document.Add(new sharp.Annotation("Annotation", "Its a Sample Annotation",100f, 500f, 200f, 600f));

  56. cb.SetRGBColorStroke(0xFF, 0x00, 0x00);

  57. cb.Rectangle(200, 500, 100, 100);

  58. cb.Stroke();


  59. document.Add(new sharp.Annotation(200f, 500f, 300f, 600f, "http://ramcrishna.blogspot.com"));

  60. cb.SetRGBColorStroke(0x00, 0xFF, 0x00);

  61. cb.Rectangle(300, 500, 100, 100);

  62. cb.Stroke();

  63. document.Add(new sharp.Annotation(300f, 500f, 400f, 600f, "http://ramcrishna.blogspot.com"));


  64. document.Close();



Creating Headers & Footers

  1. // Creating Headers & Footer


  2. sharp.Document document = new sharp.Document();

  3. string path = Server.MapPath(".") + "\\" + "Header.pdf";

  4. PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));

  5. document.Open();


  6. sharp.HeaderFooter hf = new sharp.HeaderFooter(new sharp.Phrase("Header Text"), false);

  7. document.Header = hf;


  8. sharp.HeaderFooter foot = new sharp.HeaderFooter(new sharp.Phrase("Footer Text"), false);

  9. document.Footer = foot;

  10. document.Open();

  11. document.Add(new sharp.Paragraph("Page 1"));

  12. document.NewPage();

  13. document.Add(new sharp.Paragraph("Page 2"));

  14. document.Close();



Creating Tables

  1. // Creating tables

  2. sharp.Document document = new sharp.Document();

  3. string path = Server.MapPath(".") + "\\" + "Tables.pdf";

  4. PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));

  5. document.Open();

  6. // Static Table with 2 rows and 2 columns

  7. sharp.Table tbl = new sharp.Table(2,2);

  8. tbl.Cellpadding = 3;

  9. tbl.Cellspacing = 1;

  10. tbl.BorderColor = new sharp.Color(0, 0, 255);

  11. tbl.AddCell("Col1");

  12. tbl.AddCell("Col2");

  13. tbl.AddCell("Col3");

  14. tbl.AddCell("Col4");

  15. document.Add(tbl);


  16. //Dynamic table from Dataset

  17. document.NewPage();


  18. DataTable dt = new DataTable();

  19. DataColumn dtcol;

  20. DataRow dr;


  21. dtcol = new DataColumn("UserName");

  22. dt.Columns.Add(dtcol);

  23. dtcol = new DataColumn("FName");

  24. dt.Columns.Add(dtcol);

  25. dtcol = new DataColumn("LName");

  26. dt.Columns.Add(dtcol);

  27. dtcol = new DataColumn("City");

  28. dt.Columns.Add(dtcol);

  29. dtcol = new DataColumn("Email");

  30. dt.Columns.Add(dtcol);

  31. dtcol = new DataColumn("Mobile");

  32. dt.Columns.Add(dtcol);


  33. dr = dt.NewRow();

  34. dr["UserName"] = "Ram";

  35. dr["FName"] = "Ram";

  36. dr["LName"] = "Krishna ";

  37. dr["City"] = "Hyderabad";

  38. dr["Email"] = "n.ramakrishna27@gmail.com";

  39. dr["Mobile"] = "123456";

  40. dt.Rows.Add(dr);


  41. dr = dt.NewRow();

  42. dr["UserName"] = "Naveen";

  43. dr["FName"] = "Naveen";

  44. dr["LName"] = "Reddy";

  45. dr["City"] = "Sec-Bad";

  46. dr["Email"] = "naveen@yahoo.co.in";

  47. dr["Mobile"] = "4210";

  48. dt.Rows.Add(dr);


  49. dr = dt.NewRow();

  50. dr["UserName"] = "Raj";

  51. dr["FName"] = "Raja";

  52. dr["LName"] = "Shekar";

  53. dr["City"] = "Banglore";

  54. dr["Email"] = "raj4u@yahoo.com";

  55. dr["Mobile"] = "01200";

  56. dt.Rows.Add(dr);


  57. dr = dt.NewRow();

  58. dr["UserName"] = "Suresh";

  59. dr["FName"] = "Suresh";

  60. dr["LName"] = "Kumar";

  61. dr["City"] = "Chennai";

  62. dr["Email"] = "suri@hotmail.com";

  63. dr["Mobile"] = "10120";

  64. dt.Rows.Add(dr);


  65. dr = dt.NewRow();

  66. dr["UserName"] = "Ravi";

  67. dr["FName"] = "Ravi";

  68. dr["LName"] = "Kumar";

  69. dr["City"] = "Vizag";

  70. dr["Email"] = "ravi@gmail.com";

  71. dr["Mobile"] = "1234";

  72. dt.Rows.Add(dr);


  73. dr = dt.NewRow();

  74. dr["UserName"] = "Shaik";

  75. dr["FName"] = "Shaik";

  76. dr["LName"] = "Khan";

  77. dr["City"] = "Vijayawada";

  78. dr["Email"] = "shaik@rediff.com";

  79. dr["Mobile"] = "1234";

  80. dt.Rows.Add(dr);


  81. dr = dt.NewRow();

  82. dr["UserName"] = "Kiran";

  83. dr["FName"] = "Kiran";

  84. dr["LName"] = "Kumar";

  85. dr["City"] = "Hyderabad";

  86. dr["Email"] = "kiran@yahoo.com";

  87. dr["Mobile"] = "12453";

  88. dt.Rows.Add(dr);


  89. dr = dt.NewRow();

  90. dr["UserName"] = "Santu";

  91. dr["FName"] = "Santhosh";

  92. dr["LName"] = "Reddy";

  93. dr["City"] = "Hyderabad";

  94. dr["Email"] = "santu@yahoo.com";

  95. dr["Mobile"] = "123";

  96. dt.Rows.Add(dr);


  97. dr = dt.NewRow();

  98. dr["UserName"] = "Ashfak";

  99. dr["FName"] = "Ashfak";

  100. dr["LName"] = "Khan";

  101. dr["City"] = "Hyderabad";

  102. dr["Email"] = "ash@yahoo.com";

  103. dr["Mobile"] = "123";

  104. dt.Rows.Add(dr);


  105. dr = dt.NewRow();

  106. dr["UserName"] = "Sajju";

  107. dr["FName"] = "Sajju";

  108. dr["LName"] = "MD";

  109. dr["City"] = "Hyderabad";

  110. dr["Email"] = "sajju@gmail.com";

  111. dr["Mobile"] = "123";

  112. dt.Rows.Add(dr);


  113. int rowcount, coloumscount;

  114. rowcount = dt.Rows.Count;

  115. coloumscount = dt.Columns.Count;


  116. PdfPTable pdftbl = new PdfPTable(coloumscount);

  117. pdftbl.DefaultCell.Padding = 3;

  118. float[] headerwidths = { 9, 9, 8, 10, 12, 8 };

  119. pdftbl.SetWidths(headerwidths);

  120. pdftbl.WidthPercentage = 100;

  121. pdftbl.DefaultCell.BorderWidth = 2;

  122. pdftbl.DefaultCell.HorizontalAlignment = sharp.Element.ALIGN_CENTER;


  123. pdftbl.AddCell("UserName");

  124. pdftbl.AddCell("FName");

  125. pdftbl.AddCell("LName");

  126. pdftbl.AddCell("City");

  127. pdftbl.AddCell("Email");

  128. pdftbl.AddCell("Mobile");


  129. pdftbl.HeaderRows = 1;

  130. pdftbl.DefaultCell.BorderWidth = 1;


  131. int max = dt.Rows.Count;

  132. for (int row = 1,j=1 ; row < rowcount j<max; row++,j++)

  133. {

  134. if (j % 2 == 1)

  135. {

  136. pdftbl.DefaultCell.GrayFill = 0.9f;

  137. }

  138. for (int colm = 0; colm < coloumscount; colm++)

  139. {

  140. pdftbl.AddCell(dt.Rows[row - 1][colm].ToString());


  141. }

  142. if (j % 2 == 1)

  143. {

  144. pdftbl.DefaultCell.GrayFill = 0.4f;

  145. }

  146. }

  147. document.Add(pdftbl);

  148. document.Close();



PDF with Image

  1. // Creating Images And water Marks

  2. sharp.Document document = new sharp.Document();

  3. string path = Server.MapPath(".") + "\\" + "Images.pdf";

  4. PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));

  5. document.Open();


  6. //Adding Images

  7. string imgpath = Server.MapPath("images") + "\\" + "img1.jpg";

  8. sharp.Image img = sharp.Image.GetInstance(imgpath);

  9. img.ScalePercent(50);

  10. document.Add(img);

  11. document.Close();



Creating Password to PDF

  1. sharp.Document document = new sharp.Document();

  2. string path = Server.MapPath(".") + "\\" + "Password.pdf";

  3. PdfWriter pd = PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));

  4. pd.SetEncryption(PdfWriter.STRENGTH128BITS, "userpass", "password", PdfWriter.AllowPrinting PdfWriter.AllowCopy);

  5. document.Open();

  6. document.Add(new sharp.Paragraph("Congrats, You can access this file now."));

  7. document.Close();



Creating BarCodes in PDF

  1. sharp.Document document = new sharp.Document(sharp.PageSize.A4, 50, 50, 50, 50);

  2. string path = Server.MapPath(".") + "\\" + "d.pdf";

  3. PdfWriter pd = PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));

  4. document.Open();

  5. PdfContentByte cb = pd.DirectContent;

  6. sharp.pdf.Barcode39 code39 = new sharp.pdf.Barcode39();

  7. code39.Code = "CODE39-1234567890";

  8. code39.StartStopText = false;

  9. sharp.Image image39 = code39.CreateImageWithBarcode(cb, null, null);

  10. sharp.pdf.Barcode39 code39ext = new sharp.pdf.Barcode39();

  11. code39ext.Code = "The willows.";

  12. code39ext.StartStopText = false;

  13. code39ext.Extended = true;

  14. sharp.Image image39ext = code39ext.CreateImageWithBarcode(cb, null, null);

  15. sharp.pdf.Barcode128 code128 = new sharp.pdf.Barcode128();

  16. code128.Code = "1Z234786 hello";

  17. sharp.Image image128 = code128.CreateImageWithBarcode(cb, null, null);

  18. sharp.pdf.BarcodeEAN codeEAN = new sharp.pdf.BarcodeEAN();

  19. codeEAN.CodeType = BarcodeEAN.EAN13;

  20. codeEAN.Code = "9780201615883";

  21. sharp.Image imageEAN = codeEAN.CreateImageWithBarcode(cb, null, null);


  22. PdfPTable table = new PdfPTable(2);

  23. table.WidthPercentage = 100;

  24. table.DefaultCell.Border = sharp.Rectangle.NO_BORDER;

  25. table.DefaultCell.HorizontalAlignment = sharp.Element.ALIGN_CENTER;

  26. table.DefaultCell.VerticalAlignment = sharp.Element.ALIGN_MIDDLE;

  27. table.DefaultCell.FixedHeight = 70;

  28. table.AddCell("CODE 39");

  29. table.AddCell(new sharp.Phrase(new sharp.Chunk(image39, 0, 0)));

  30. table.AddCell("CODE 39 EXTENDED");

  31. table.AddCell(new sharp.Phrase(new sharp.Chunk(image39ext, 0, 0)));

  32. table.AddCell("CODE 128");

  33. table.AddCell(new sharp.Phrase(new sharp.Chunk(image128, 0, 0)));

  34. table.AddCell("CODE EAN");

  35. table.AddCell(new sharp.Phrase(new sharp.Chunk(imageEAN, 0, 0)));


  36. document.Add(table);

  37. document.Close();



You Can Find more samples here http://itextsharp.sourceforge.net/tutorial/ch01.html
Download Complete Application of Above Examples: Click Here

4 comments:

Hasan said...

I found this blog is important to me, for that am thanked you...

and i had copied this blog to my blog in http://monusan.blogspot.com/2008/11/creating-pdfs-on-fly-with-aspnet-for.html

but all the credit goes to you...

thank you...

RRave said...

Invitation to new programming Resources Website



Dear Sir,

I hope you are doing well. I got this email address from one of your contribution web site. I have launched a web site www.codegain.com and it is basically aimed C#,JAVA,VB.NET,ASP.NET,AJAX,Sql Server,Oracle,WPF,WCF and etc resources, programming help, articles, code snippet, video demonstrations and problems solving support. I would like to invite you as an author and a supporter.
Looking forward to hearing from you and hope you will join with us soon.

Thank you
RRaveen
Founder CodeGain.com

Anjana said...

Great!!!!

this article helped me alot...

this is so clear and EASY to use and i just finished my job with creating barcodes in few min where as it took me 2 days to google for right article.

Anonymous said...

Very nice article!
Could you inform us of the version of itextsharp you are using?