Forum


Replies: 3   Views: 148
A label next to a checkbox

Posted by aventyret  · 11-09-2024 - 13:19

I need to be able to add chewckboxes with a label after it. So like:
 

|CHECKBOX| I am a sausage

 This kind of works ok when using html and embedding it.

$html = '<input type="checkbox"/> <span style="font-size: 12px">' . $label . '</span>';

But then I get the legacy form component checkbox. I want to use the command

addStructuredDocumentTag('checkbox')

But I can't figure out how to be able to write text to the right (opr left) of it. Any ideas`?

Posted by admin  · 11-09-2024 - 13:49

Hello,

You need to create a WordFragment and add it as inline content. addStructuredDocumentaTag supports working as an inline WordFragment since the release of phpdocx 13.5.

In the Contents/addStructuredDocumentaTag/sample_1.php example included in the package you can find the following code that illustrates how to add a checkbox and text in the same paragraph:

// checkbox added as WordFragment
$checkboxFragment = new WordFragment($docx, 'document');
$checkboxFragment->addStructuredDocumentTag('checkbox', ['fontSize' => 11, 'checked' => true]);
$docx->addText([array('text' => 'Checkbox: ', 'fontSize' => 12), $checkboxFragment]);

To add the checkbox and then the text you need to set them in the required order:

// checkbox added as WordFragment
$checkboxFragment = new WordFragment($docx, 'document');
$checkboxFragment->addStructuredDocumentTag('checkbox', ['fontSize' => 11, 'checked' => true]);
$docx->addText([$checkboxFragment, array('text' => ' checkbox', 'fontSize' => 12)]);

Regards.

Posted by aventyret  · 11-09-2024 - 14:43

Thank you. That was a perfect solution.

I will need to do the same with a richtext component too. But I would need to retain the styling of it. Is that at all possible? 

So what I need to do there is to have a richtext Structured Document Tag with a border. So far so good. I managed that. But after this richtext I need some text that is outside it. Inline, after it just like the checkbox. 

When I use the same inline technique as with the checkbox, all the styling of the Structured Tag disappears. Since it's inside a paragraph I suppose.

 

Thank you kindly for your help.



 

Posted by admin  · 11-09-2024 - 15:59

Hello,

When a content is added as inline content, only inline styles remain. Block contents/styles are removed.

You can add a rich text content as inline content:

// richText
$richTextFragment = new WordFragment($docx, 'document');
$options = array(
        'placeholderText' => 'A content in a rich text',
        'alias' => 'Rich text',
);
$richTextFragment->addStructuredDocumentTag('richText', $options);
$docx->addText([array('text' => 'Rich text: ', 'fontSize' => 12), $richTextFragment]);

(block styles are removed from the inline contents).

You can also apply paragraph options to the whole paragraph that contains inline contents:

$paragraphOptions = array(
        'textAlign' => 'center',
);
$docx->addText([array('text' => 'Rich text: ', 'fontSize' => 12), $richTextFragment], $paragraphOptions);

but these styles apply to the entire paragraph.

The addText method supports adding character borders to run-of-text contents:

$docx->addText([array('text' => 'Rich text: ', 'fontSize' => 12, 'characterBorder' => array('single')), $richTextFragment], $paragraphOptions);

 but the characterBorder style is not supported when creating a structured document tag content in the current stable version of phpdocx.

The current testing branch includes more rPr styles that can be applied to structured document tags. For example to include a character border:

$richTextFragment = new WordFragment($docx, 'document');
$options = array(
        'placeholderText' => 'A content in a rich text',
        'alias' => 'Rich text',
        'characterBorder' => array('type' => 'single'),
);
$richTextFragment->addStructuredDocumentTag('richText', $options);

This is a run-of-text style, so it remains when adding the inline content with addText.
Please note that changes from the testing branch are only available for licenses with active LUS (https://www.phpdocx.com/updates). If you purchase LUS for your license (MY PHPDOCX page after login), please send a message to contact[at]phpdocx.com and we'll send you the updated class.

A similar approach can be done using DOCXCustomizer in the stable release of phpdocx, but this feature is only available in Premium licenses.

Regards.