PHPWord icon indicating copy to clipboard operation
PHPWord copied to clipboard

Contents of the cells are not vertically centered

Open philips81 opened this issue 11 months ago • 1 comments

Hi, I have to use phpword 0.18.3 and when creating the tables I encounter the problem shown in the image.

$cell = $row->addCell(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(9), ['valign' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER]);

The contents of the cells are not vertically centered and I cannot do this from Office or another word editor. Does anyone have a solution? Thanks

Screenshot 2024-03-08 093141

philips81 avatar Mar 08 '24 08:03 philips81

Hi @philips81,

I'm using 1.20 and I've been facing this issue as well. After much trial and error I found that I needed to add "spaceAfter" twice on the text in the cell.

$headerTextStyle = array(
    'bold'       => true,
    'spaceAfter' => 0
);

$textStyle = array(
    'spaceAfter' => 0
);

$tableStyle = array(
    'width'       => 100 * 50, // Word 2007 table width, in percentages, is measured in 50ths of a percent
    'unit'        => 'pct',
    'alignment'   => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER,
    'cellMargin'  => 100,
);

$table = new \PhpOffice\PhpWord\Element\Table($tableStyle);

$row = $table->addRow();
$row->addCell()->addText('Vertically centered heading', $headerTextStyle, $textStyle);

$row = $table->addRow();
$row->addCell()->addText('Vertically centered', $textStyle, textStyle);

I hope this helps!

FredrikMBP avatar May 22 '24 08:05 FredrikMBP