DLL iTextsharp- Used to Create PDF in ASP.NET

Lakhangarg
Posted by Lakhangarg under C# category on | Views : 6920
itextsharp.dll used to create pdf document in Dot Net.


Sample Code:

private void CreateMyPDF()
{
Document doc = new Document(PageSize.A4);

PdfWriter writer = PdfWriter.GetInstance(doc, new
FileStream("C:\\lakhan.pdf", FileMode.Create));

doc.Open();

string strURL = "http://wiziq.com/index.aspx";
Uri uri = new Uri(strURL);

//Create the request object

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
req.UserAgent = "Get Content";
WebResponse resp = req.GetResponse();
Stream stream = resp.GetResponseStream();
StreamReader sr = new StreamReader(stream);
string html = sr.ReadToEnd();

ArrayList lt = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(html), null);

ColumnText ct = new ColumnText(writer.DirectContent);

ct.SetSimpleColumn(50, 50, PageSize.A4.Width - 50, PageSize.A4.Height - 50);

for (int k = 0; k < lt.Count; ++k) {

ct.AddElement((IElement)lt[k]);
}
ct.Go();
doc.Close();
}

private void CreateNewPDF()
{
string strURL="http://wiziq.com/index.aspx";
Uri uri = new Uri(strURL);

//Create the request object
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
req.UserAgent = "Get Content";
WebResponse resp = req.GetResponse();
Stream stream = resp.GetResponseStream();
StreamReader sr = new StreamReader(stream);
string html = sr.ReadToEnd();
Document Doc = new Document();

PdfWriter.GetInstance(Doc, new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+ "\\Lakhan.pdf", FileMode.Create));
Doc.Open();

Chunk c = new Chunk("Export HTML to PDF\n",FontFactory.GetFont("Verdana", 15));
Paragraph p = new Paragraph();
p.Alignment = Element.ALIGN_CENTER;
p.Add(c);
Chunk chunk1 = new Chunk("By Lakhan Pal Garg, \n",FontFactory.GetFont("Verdana", 8));
Paragraph p1 = new Paragraph();
p1.Alignment = Element.ALIGN_RIGHT;
p1.Add(chunk1);

Doc.Add(p);
Doc.Add(p1);
HtmlParser.Parse(Doc, "E:\\Lakhan\\Projects\\Testweb\\1.html");

Doc.Close();
string Path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+ "\\Lakhan.pdf";

ShowPdf(Path);
}


Please get the itextsharp.dll from internet to run the above code.

Comments or Responses

Login to post response