当我们使用phpword制作word的时候,常常需要使用打钩的复选框,接下来分享一个自用的方法
以下为php方法:
/**
* 输出一组复选框
* @param $textRun 容器
* @param $map 数组
* @param $key 选中的值
* @param $fStyle 字体
* @param array $option 可选项
*/
protected function renderCheckBox($textRun,$map,$key,$fStyle,$option=[]){
//方框的大小
$checkSize=empty($option['checkSize']) ? 14:$option['checkSize'];
foreach ($map as $k => $v) {
if(strpos($k,'space')!==false){//添加空格
$textRun->addText(str_repeat(' ',$v),$fStyle);
continue;
}
if(strpos($k,'break')!==false){//添加换行
$textRun->addTextBreak($v);
continue;
}
//方框在文字后面
if( isset($option['after']) && $option['after']==true ){
$textRun->addText($v,$fStyle);
}
if($k==$key){//选中
//$textRun->addText(self::CHECKED,array('name' => 'Wingdings 2', 'size' =>$checkSize));//此方式不兼容wps
$textRun->addText(self::CHECKED,array('name' => 'Wingdings', 'size' =>$checkSize));
}else{//不选中
//$textRun->addText(self::CHECKBOX,array('name' => '宋体', 'size' =>$checkSize));//此方式不兼容wps
$textRun->addText(self::CHECKBOX,array('name' => 'Wingdings', 'size' =>$checkSize));
}
//方框在文字前面
if(empty($option['after']) || $option['after']==false){
$textRun->addText($v,$fStyle);
}
}
}