Using iText to generate PDF from Html using HtmlWorker

Someone asked this on Google+ and I had a program that did it so I decided to turn it into a quick blog post.  I stripped away the application specific logic, but this is the gist of the iText code. (Technically this is iTextSharp, but I’m guessing it will work in iText as well.)  The trick is to use the HtmlWorker. (If I remember right, there’s a newer XMLWorkerHelper object that’s newer/better.)

This returns the PDF in a memory stream (useful for sending to the Response object to send to a browser):

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;

public static Stream RenderPDF()


    Document document = new Document(PageSize.LETTER, 36F, 36F, 36F, 36F);
    MemoryStream memoryStream = new MemoryStream();
    PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);

    HTMLWorker htmlWorker = new HTMLWorker(document);
    var sectionTitleFont = new Font(Font.FontFamily.TIMES_ROMAN, 24, Font.BOLD, BaseColor.BLACK);
    var sectionTitleParagraph = new Paragraph(“Title text”, sectionTitleFont);



    StringBuilder sb = new StringBuilder();
    string strBody = ”

This is the paragraph body


    htmlWorker.Parse(new StringReader(sb.ToString()));

    writer.CloseStream = false;
    memoryStream.Position = 0;
    return memoryStream;


Nashville Sounds / Herschel Greer Stadium

I went to Nashville this weekend to see the Sacramento River Cats play the Sounds. I always like getting to see new stadiums.  I think this may have been the first Triple-A game I’ve been to and was a little underwhelmed at the stadium.

Having been to many Birmingham Barons (Double-A) games at the Hoover Met (and now the new Regions Field), I was expecting a step up from that, but it was really a step down. I know the Barons were at the Met for 25 years so it’s not exactly new, but it’s a much nicer stadium than Nashville’s.

Nashville Sounds Greer Stadium

Nashville Sounds Greer Stadium

These pictures were taken from Section J, right behind the first base dugout, which is where the visitors sit.

Nashville Sounds Greer Stadium

There are a few seats and a picnic area behind right field, but you can’t go behind center and left field. (The tan metal building apparently houses the locker rooms. After the games, I saw players from both teams walking that way dragging equipment bags.)

The whole thing reminded me of the stadium in Bull Durham.

Nashville Sounds Greer Stadium

Having said all of that, there was nothing wrong with the stadium. Friendly, helpful staff and we had a great time – but it really made me appreciate the facilities we’ve got in Birmingham.

My first trip to the Barons’ new stadium – Regions Field

I made my first visit to the Birmingham Barons‘ new stadium (Regions Field) today.  I had heard and read a lot of  good things and am happy to say that I was not disappointed.

There’s a pretty good review here so I won’t repeat all of the details, but will just list some thoughts I had.

  • I really like the fact that you can walk all the way around the field. (In the old stadium in Hoover, you couldn’t get behind the outfield.) We sat in a grassy area in center field for several innings and I really enjoyed that vantage point.
  • There are a lot of picnic tables.
  • I wasn’t sure what to expect with regard to parking, but it was no problem. We paid $5 to park in a lot at 14th Street and 1st Avenue North (just north of the ballpark after going through the tunnel under the train tracks). There were many shuttles (golf carts with 3 rows of seats like you see in airports) to take us from there to the stadium entrance.  It would have been an easy walk, but one member of our group doesn’t get around very well, so this was very nice. These shuttles run continuously during the game. The shuttles are free, but there is a sign that says “we work for tips”.
  • It’s cool that you can look down into the bullpens from the outfield and watch the pitchers warming up.
  • There was a good selection of food. In addition to the expected hot dogs, french fries, popcorn, etc., I saw signs for burritos, tacos, and Philly cheesesteaks.  I went with the Dreamland BBQ nachos (which were great, if you’re into that sort of thing like I am).

A couple of weeks ago, I ate at the new Todd English P.U.B. in the entertainment district before seeing Jerry Seinfeld at BJCC and it was also a very good experience.  Between that and the new ballpark – I’m cautiously optimistic about downtown Birmingham.