Center text in Table Cell - OpenXML SDK

Viewed 531

I created a Table and in the last row I added Borders.

TableCellProperties tcp = new TableCellProperties();

TableCellBorders tcb = new TableCellBorders(
        new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.BasicThinLines) },
        new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.Double) });

tcp.Append(tcb);
gCell.Append(tcp);

enter image description here

My Problem is that the text '28.329,36 €' is too close to the top border. I would like to put the text a bit lower so the text has the same distance to the top and bottom border.

How could I achieve this?

1 Answers

You can control the vertical alignment using the TableCellVerticalAlignment class:

TableCellProperties tcp = new TableCellProperties();

TableCellBorders tcb = new TableCellBorders(
        new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.BasicThinLines) },
        new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.Double) });

//set the alignment to "Center"
TableCellVerticalAlignment tcVA = new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center };

tcp.Append(tcb);
//append the TableCellVerticalAlignment instance
tcp.Append(tcVA);
gCell.Append(tcp);

One thing to note is that the "After" line spacing will be set to 8pt so the above will actually look very similar to what you have with the default height:

"After" line spacing still causes issues

But if you were to expand the cell you will see it's centred (almost, the spacing is still there throwing it out slightly):

enter image description here

To adjust the spacing, you can use the SpacingBetweenLines class:

TableCellProperties tcp = new TableCellProperties();

TableCellBorders tcb = new TableCellBorders(
        new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.BasicThinLines) },
        new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.Double) });

//set the alignment to "Center"
TableCellVerticalAlignment tcVA = new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center };

tcp.Append(tcb);
tcp.Append(tcVA);
gCell.Append(tcp);

Paragraph para = gCell.AppendChild(new Paragraph());

//Adjust the spacing between lines
ParagraphProperties paraProps = new ParagraphProperties();
SpacingBetweenLines spacing = new SpacingBetweenLines() { After = "0" };
paraProps.SpacingBetweenLines = spacing;
para.ParagraphProperties = paraProps;

Run run = para.AppendChild(new Run());
run.AppendChild(new Text("28.329,46 €"));

This gives this result by default:

enter image description here

And expanding the cell shows it really is now in the center:

enter image description here

Related