Forum


Replies: 3   Views: 907
Can you add font stylings on template variable replacements?
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 admin  · 23-03-2022 - 18:56

Hello,

Yes, you can replace placeholders by a new text that includes styles. Even by other contents such as images, tables, charts... or multiple contents.

You need to use WordFragments and the replaceVariableByWordFragment method. On the previous page and the package you can find samples that detail how to use this method. Please also check the following page availabler in the Practical guide about working with WordFragments : https://www.phpdocx.com/documentation/practical/wordfragments-and-wordml

The following sample replaces a text placeholder by a new text content with styles (bold, color and fontSize):

$docx = new CreateDocxFromTemplate('template.docx');

$textFragment = new WordFragment($docx, 'document');
$textStyles = array(
    'bold' => true,
    'color' => 'B70000',
    'fontSize' => 24,
);
$textFragment->addText('new text', $textStyles);

$docx->replaceVariableByWordFragment(array('WORDFRAGMENT' => $textFragment), array('type' => 'inline'));

$docx->createDocx('output');

WordFragment is a very powerful class included in phpdocx to add contents.

HTML could also be used to replace a placeholder using replaceVariableByHTML, but please note that transforming HTML adds default styles.

Please note the license you are using includes support tickets. If needed, you can open a ticket on MY PHPDOCX page after log in with the user that purchased the license, attaching a DOCX template you are using and we'll generate a custom sample using it.

Regards.