Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot align text #834

Closed
Vayel opened this issue Jul 8, 2016 · 14 comments
Closed

Cannot align text #834

Vayel opened this issue Jul 8, 2016 · 14 comments

Comments

@Vayel
Copy link

Vayel commented Jul 8, 2016

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.

@aslubsky
Copy link

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

@Vayel
Copy link
Author

Vayel commented Jul 17, 2016

Thanks!

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

@lythanhngodev
Copy link

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

@venkata-koatiah
Copy link

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

@troosan
Copy link
Contributor

troosan commented Apr 17, 2018

@troosan troosan closed this as completed Apr 17, 2018
@mcabanatan
Copy link

mcabanatan commented May 11, 2018

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!

@troosan
Copy link
Contributor

troosan commented May 11, 2018

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

@mcabanatan
Copy link

mcabanatan commented May 15, 2018

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

@troosan
Copy link
Contributor

troosan commented May 17, 2018

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

@mcabanatan
Copy link

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!

@troosan
Copy link
Contributor

troosan commented May 20, 2018

@mcabanatan That's exactly from Sample_09_Tables.php

@luizz
Copy link

luizz commented May 15, 2019

@mcabanatan set space in $tableCenterValignText

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

@Tamn9208
Copy link

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

@Stream3rPixel
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

9 participants