Search code examples
c#openxmlwordprocessingml

Unreadable Word document after removing cell borders (OpenXml.Wordprocessing)


After creating a TableCellProperties and removing the TableCellBorders, the word document becomes unreadable and I get:

Word found unreadable content in test.docx. Do you want to recover the contents of this document? If you trust the source of this document, click Yes.

The code that I use:

TableCellProperties cellProp = new TableCellProperties(
    new TableCellBorders(
        new TopBorder()
        {
            Val =
                new EnumValue<BorderValues>(BorderValues.Nil),
        },
        new BottomBorder()
        {
            Val =
                new EnumValue<BorderValues>(BorderValues.Nil),
        },
        new LeftBorder()
        {
            Val =
                new EnumValue<BorderValues>(BorderValues.Nil),

        },
        new RightBorder()
        {
            Val = new EnumValue<BorderValues>(BorderValues.Nil),
        }
    )
);
TableCell tc = new TableCell();


tc.Append(cellProp);

TableRow trTest = new TableRow();
trTest.Append(new TableCell(tc.OuterXml));
trTest.Append(new TableCell(new Paragraph(new Run(new Text("B")))));
trTest.Append(new TableCell(new Paragraph(new Run(new Text("C")))));
trTest.Append(new TableCell(new Paragraph(new Run(new Text("D")))));
trTest.Append(new TableCell(new Paragraph(new Run(new Text("E")))));
trTest.Append(new TableCell(new Paragraph(new Run(new Text("F")))));
t.Append(trTest);

The BorderValue is set to Nil, since None does not seem to remove the borders. After the MS Word auto recover procedure, the file is fine. Whan can cause such an issue?


Solution

  • Problem solved!

    Each table cell should contain/end with Paragraph object, so the solution is:

    tc.Append(cellProp);
    tc.Append(new Paragraph());
    

    Looks like I have the same problem like in this question, but without the error.