PHPWord添加文本到指定位置
大家讲道理
大家讲道理 2017-04-10 17:58:31
[PHP讨论组]

想导出word文档,将文本添加到文档的指定位置,比如前一段话的后面,而前一段话是循环生成的,该段后面又有循环生成的另一段话,而我想加入的文本不能放在循环里,只能在两次循环输出文本之后添加,这样的话我怎么才能把文本输出到紧接着的第一个循环之后?
例如:

我想将答案放在问题的后面,而问题和选项都是循环生成的,怎么才能实现将其放在“处分”的后面??
求大神赐教

部分代码:

foreach ($ques['answer'] as $key => $an) {
            $textrun1 = $section->createTextRun('rStyle');
                            if (is_array($an['synopsis'])) {
                                $textrun1->addText($this->aZ[$key] . ".", 'rStyle');
                                foreach ($an['synopsis'] as $i) {
                                    if (isset($i['type']) && $i['type'] == "text") {
                                        $textrun1->addText($i['value'], 'rStyle');
                                    }
                                    if (isset($i['type']) && $i['type'] == "img" && file_exists($img = ROOT_DIR . str_replace(ROOT_URL, '', $i['value']))) {
                                        $imageStyle = array(
                                            'width' => $i['width'] == 0 ? 350 : $i['width'],
                                            'height' => $i['height'] == 0 ? 350 : $i['height']
                                            // 'align' => 'center',
                                        );
                                        $textrun1->addImage($img, $imageStyle);
                                    }
                                }
                                unset($textrun1);
                            } else {
                                $section->addText(strtoupper($this->aZ[$key]) . "." . $an['synopsis'], 'rStyle');
                            }
                            if ($an['value']) {
                                $answer[] = $this->aZ[$key];
                            }
                            $section->addTextBreak();
                        }
                        //判断是否输出答案
                        if($withAnswer==1){
                            $section->addText("(" . implode($answer, ",") . ")", 'rStyle');
                            $section->addTextBreak();
                            $answer = array();
                        }
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(1)
黄舟

已解决,采用TextRun,将想要连接在一起的文本用同一个textrun对象表示出来。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号