Forum


Replies: 1   Views: 796
Use highlight instead of background color
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by ordersvrc  · 14-10-2021 - 21:10

Right now, any attempt at highlighting results in adding the paragraph shading instead.  I need to actually highlight text.  I have tried everything but nothing seems to work.

Posted by admin  · 15-10-2021 - 06:23

Hello,

A shading color can be added using the backgroundColor option available in addText:

$paragraphOptions = array(
    'backgroundColor' => 'FF0000'
);
$docx->addText('Lorem ipsum.', $paragraphOptions);

A highlight color can be added using the highlightColor option:

$paragraphOptions = array(
    'highlightColor' => 'red'
);
$docx->addText('Lorem ipsum.', $paragraphOptions);

Please note, as explained on the API doc page (https://www.phpdocx.com/api-documentation/word-content/add-paragraph-text-Word-document-with-PHP), that backgroundColor (shading color) can be a HEX value, but highlightColor is limited to specific color values (MS Word only supports the following highlight colors: black, blue, cyan, green, magenta, red, yellow, white, darkBlue, darkCyan, darkGreen, darkMagenta, darkRed, darkYellow, darkGray, lightGray, none).

If you are using HTML methods:

$docx->embedHTML('<p style="background-color: #FF0000;">Lorem ipsum.</p>');

$docx->embedHTML('<p><span style="background-color: #FF0000;">Lorem ipsum.</span></p>');

phpdocx always adds a shading style, not a highlight style.

UPDATE: phpdocx 12.5 added the data-highlight style to apply a highlight style when importing HTML in Premium licenses (HTML Extended and CSS Extended features).

Also note that you can use the searchAndHighlight method to highlight specific keywords in a DOCX.

If you are transforming HTML to DOCX and you need to add highlight styles (instead of or in addition to the default shading styles), please send an email to contact[at]phpdocx.com with the most simple sample that illustrates your case and we'll check it. Maybe HTML Extended can include a new specific extended style to accomplish your needs.

Regards.