iTextSharp – HTML to PDF – Finishing Up
In the last post I mentioned there were a few topics we need to close up today. The two topics we’ve left undone are popping the attribute information off the stack when we hit a closing element and dealing with the paragraph gap that normally appears between paragraph elements.
The first thing you’ll want to do when you hit a closing element is to retrieve its name again. Just like we did at the beginning element. Once you have that you can pop the attribute information off the stack(s).
You’ll also want to undo any indentation that you applied during the opening element.
To handle the paragraph break, I defined a _crlfAtEnd attribute in my resource file. If it was defined as true, I added an extra line feed at the end to account for the gap.
1: isBlock = Resources.html2pdf
2: .ResourceManager
3: .GetString(tagName + "_isBlock");
4: if (isBlock != null &&
5: isBlock.ToLower() == "true")
6: {
7: isBlock = Resources.html2pdf
8: .ResourceManager
9: .GetString(tagName + "_crlfAtEnd");
10: if (isBlock != null &&
11: isBlock.ToLower() == "true")
12: {
13: et = stack.Peek();
14: Font f = getCurrentFont();
15: if (et is Phrase)
16: {
17: ((Phrase)(et)).Add(
18: new Chunk("\n\r", f));
19: stack.Pop();
20: }
21: }
22: p = new Paragraph();
23: ((Paragraph)p).Add("");
24: ((Paragraph)p).SetLeading(m_leading, 1);
25: list.Add(p);
26: stack.Push(p);
27: }
One problem I’ve had with this in the past is that this cr/lf gets added at the end even if the block is the last block. I really need to find some way to detect that this is the last place this occurs either nested or in the outermost block. But I’ll leave that enhancement for you.
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
Related Post
-
LeeZilla
-
LeeZilla
-
Ankit


