Thursday, March 28, 2013

Generating a .PDF file in ASP.NET

Download the iTextSharp library and add a reference of itextsharp.dll to your project, you can find it inside the zip folder.

After adding the reference add the below references on the web page.

using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Text;

Now you can use below method to generate and save a .pdf file.

private void GeneratePDF()
string pdfPath = "~/PDF/File.pdf";
Session["pdfpath"] = pdfPath;
StringBuilder sb = new StringBuilder();
sb.Append("Name : Your Name" + Environment.NewLine);
sb.Append("Address : Your Address" + Environment.NewLine);
sb.Append("Institute : Your School" + Environment.NewLine);
sb.Append("Image :" + Environment.NewLine);
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(Server.MapPath("~/image.JPG"));
Document doc = new Document();
PdfWriter.GetInstance(doc, new FileStream(Server.MapPath(pdfPath), FileMode.Create));
doc.Add(new Paragraph(sb.ToString()));
catch (Exception ex)
throw ex;

No comments:
Write comments
Recommended Posts × +