Before we get into the nitty gritty of parsing the HTML so that we can create PDF code from it, it is important that we develop the concept of how text layout works in iTextSharp. So today we will cover those basics.
The first type of element we want to deal with when we parse our HTML into a PDF is the Paragraph element.
When we get to actually parsing our HTML to PDF code we will use the Paragraph object for all of our block elements. This allows us to add other Paragraphs and Chunks into it which we can format.
A Chunk is our second object that we will be using. The Chunk is the main object that will allow us to format the font. In fact, even if our block element specifies some sort of specific font, the font doesn’t actually get applied in the code until we add the text.
Typical code to place text into a PDF document would look something like this
p = new Paragraph(new Chunk("text that needs a font", FontFactory.GetFont("Arial", 10, Font.NORMAL, Color.BLACK))); p.Alignment = (Element.ALIGN_CENTER); ct.AddElement(p);
The only other two classes we need to discuss are the list classes. We use the List to create an item that will handle both the OL and UL tags. The ListItem class will handle the individual items within the list. The List constructor handles which of the two types of list we are dealing with by specifying true or false in the first parameter, numbered.
I have not yet added the ability to handle tables to my HTML parser mainly because I have not had the need. I think once I show you how to create tables and how to parse HTML you should be able to handle adding table parsing code yourself.
Other post in iTextSharp
- PDFs Using iTextSharp - June 17th, 2009
- iTextSharp – The easy way - June 24th, 2009
- iTextSharp – Adding Images - June 30th, 2009
- iTextSharp – HTML to PDF – Positioning Text - July 8th, 2009
- iTextSharp – HTML to PDF - Prerequisites - July 14th, 2009
- iTextSharp – PDF to HTML – Cleaning HTML - July 20th, 2009
- iText IN ACTION – Creating and Manipulating PDF - July 24th, 2009
- iTextSharp – HTML to PDF – Parsing HTML - July 28th, 2009
- iTextSharp – HTML to PDF – Writing the PDF - August 4th, 2009
- iTextSharp – HTML to PDF – Finishing Up - August 12th, 2009
- iTextSharp Tables - September 17th, 2009