Triazic
Triazic

Reputation: 53

OpenXML - left table border doesn't work?

Here is my border formatting for creating a test table in a docx using OpenXML (C#):

    //Create a TableProperties object and specify its border information.
    var borderType = BorderValues.Single;
    DocumentFormat.OpenXml.UInt32Value size = 5;
    TableProperties tblProp = new TableProperties(
        new TableJustification() { Val = TableRowAlignmentValues.Center },
        new TableBorders(
            new TopBorder(){ Val = new EnumValue<BorderValues>(borderType), Size = size },
            new BottomBorder(){ Val = new EnumValue<BorderValues>(borderType), Size = size },
            new LeftBorder(){ Val = new EnumValue<BorderValues>(borderType), Size = size },
            new RightBorder(){ Val = new EnumValue<BorderValues>(borderType), Size = size },
            new InsideHorizontalBorder(){ Val = new EnumValue<BorderValues>(borderType), Size = size },
            new InsideVerticalBorder(){ Val = new EnumValue<BorderValues>(borderType), Size = size }
        )
    );

    // Append the TableProperties object to the empty table.
    table.AppendChild<TableProperties>(tblProp);

The above formatting produces this (when viewed in 365 word viewer):
enter image description here
The question is, why?

Here is the generated xml (extracted from zip): enter image description here

EDIT: it renders correctly in the word desktop app.. so this seems to be a #Microsoft bug.

Upvotes: 0

Views: 702

Answers (1)

Stewart Ritchie
Stewart Ritchie

Reputation: 926

Different clients often support the specification to different degrees. I remember fighting this when I was trying to produce content for Google docs.

There are some things you can try.

Firstly, instead of using LeftBorder and RightBorder, try StartBorder and EndBorder. These are important to support semantic considerations in RTL cultures vs. LTR cultures, and may consequently be better supported.

Secondly, reverse engineering nearly always worked for me. Start by styling a document how you want it to look in Word or other tools, then extract and examine the XML.

I think you'll find that Word defines a style that it applies to the table instead of trying to do it all inline - copying that approach is more complex, but often does the trick.

Upvotes: 1

Related Questions