Export Html to PDF using ITextsharp

Qaisar Shabbir Awan picture Qaisar Shabbir Awan · Aug 22, 2013 · Viewed 50.2k times · Source

I have tried the code below, I am also facing an error. I am using latest DLL.

String strSelectUserListBuilder = @"<html><body>
                                <h1>My First Heading</h1>
                                <p>My first paragraph.</p>
                            </body>
                        </html>";

String htmlText = strSelectUserListBuilder.ToString();

List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(htmlText), null);

I got this error:

The given key was not present in the dictionary.

Answer

Kapil Khandelwal picture Kapil Khandelwal · Aug 22, 2013

Try this:

Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\MySamplePDF.pdf", FileMode.Create));
document.Open();
iTextSharp.text.html.simpleparser.HTMLWorker hw = 
             new iTextSharp.text.html.simpleparser.HTMLWorker(document);
hw.Parse(new StringReader(htmlText));
document.Close();