This project has moved and is read-only. For the latest updates, please go here.

Remove paragraphs in table cells

Jan 11, 2010 at 3:02 PM

Hi all,

First, thanks for this wonderful peace of software!

I had a problem removing paragraphs in table cells. It never removed the paragraph, but only set the value to empty. When I tried to remove the paragraph, Word rejected the document. The problem is, that there must be at least one paragraph in a cell. Here is the changed Remove method in paragraph.cs that takes care of this situation.

public void Remove(bool trackChanges)
{
    if (trackChanges)
    {
        DateTime now = DateTime.Now.ToUniversalTime();

        List<XElement> elements = Xml.Elements().ToList();
        List<XElement> temp = new List<XElement>();
        for (int i = 0; i < elements.Count(); i++ )
        {
            XElement e = elements[i];

            if (e.Name.LocalName != "del")
            {
                temp.Add(e);
                e.Remove();
            }

            else
            {
                if (temp.Count() > 0)
                {
                    e.AddBeforeSelf(CreateEdit(EditType.del, now, temp.Elements()));
                    temp.Clear();
                }
            }
        }

        if (temp.Count() > 0)
            Xml.Add(CreateEdit(EditType.del, now, temp));                   
    }

    else
    {
        runLookup.Clear();

        IEnumerable<XElement> paragraphs = Xml.Parent.Descendants(XName.Get("p", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"));

        //At least one paragraph has to be in a table cell.
        //only remove the paragraph if there are others
        //otherwise empty the paragraph
        if (Xml.Parent.Name.LocalName == "tc" && paragraphs.Count() == 1)
        {
            Xml.Value = string.Empty;
        }
        else
        {
            // Remove this paragraph from the document
            Xml.Remove();
            Xml = null;

            runLookup = null;
        }
    }

    DocX.RebuildParagraphs(Document);
}


Cheers
Florian

Jan 11, 2010 at 3:30 PM
Florian,

thank you for contributing your bug fix to the source. I will add this update to the next release of DocX and credit you for it.

Thanks again,
Cathal