Forum


Replies: 4   Views: 546
Question about adding form elements on the same line as text
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 tjchen  · 20-04-2023 - 08:53

Dear phpdocx support team,

I am using phpdocx to generate Word documents and I have a question about adding form elements on the same line as text. I am currently using the following code:

$item->addText('1.1', ['font' => 'Arial', 'fontSize' => 8, 'indent_left' => 10]);
$item->addFormElement('select', ['selectOptions' => [' 0 ', ' 1 ', ' 2 ', ' 3 ']]);

However, this adds the form element on a new line instead of on the same line as the text. Is there a way to add form elements on the same line as text using phpdocx?

Thank you for your help.

Posted by tjchen  · 20-04-2023 - 09:25

I have tried the approach like this :

$item = new WordFragment($docx);
$content1 = new WordFragment($docx);
$content2 = new WordFragment($docx);
$content1->addText('1.1', ['font' => 'Arial', 'fontSize' => 8, 'indent_left' => 10]);
$content2->addFormElement('select', ['selectOptions' => [' 0 ', ' 1 ', ' 2 ', ' 3 ']]);
$content = array();
$content[] = $content1;
$content[] = $content2;
$item->addText($content);

But the text and the form element are still not in the same line. Is there anything wrong with the code?

Posted by admin  · 20-04-2023 - 09:51

Hello,

What version of phpdocx are you using?

We have run the the following code using phpdocx 13.5:

$docx = new CreateDocx();

$content1 = new WordFragment($docx);
$content1->addText('1.1', ['font' => 'Arial', 'fontSize' => 8, 'indent_left' => 10]);

$content2 = new WordFragment($docx);
$content2->addFormElement('select', ['selectOptions' => [' 0 ', ' 1 ', ' 2 ', ' 3 ']]);

$content = array();
$content[] = $content1;
$content[] = $content2;

$item = new WordFragment($docx);
$item->addText($content);

$docx->addText(array($item));

$docx->createDocx('output.docx');

and the output is correct. Both contents appear in the same line.

Also note that you can avoid generating an extra WordFragment:

$docx = new CreateDocx();

$content1 = new WordFragment($docx);
$content1->addText('1.1', ['font' => 'Arial', 'fontSize' => 8, 'indent_left' => 10]);

$content2 = new WordFragment($docx);
$content2->addFormElement('select', ['selectOptions' => [' 0 ', ' 1 ', ' 2 ', ' 3 ']]);

$docx->addText(array($content1, $content2));

$docx->createDocx('output.docx');

Please note that adding form and structured document tags as inline contents was added in phpdocx 13.5.

Regards.

Posted by tjchen  · 21-04-2023 - 01:12

I have tried again and it works. Thank you very much.