Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In PHPWord, Right and Left Align Words

Tags:

php

phpword

I was wondering if there was anyway to left and right align some text on the same line. So for example a resume would have a company name aligned to the left, and a date aligned to the right, on the same line.

I was trying to do this with a text run, but doesn't seem to work. I know I can use \t\t but the left text is going to be different lengths, so the tabs will be very inconsistent.

This is just a simple example:

$section = $phpWord->addSection();
$textrun = $section->addTextRun();
$textrun->addText("Left Text", array(), array("align" => "left"));
$textrun->addText("Right Text", array(), array("align" => "right"));
like image 627
James Avatar asked Mar 02 '16 22:03

James


2 Answers

You can achieve the effect of different alignment on the same line of text using a paragraph style with a single custom tab stop, right aligned against the right margin.

$section = $phpWord->addSection();

$section_style = $section->getStyle();
$position =
    $section_style->getPageSizeW()
    - $section_style->getMarginRight()
    - $section_style->getMarginLeft();
$phpWord->addParagraphStyle("leftRight", array("tabs" => array(
    new \PhpOffice\PhpWord\Style\Tab("right", $position)
)));

$section->addText("Left Text\tRight Text", array(), "leftRight");
like image 115
Matt Raines Avatar answered Sep 18 '22 00:09

Matt Raines


You should to do this, just define style by apart and send as parameter in addTextRun.

   $phpWord->addParagraphStyle('pStyler', array('align' => 'right'));

   $textrun = $section->addTextRun('pStyler');

   $textrun->addText(htmlspecialchars("Some text here..."));
like image 40
Juan José Rodríguez Avatar answered Sep 18 '22 00:09

Juan José Rodríguez