views:

706

answers:

3

I posted a question here a few weeks ago asking about an alternative to creating .fdf files to fill in pdf documents and someone here pointed me to ITextSharp.

It's working like a champ so thanks for that.

I'd now 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.

A: 

You can use HTMLDOC -> http://www.htmldoc.org/

Glennular
A: 

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
            {
            }
        }
Kyle
A: 

I came across the same question a few weeks ago and this is the result from what I found. This method does a quick dump of HTML to a PDF. The document will most likely need some format tweaking.

    private MemoryStream createPDF(string html)
    {
        MemoryStream msOutput = new MemoryStream();
        TextReader reader = new StringReader(html);

        // step 1: creation of a document-object
        Document document = new Document(PageSize.A4, 30, 30, 30, 30);            

        // step 2:
        // we create a writer that listens to the document
        // and directs a XML-stream to a file
        PdfWriter writer = PdfWriter.GetInstance(document, msOutput);

        // step 3: we create a worker parse the document
        HTMLWorker worker = new HTMLWorker(document);

        // step 4: we open document and start the worker on the document
        document.Open();
        worker.StartDocument();

        // step 5: parse the html into the document
        worker.Parse(reader);

        // step 6: close the document and the worker
        worker.EndDocument();
        worker.Close();
        document.Close();

        return msOutput;
    }
Jonathan