Dave's Notebook

iTextSharp – HTML to PDF – Finishing Up

tiger 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
isBlock = Resources.html2pdf
.ResourceManager
.GetString(tagName + "_isBlock");
if (isBlock != null &&
isBlock.ToLower() == "true")
{
isBlock = Resources.html2pdf
.ResourceManager
.GetString(tagName + "_crlfAtEnd");
if (isBlock != null &&
isBlock.ToLower() == "true")
{
et = stack.Peek();
Font f = getCurrentFont();
if (et is Phrase)
{
((Phrase)(et)).Add(
new Chunk("\n", f));
stack.Pop();
}
}
p = new Paragraph();
((Paragraph)p).Add("");
((Paragraph)p).SetLeading(m_leading, 1);
list.Add(p);
stack.Push(p);
}

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.