Phpword: Cannot align text

Created on 8 Jul 2016  路  14Comments  路  Source: PHPOffice/PHPWord

Hi,

I'm using PHPWord 12.1 but I don't manage to align text. The following code generates bold text but aligned to the left:

<?php
date_default_timezone_set('Europe/Paris');
require_once 'vendor/autoload.php';

$phpWord = new \PhpOffice\PhpWord\PhpWord();

// New portrait section
$section = $phpWord->addSection();

// Simple text
$section->addTitle('Welcome to PhpWord', 1);
$section->addText('Hello World!');

// Two text break
$section->addTextBreak(2);

// Define styles
$section->addText('Centered', array('bold' => true), array('alignment' => 'center'));

// Save file
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('sample.docx');

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
$objWriter->save('sample.odt');

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
$objWriter->save('sample.html');

Thanks


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Most helpful comment

$section->addText("Center", array('bold' => true, "size"=>25), array('align' => 'center'));

All 14 comments

use
$section->addText('some text', [], [ 'align' => \PhpOffice\PhpWord\Style\Alignment::ALIGN_CENTER ]);

Thanks!

However, it does not work for ODText format, only for Word2007 (I use LibreOffice 5).

$table->addCell(6500)->addText("Text", [], ['align' => \PhpOffice\PhpWord\Style\Cell::VALIGN_CENTER ]);

Currently I am Struggling for alignment in phpword (0.14.0) .I am Not able to do perform align left,right, center. But for alignment Justify i am using this code it's working for me

$phpWord->setDefaultParagraphStyle(
array(
'align' => 'both',
'spaceAfter' => \PhpOffice\PhpWord\Shared\Converter::pointToTwip(12),
'spacing' => 120,
)
);

i want to know alignment ( left,right, center ) . code If anybody knows please let me know

Hi @troosan,

How can I valign text in a cell? Below methods seems not to work, only I can center it horizontally but not vertically:

$fancyTableCellStyle = array('valign' => 'center');
$fancyTableFontStyle = array('bold' => true, 'color' => 'FFFFFF');
$tableCenterValignText = array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER, 'align' => \PhpOffice\PhpWord\Style\Cell::VALIGN_CENTER);

$table->addCell(3000, $fancyTableCellStyle)->addText('Acct Name', $fancyTableFontStyle, $tableCenterValignText)

Regards!

@mcabanatan please check Sample_09_Tables.php, there's an example there

HI @troosan,

My above code came from Sample_09_Tables.php which doesnt work. I'm opening the word in word2013. Any chance you can try it on your end?

Regards

This is what I'm getting (word 2016 for mac)
image

Well that works perfect! I don't know why it doesn't work on mine(word2013). Can I have the code you use to generate the word file?

Thanks in advance!

@mcabanatan That's exactly from Sample_09_Tables.php

@mcabanatan set space in $tableCenterValignText

$tableCenterValignText = array(
    'spaceBefore'=>0,
    'spaceAfter'=>0,
    'alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER, 
    'align' => \PhpOffice\PhpWord\Style\Cell::VALIGN_CENTER
);

$section->addText('Centered', array('bold' => true), array('align' => 'center'));

$section->addText("Center", array('bold' => true, "size"=>25), array('align' => 'center'));

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Joel-James picture Joel-James  路  3Comments

ahmednawazbutt picture ahmednawazbutt  路  3Comments

cedrictailly picture cedrictailly  路  5Comments

fishwolf picture fishwolf  路  4Comments

jt6a74 picture jt6a74  路  5Comments