2
votes

I am trying to create a neat Word document using the PhpWord library. Unfortunately, the documentation seems to be incomplete. I'd like to have paragraphs with a border and a background color, spanning the whole page width. I did some research and found the attributes borderSize and borderColor which are undocumented for the addText method. There also is a bgColor attribute which can be set on text. The following example creates a paragraph surrounded with a border. The text has some background color too, but it's just the text, not the whole area surrounded by the border. The attribute bgColor unfortunately does not have any effect on paragraph level.

Does anyone have an idea on how to achieve this?

use \PhpOffice\PhpWord\IOFactory;
$document = new \PhpOffice\PhpWord\PhpWord();
$titlepage = $document->createSection();
$titlepage->addText('My Title', 
    array("size" => 24, "color" => "FF0000", "bgColor"=>"00FF00"),
    array("borderSize"=>6, "borderColor"=>"0000FF")
);
$objWriter = IOFactory::createWriter($document, 'Word2007');
1

1 Answers

0
votes

I just found out by looking at the source code:

If you want to set the paragraph background color you will have to set the shading of the paragraph style:

$section->addText('Your text', array(), array('shading' => array('fill' => 'dddddd')));

The background color for the paragraph style is set as the fill of the shading.

As far I can tell, you have to read the PhpWord API

http://phpoffice.github.io/PHPWord/docs/master/classes/PhpOffice.PhpWord.Style.Shading.html

and / or the docx specification

http://officeopenxml.com/WPshading.php

to find the right parameters for the "shading".