Forum


Replies: 1   Views: 399
Checkbox background color and tickmarks
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 gtassinari  · 29-12-2023 - 01:32

Hello,

Hello I am using PHPDOCX advanced version 14.I have been trying to add a checkbox background color as white, i have used both the bgColor as well as tried to color option. The bgColor keeps the background as grey, the color makes the border white but still keeps the background grey. See below my code as well as the output

            $wordFragment = new WordFragment($docx);
            $checkboxOptions = array(
                'fontSize' => 8,
                'defaultValue' => !empty($p['findings_detail']['environmentalLiensFound']),
                'backgroundColor' => 'FFFFFF'
            );
            $wordFragment->addFormElement('checkbox', $checkboxOptions);
            $docx->replaceVariableByWordFragment(array(
                'env_found' => $wordFragment,
                'env_found_detail' => $wordFragment
            ),
                array('type' => 'inline', 'firstMatch' => 'true'));

Output: https://pasteboard.co/pWiaEO1UagqH.png

Also, is there anyway to make the checkboxes windings character 254 like a tickmark instead of an 'X'

Thanks

Karthik

Posted by admin  · 29-12-2023 - 09:33

Hello,

Please note that the addFormElement method adds legacy form contents. These legacy contents have limited styles. In this case, the grey background is not a color applied to the checkbox but a MS Word setting.
If you run the following code:

$checkboxOptions = array('fontSize' => 12, 'defaultValue' => true);
$docx->addFormElement('checkbox', $checkboxOptions);
$docx->docxSettings(array('doNotShadeFormData' => true));

You set the doNotShadeFormData as true using docxSettings, so the checkbox background won't be grey.

Instead of addFormElement, we recommend you use addStructuredDocumentTag. For example:

$docx->addStructuredDocumentTag('checkbox', ['sz' => 18, 'fontSize' => 18, 'checked' => true]);
$docx->addStructuredDocumentTag('checkbox', ['sz' => 18, 'fontSize' => 18, 'checked' => true, 'highlightColor' => 'red']);

Regarding the character used for the checkbox, sorry but there's no option to change it to a custom one using the current stable release of phpdocx.
We have opened a task to the dev team to include this feature in this next stable release of phpdocx (there's no release date), and they have generated a patch in the testing branch, so a custom font and char using addStructuredDocumentTag can be set. For example, to use wingdings and tickmark:

$docx->addStructuredDocumentTag('checkbox', 
  [
    'sz' => 18,
    'fontSize' => 18,
    'checked' => true,
    'checkedState' => ['font' => 'Wingdings', 'value' => '00FE'],
    'uncheckedState' => ['font' => 'Wingdings', 'value' => '006F'],
    'sym' => ['char' => '00FE', 'font' => 'Wingdings'],
  ]
);

Please note that these new features available in the testing branch are only available when LUS is active (https://www.phpdocx.com/updates) If you upgrade your license to phpdocx 14.5 and include LUS on MY PHPDOCX page after login, we can send it to your email address. In this case, please send to contact[at]phpdocx.com if you are using the classic or the namespaces package, and we'll send you the updated class and a sample using it.

Regards.