PHPWord
PHPWord copied to clipboard
Contents of the cells are not vertically centered
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
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!