Search code examples
.netitexthtml-to-pdf

ITextSharp HTML to PDF?


I'd like to know if ITextSharp has the capability of converting HTML to PDF. Everything I will convert will just be plain text but unfortunately there is very little to no documentation on ITextSharp so I can't determine if that will be a viable solution for me.

If it can't do it, can someone point me to some good, free .net libraries that can take a simple plain text HTML document and convert it to a pdf?

tia.


Solution

  • after doing some digging I found a good way to accomplish what I need with ITextSharp.

    Here is some sample code if it will help anyone else in the future:

    protected void Page_Load(object sender, EventArgs e)
    {
        Document document = new Document();
        try
        {
            PdfWriter.GetInstance(document, new FileStream("c:\\my.pdf", FileMode.Create));
            document.Open();
            WebClient wc = new WebClient();
            string htmlText = wc.DownloadString("http://localhost:59500/my.html");
            Response.Write(htmlText);
            List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(htmlText), null);
            for (int k = 0; k < htmlarraylist.Count; k++)
            {
                document.Add((IElement)htmlarraylist[k]);
            }
    
            document.Close();
        }
        catch
        {
        }
    }