2

Can anyone tell me how to convert a PDF document from an HTML code using C#? I'm using itextsharp 5.0.2 dll. I have come across this forum. I didnt find any solution so far. And some of them mentioned iTextSharp.HtmlParser but i cannot find such kind of class in my version of dll. In which version should i need to use to achieve my task.

I have tried with this code...i dont know what wrong did i do? Im not getting the PDF file.

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestResult.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);

StringBuilder htmlText =
            new StringBuilder("<table style='color:red;' border='1'>");
htmlText.Append("<tr><th>Karunagara Pandi</th><tr><td> Software Engineer</td></tr></table>");

StringReader stringReader = new StringReader(htmlText.ToString());
Document doc = new Document(PageSize.A4);
List<iTextSharp.text.IElement> elements = 
        iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(stringReader, null);
doc.Open();
foreach (object item in elements)
{
    doc.Add((IElement)item);
}

// Response Output
PdfWriter.GetInstance(doc, Response.OutputStream);
doc.Open();

//doc.Close();

Response.Write("PDF is created");

This code also taken from this site only....I need to your proper solution. I'll also try to get.....

Thanks.

1 Answer 1

2

You have two doc.Open statements without an intervening doc.Close(). The second one will effectively overwrite the contents you've just added.

doc.Open();  // ONE
foreach (object item in elements)
{
    doc.Add((IElement)item);
}

// Response Output
PdfWriter.GetInstance(doc, Response.OutputStream);
doc.Open();  // TWO
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.