Thursday, July 3, 2008

HTML 2 PDF Using iTextSharp Library

Make reference to the following libraries
using iTextSharp.text;
using iTextSharp.text.html;
using iTextSharp.text.pdf;

Document doc = new Document(PageSize.A4, 80, 50, 30, 65);
try
{
PdfWriter.GetInstance(doc, new System.IO.FileStream("output.pdf", FileMode.Create));
doc.Open();
ArrayList ListaElemenata = new ArrayList();
StreamReader sr = new StreamReader(@quotC:\Document.html", Encoding.UTF8);
try
{
ListaElemenata = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, null);
}
catch{ }
for (int i = 0; i < ListaElemenata.Count; i++)
{
try
{
IElement ielement = (IElement)ListaElemenata[i];
ArrayList chunks = ielement.Chunks;
for (int j = 0; j < chunks.Count; j++)
{
Chunk chunk = (Chunk)chunks[j];
chunk.Font = FontFactory.GetFont(FontFactory.TIMES_ROMAN, "Cp1250", 12);
doc.Add(new Phrase(chunk));
}
}
catch{ }
}
doc.Close();
}
catch(Exception excep)
{
MessageBox.Show(excep.Message);
}



you can download itextsharp.dll from http://www.sf.net