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

Resolution to rowspan issue

Aug 7, 2012 at 1:34 AM

Earlier, I posted about rowspan attributes resulting in a malformed Word document. This error does not occur in your version of the code. Instead, it was introduced by changes I had made to the code. But perhaps you are interested in the issue I was addressing when I made that change.

I had replaced RemoveEmptyParagraphs() with the following methods. The reason for this is because I was getting a bunch of extra spacing at the bottom of each table cell. In some cases, I thought I had an extra, empty paragraph at the end.

The code below cleaned that up nicely. Unfortunately, this creates an error when a VerticalMerge is used because an empty paragraph is required in that case. So I need to find a fix.

But if your code were fixed so it did not display all those extra spaces, I could simply use your code.

 

/// <summary>
/// Remove empty paragraph unless 2 tables are side by side.
/// These paragraph could be empty due to misformed html or spaces in the html source.
/// </summary>
private void RemoveEmptyParagraphs()
{
    for (int i = 0; i < paragraphs.Count; i++)
    {
        if (paragraphs[i] is Paragraph)
        {

            // If the paragraph is between 2 tables, we don't remove it (it provides some
            // separation or Word will merge the two tables)
            if (i > 0 && i + 1 < paragraphs.Count - 1
                && paragraphs[i - 1].LocalName == "tbl"
                && paragraphs[i + 1].LocalName == "tbl") continue;

            OpenXmlCompositeElement p = paragraphs[i];

            // Keep paragraph if it contains any elements other than ParagraphProperties
            // Empty paragraphs that contain only default style or attributes are removed
            if (p.ChildElements.Any(e => !(e is ParagraphProperties)))
                continue;

            paragraphs.RemoveAt(i);
            i--;
        }
        else if (paragraphs[i] is Table)
        {
            Table tbl = paragraphs[i] as Table;
            foreach (var row in tbl.Elements<TableRow>())
                foreach (var cell in row.Elements<TableCell>())
                    RemoveEmptyParagraphs(cell.ChildElements);
        }
    }
}

/// <summary>
/// This version of RemoveEmptyParagraphs() is used to recursively handle paragraphs
/// within tables. Unfortunately, a separate version is needed because the paragraphs
/// collection is of type List&lt;OpenXmlCompositeElement&gt; while child-element collections
/// are of type OpenXmlElementList.
/// </summary>
/// <param name="elements"></param>
private void RemoveEmptyParagraphs(OpenXmlElementList elements)
{
    for (int i = 0; i < elements.Count; i++)
    {
        if (elements[i] is Paragraph)
        {
            // If the paragraph is between 2 tables, we don't remove it (it provides some
            // separation or Word will merge the two tables)
            if (i > 0 && i + 1 < elements.Count
                && elements[i - 1].LocalName == "tbl"
                && elements[i + 1].LocalName == "tbl") continue;

            Paragraph p = elements[i] as Paragraph;

            // Keep paragraph if it contains any elements other than ParagraphProperties
            // Empty paragraphs that contain only default style or attributes are removed
            if (p.ChildElements.Any(e => !(e is ParagraphProperties)))
                continue;

            // Remove this element
            p.Remove();
            i--;
        }
        else if (elements[i] is Table)
        {
            Table tbl = elements[i] as Table;
            foreach (var row in tbl.Elements<TableRow>())
                foreach (var cell in row.Elements<TableCell>())
                    RemoveEmptyParagraphs(cell.ChildElements);
        }
    }
}

 

 

Coordinator
Aug 9, 2012 at 9:34 AM
Edited Aug 9, 2012 at 9:40 AM

Thank your but I'm already aware of that bug ; it has been fixed in changeset #46273. I clean the paragraph in ProcessClosingTableColumn method.

I really recommend you to use the latest version because many many bugs has been fixed. But I do appreciate your feedback.