News

Checkboxes as plain symbols

  • 12 / 21 / 2012

Although one can easily introduce real checkboxes in a Word document generated by PHPDocX via the embedHTML method (just include the corresponding web form and form elements in the HTML code to be parsed and inserted), sometimes it may be useful just to include a checkbox like one includes a plain symbol in teh MS Word interface.

One can easily check that in the Wingdings font the character numbers 111 and 254 correspond to an empty checkbox and a checked one respectively (of course, other options with other charsets are also available).

With that info it is now simple to include the required checkboxes as plain symbols via PHPDocX:



require_once('pathToPHPDocX/classes/CreateDocx.inc');
$docx = new CreateDocx();
$checkbox = chr(111);
$checked = chr(254);

//checkbox run
$run_1 =array(
'font' => 'Wingdings',
'sz' => '12',
'type' => 'text',
'text' => $checkbox
);
$run_2 =array(
'font' => 'Arial',
'sz' => '11',
'type' => 'text',
'text' => ' This is a text for an empty checkbox. '
);
$parameters = array($run_1, $run_2);
$docx->addParagraph($parameters, array());
//checked run
$run_1 =array(
'font' => 'Wingdings',
'sz' => '12',
'type' => 'text',
'text' => $checked
);
$run_2 =array(
'font' => 'Arial',
'sz' => '11',
'type' => 'text',
'text' => ' and now is checked. '
);
$parameters = array($run_1, $run_2);
$docx->addParagraph($parameters, array());

$docx->createDocx('test_checkbox_symbol');



The resulting Word document looks like this:



Of course this method can be extended to any other symbol....just make sure that you use a font that is available to you final users.

Post scriptum: Our friend Raymond Otto from www.digiwhite.nl has asked how to get rid of the extra space between lines in the above example. There are different ways to do so but maybe the simplest one is:


$html = ''.chr(111).' an empty checkbox'.chr(254).'a checked checkbox';
$docx->embedHTML($html);



By the way, if you wrap the spans with tags you will get a similar result to the first method proposed in this blog entry.