PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.16k stars 2.68k forks source link

Contents of the cells are not vertically centered #2583

Open philips81 opened 3 months ago

philips81 commented 3 months ago

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

FredrikMBP commented 1 month ago

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!