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.
- using sharp=iTextSharp.text;
- using iTextSharp.text.pdf;
- using iTextSharp.text.xml;
Creating Sample PDF
- string path = Server.MapPath(".") + "\\" + "Sample.pdf";
- document.Open();
- //Here I am adding a Paragraph to the document
- document.Add(new sharp.Paragraph("Its a Sample Example for Creating PDF File Using iTextSharp. Its For Free of Cost."));
- document.Close();
PDF with Different Fonts
- string path = Server.MapPath(".") + "\\" + "Fonts.pdf";
- document.Open();
- document.Add(myPhrase);
- myPhrase = new sharp.Phrase("Hello 1bis! ", sharp.FontFactory.GetFont(sharp.FontFactory.TIMES_ROMAN, 8, sharp.Font.BOLD));
- myPhrase.Add(new sharp.Phrase("some other font ", sharp.FontFactory.GetFont(sharp.FontFactory.HELVETICA, 8)));
- myPhrase.Add(new sharp.Phrase("This is the end of the sentence.\n", sharp.FontFactory.GetFont(sharp.FontFactory.TIMES_ROMAN, 8, sharp.Font.ITALIC)));
- document.Add(myPhrase);
- document.Add(myParagraph);
- document.Add(myParagraph);
- document.Add(myPhrase);
- document.Add(myPhrase);
- document.Close();
Creating Links, Lists, Annotations
- // Here I am Creating Multiple Pages.
- string path = Server.MapPath(".") + "\\" + "Links.pdf";
- document.Open();
- //Page 1 Links
- anchor.Reference = "http://ramcrishna.blogspot.com";
- anchor.Name = "top";
- para.Add(anchor);
- document.Add(para);
- //Page 2 Lists
- document.NewPage();
- document.Add(lst);
- lst.Add("ASP.Net");
- lst.Add("VB.Net");
- lst.Add("C#.Net");
- para1.Add(lst);
- document.Add(para1);
- //Page 3 Annotations
- document.NewPage();
- PdfContentByte cb = writer.DirectContent;
- cb.SetRGBColorStroke(0x00, 0x00, 0xFF);
- cb.Rectangle(100, 700, 100, 100);
- cb.Stroke();
- sharp.Annotation annot = new sharp.Annotation(100f, 700f, 200f, 800f, "http://ramcrishna.blogspot.com");
- document.Add(annot);
- cb.SetRGBColorStroke(0xFF, 0x00, 0x00);
- cb.Rectangle(200, 700, 100, 100);
- cb.Stroke();
- try
- {
- }
- catch
- {
- }
- cb.SetRGBColorStroke(0x00, 0xFF, 0x00);
- cb.Rectangle(300, 700, 100, 100);
- cb.Stroke();
- cb.SetRGBColorStroke(0x00, 0x00, 0xFF);
- cb.Rectangle(100, 500, 100, 100);
- cb.Stroke();
- cb.SetRGBColorStroke(0xFF, 0x00, 0x00);
- cb.Rectangle(200, 500, 100, 100);
- cb.Stroke();
- cb.SetRGBColorStroke(0x00, 0xFF, 0x00);
- cb.Rectangle(300, 500, 100, 100);
- cb.Stroke();
- document.Close();
Creating Headers & Footers
- // Creating Headers & Footer
- string path = Server.MapPath(".") + "\\" + "Header.pdf";
- document.Open();
- document.Header = hf;
- document.Footer = foot;
- document.Open();
- document.NewPage();
- document.Close();
Creating Tables
- // Creating tables
- string path = Server.MapPath(".") + "\\" + "Tables.pdf";
- document.Open();
- // Static Table with 2 rows and 2 columns
- tbl.Cellpadding = 3;
- tbl.Cellspacing = 1;
- tbl.AddCell("Col1");
- tbl.AddCell("Col2");
- tbl.AddCell("Col3");
- tbl.AddCell("Col4");
- document.Add(tbl);
- //Dynamic table from Dataset
- document.NewPage();
- DataColumn dtcol;
- DataRow dr;
- dt.Columns.Add(dtcol);
- dt.Columns.Add(dtcol);
- dt.Columns.Add(dtcol);
- dt.Columns.Add(dtcol);
- dt.Columns.Add(dtcol);
- dt.Columns.Add(dtcol);
- dr = dt.NewRow();
- dr["UserName"] = "Ram";
- dr["FName"] = "Ram";
- dr["LName"] = "Krishna ";
- dr["City"] = "Hyderabad";
- dr["Email"] = "n.ramakrishna27@gmail.com";
- dr["Mobile"] = "123456";
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["UserName"] = "Naveen";
- dr["FName"] = "Naveen";
- dr["LName"] = "Reddy";
- dr["City"] = "Sec-Bad";
- dr["Email"] = "naveen@yahoo.co.in";
- dr["Mobile"] = "4210";
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["UserName"] = "Raj";
- dr["FName"] = "Raja";
- dr["LName"] = "Shekar";
- dr["City"] = "Banglore";
- dr["Email"] = "raj4u@yahoo.com";
- dr["Mobile"] = "01200";
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["UserName"] = "Suresh";
- dr["FName"] = "Suresh";
- dr["LName"] = "Kumar";
- dr["City"] = "Chennai";
- dr["Email"] = "suri@hotmail.com";
- dr["Mobile"] = "10120";
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["UserName"] = "Ravi";
- dr["FName"] = "Ravi";
- dr["LName"] = "Kumar";
- dr["City"] = "Vizag";
- dr["Email"] = "ravi@gmail.com";
- dr["Mobile"] = "1234";
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["UserName"] = "Shaik";
- dr["FName"] = "Shaik";
- dr["LName"] = "Khan";
- dr["City"] = "Vijayawada";
- dr["Email"] = "shaik@rediff.com";
- dr["Mobile"] = "1234";
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["UserName"] = "Kiran";
- dr["FName"] = "Kiran";
- dr["LName"] = "Kumar";
- dr["City"] = "Hyderabad";
- dr["Email"] = "kiran@yahoo.com";
- dr["Mobile"] = "12453";
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["UserName"] = "Santu";
- dr["FName"] = "Santhosh";
- dr["LName"] = "Reddy";
- dr["City"] = "Hyderabad";
- dr["Email"] = "santu@yahoo.com";
- dr["Mobile"] = "123";
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["UserName"] = "Ashfak";
- dr["FName"] = "Ashfak";
- dr["LName"] = "Khan";
- dr["City"] = "Hyderabad";
- dr["Email"] = "ash@yahoo.com";
- dr["Mobile"] = "123";
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["UserName"] = "Sajju";
- dr["FName"] = "Sajju";
- dr["LName"] = "MD";
- dr["City"] = "Hyderabad";
- dr["Email"] = "sajju@gmail.com";
- dr["Mobile"] = "123";
- dt.Rows.Add(dr);
- int rowcount, coloumscount;
- rowcount = dt.Rows.Count;
- coloumscount = dt.Columns.Count;
- pdftbl.DefaultCell.Padding = 3;
- float[] headerwidths = { 9, 9, 8, 10, 12, 8 };
- pdftbl.SetWidths(headerwidths);
- pdftbl.WidthPercentage = 100;
- pdftbl.DefaultCell.BorderWidth = 2;
- pdftbl.DefaultCell.HorizontalAlignment = sharp.Element.ALIGN_CENTER;
- pdftbl.AddCell("UserName");
- pdftbl.AddCell("FName");
- pdftbl.AddCell("LName");
- pdftbl.AddCell("City");
- pdftbl.AddCell("Email");
- pdftbl.AddCell("Mobile");
- pdftbl.HeaderRows = 1;
- pdftbl.DefaultCell.BorderWidth = 1;
- int max = dt.Rows.Count;
- for (int row = 1,j=1 ; row < rowcount j<max; row++,j++)
- {
- if (j % 2 == 1)
- {
- pdftbl.DefaultCell.GrayFill = 0.9f;
- }
- for (int colm = 0; colm < coloumscount; colm++)
- {
- pdftbl.AddCell(dt.Rows[row - 1][colm].ToString());
- }
- if (j % 2 == 1)
- {
- pdftbl.DefaultCell.GrayFill = 0.4f;
- }
- }
- document.Add(pdftbl);
- document.Close();
PDF with Image
- // Creating Images And water Marks
- string path = Server.MapPath(".") + "\\" + "Images.pdf";
- document.Open();
- //Adding Images
- string imgpath = Server.MapPath("images") + "\\" + "img1.jpg";
- sharp.Image img = sharp.Image.GetInstance(imgpath);
- img.ScalePercent(50);
- document.Add(img);
- document.Close();
Creating Password to PDF
- string path = Server.MapPath(".") + "\\" + "Password.pdf";
- pd.SetEncryption(PdfWriter.STRENGTH128BITS, "userpass", "password", PdfWriter.AllowPrinting PdfWriter.AllowCopy);
- document.Open();
- document.Close();
Creating BarCodes in PDF
- string path = Server.MapPath(".") + "\\" + "d.pdf";
- document.Open();
- PdfContentByte cb = pd.DirectContent;
- code39.Code = "CODE39-1234567890";
- code39.StartStopText = false;
- sharp.Image image39 = code39.CreateImageWithBarcode(cb, null, null);
- code39ext.Code = "The willows.";
- code39ext.StartStopText = false;
- code39ext.Extended = true;
- sharp.Image image39ext = code39ext.CreateImageWithBarcode(cb, null, null);
- code128.Code = "1Z234786 hello";
- sharp.Image image128 = code128.CreateImageWithBarcode(cb, null, null);
- codeEAN.CodeType = BarcodeEAN.EAN13;
- codeEAN.Code = "9780201615883";
- sharp.Image imageEAN = codeEAN.CreateImageWithBarcode(cb, null, null);
- table.WidthPercentage = 100;
- table.DefaultCell.Border = sharp.Rectangle.NO_BORDER;
- table.DefaultCell.HorizontalAlignment = sharp.Element.ALIGN_CENTER;
- table.DefaultCell.VerticalAlignment = sharp.Element.ALIGN_MIDDLE;
- table.DefaultCell.FixedHeight = 70;
- table.AddCell("CODE 39");
- table.AddCell("CODE 39 EXTENDED");
- table.AddCell("CODE 128");
- table.AddCell("CODE EAN");
- document.Add(table);
- 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:
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...
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
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.
Very nice article!
Could you inform us of the version of itextsharp you are using?
Post a Comment