This project has moved. For the latest updates, please go here.

Extra Paragraphs in Table Cells

Sep 15, 2011 at 8:54 PM
Edited Sep 15, 2011 at 9:58 PM


I'm trying to resolve an issue where the number of paragraphs being added to a table cell appears to be twice as many as specified in the HTML. I'm looking through the code but I'm having trouble figuring out the cause of this.

Any chance I could get a little help? Even if you address this in your code, I'd very much appreciate if you could show the change made.



Looking further at the code, this snippet at the bottom of ProcessTableColumn() may be somewhat of interest.

if (en.IsSelfClosedTag) // Force a call to ProcessClosingTableColumn
    // we create a new currentParagraph to add new runs inside the TableCell
    cell.Append(currentParagraph = new Paragraph());

It looks like you add the current paragraph to the current cell so that paragraph processing can continue as normal. But what happens if paragraphs will be added to the cell? Does the code end the current (empty) paragraph and then start a new one? That's what appears to be happening but, in this case, extra empty paragraphs are inserted. Would love to get your insight on this.

May 28, 2012 at 10:06 AM

Sorry for the late response !

Yes you are right ; lots of empty paragraphs are generated during the process, in order to decrease the complexity of the code (regarding applying styles, paragraph properties, ...).
As a post-process operation, I clean all the empty paragraphs (private method RemoveEmptyParagraphs). In one of your previous discussions, you suggest me to clean paragraphs even inside the table.
 I will make some changes in this way.

Thanks for your feedback :)

Jul 27, 2012 at 8:03 AM

RemoveEmptyParagraphs does not remove paragraphs from cells because they are not added to the paragrphs list. Actual cleaning is done in ProcessClosingTableColumn(). But there are two bugs that prevents proper cleanup.

1. foreach on cell.Elements<Paragraph>() modifies the list so it silently exits the loop. You need to copy it to the array (or list) firstly.

Paragraph[] paraCheck = cell.Elements<Paragraph>().ToArray();

foreach(Paragraph p in paraCheck) {...}

2. cell.Append(new Paragraph(elements)); appends paragraph even if elements is empty and cell already has non-empty paragraphs. Needs additional check:

if(!cell.Elements<Paragraph>().Any() || elements.Count > 0) {...}

Jul 27, 2012 at 8:11 AM

Oh well... nice caught!

Strange my debugging test doesn't show me the silent crash... whathever I should have used a simple for loop to allow removing. I prefer using loop than having an overhead with an extra copy.

Thank you very much for your feedback. I'll made the changes to the source.