Forum


Replies: 2   Views: 55
Make checkbox only take up as much space as it needs

Posted by aventyret  · 30-11-2024 - 09:50

I'm using PHPDocx to render word documents from json data. WOrks fantastic. But... We ran in to some issues with checkboxes. We need to add rows of checkboxes with a label after.

So basically like this:
 

[ ] alternative 1
[x] alternative 2

To do that we do this:
 

    $checkboxFragment = new WordFragment($docx);
    $checkboxFragment->addStructuredDocumentTag('checkbox', [
        'fontSize' => 12,
    ]);

    $docx->addText(
        [
            $checkboxFragment,
            [
                'text' => ' alternative 1',
                'fontSize' => 10
            ],
        ]);

This also works fine. But the problem is that the structured tag for checkbox take up the space that the little handle needs, which is twice the height of the actual checkbox. So we end up having too much vertical space between the checkboxes
(see here: https://pasteboard.co/sRb8eKT2ZvgZ.png)

How do we make this smaller?
 

Posted by admin  · 30-11-2024 - 10:46

Hello,

Please note that in this case the spacing is generated by the paragraph.

If each checkbox and text are added in a paragraph, you can use spacing styles to get a smaller spacing:

$docx->addText(
    [
        $checkboxFragment1,
        [
            'text' => ' alternative 1',
            'fontSize' => 10
        ],
    ],
    [
        'spacingBottom' => 0,
        'spacingTop' => 0,
    ]
);

$docx->addText(
    [
        $checkboxFragment2,
        [
            'text' => ' alternative 2',
            'fontSize' => 10
        ],
    ],
    [
        'spacingBottom' => 0,
        'spacingTop' => 0,
    ]
);

Or add line breaks in the same paragraph:

$docx->addText(
    [
        $checkboxFragment1,
        [
            'text' => ' alternative 1' . "\n",
            'fontSize' => 10,
        ],
        $checkboxFragment2,
        [
            'text' => ' alternative 2' . "\n",
            'fontSize' => 10,
        ],
    ],
    [
        'parseLineBreaks' => true,
    ]
);

Regards.

Posted by aventyret  · 30-11-2024 - 19:59

Oh, of course! Thank you so much. Excellent support as usual :-)