Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open XML Paragraph Spacing

Tags:

c#

openxml

I have a strange problem generating a .docx with Open XML. I have a TableCell that has a Paragraph then Run then Text generated like this:

tblRow.Append(new TableCell(new Paragraph(GetPropertiesForStyle("TableDescription"), new Run(new Text(colName)))));

The GetPropertiesForStyle method returns the ParagraphProperties for the specific paragraph and sets the style. This all works fine, however within the table row the first cell seems to have double spacing before and the last cell has double spacing after - all the cells in the middle look perfectly fine.

enter image description here

My style configuration looks like this:

new Style(
    new StyleName() { Val = "TableDescription" },
    new StyleParagraphProperties(
        new ParagraphBorders(
            new BottomBorder() { Val = BorderValues.None }
        ),
        new SpacingBetweenLines() { Before = "100", After = "100", Line = "200", LineRule = LineSpacingRuleValues.Exact },
        new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center }
    )
),

I've checked the produced XML using the productivity tool and it's applying the properties perfectly fine. Each TableCell has the following configuration (there are some extra margin properties on there and a shader fill which I've omitted for brevity).

<w:tc>
    <w:p>
        <w:pPr>
            <w:pStyle w:val="TableDescription" />
        </w:pPr>
        <w:r>
            <w:t>Reporting Period</w:t>
        </w:r>
    </w:p>
</w:tc>

Any ideas what's going on?

like image 705
Paul Aldred-Bann Avatar asked Sep 19 '12 13:09

Paul Aldred-Bann


1 Answers

Managed to fix this by adding the following to my style:

new ContextualSpacing() { Val = false }

Which tells word to uncheck the Don't add space between paragraphs of the same style in paragraph options.

like image 154
Paul Aldred-Bann Avatar answered Oct 12 '22 09:10

Paul Aldred-Bann