PHPWord添加文本到指定位置

WBOY
풀어 주다: 2016-07-06 13:54:10
원래의
3646명이 탐색했습니다.

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

PHPWord添加文本到指定位置

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

部分代码:

<code>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();
                        }</code>
로그인 후 복사
로그인 후 복사

回复内容:

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

PHPWord添加文本到指定位置

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

部分代码:

<code>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();
                        }</code>
로그인 후 복사
로그인 후 복사

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

관련 라벨:
php
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!