Overview

Packages

  • PHP
  • PHPWord

Classes

  • PHPWord
  • PHPWord_Autoloader
  • PHPWord_DocumentProperties
  • PHPWord_HashTable
  • PHPWord_IOFactory
  • PHPWord_Media
  • PHPWord_Section
  • PHPWord_Section_Footer
  • PHPWord_Section_Footer_PreserveText
  • PHPWord_Section_Header
  • PHPWord_Section_Image
  • PHPWord_Section_Link
  • PHPWord_Section_ListItem
  • PHPWord_Section_MemoryImage
  • PHPWord_Section_Object
  • PHPWord_Section_PageBreak
  • PHPWord_Section_Settings
  • PHPWord_Section_Table
  • PHPWord_Section_Table_Cell
  • PHPWord_Section_Table_Row
  • PHPWord_Section_Text
  • PHPWord_Section_TextBreak
  • PHPWord_Section_TextRun
  • PHPWord_Section_Title
  • PHPWord_Shared_Drawing
  • PHPWord_Shared_File
  • PHPWord_Shared_Font
  • PHPWord_Shared_String
  • PHPWord_Shared_XMLWriter
  • PHPWord_Shared_ZipStreamWrapper
  • PHPWord_Style
  • PHPWord_Style_Cell
  • PHPWord_Style_Font
  • PHPWord_Style_Image
  • PHPWord_Style_ListItem
  • PHPWord_Style_Paragraph
  • PHPWord_Style_Row
  • PHPWord_Style_Tab
  • PHPWord_Style_Table
  • PHPWord_Style_TableFull
  • PHPWord_Style_Tabs
  • PHPWord_Style_TOC
  • PHPWord_Template
  • PHPWord_TOC
  • PHPWord_Writer_ODText
  • PHPWord_Writer_ODText_Content
  • PHPWord_Writer_ODText_Manifest
  • PHPWord_Writer_ODText_Meta
  • PHPWord_Writer_ODText_Mimetype
  • PHPWord_Writer_ODText_Styles
  • PHPWord_Writer_ODText_WriterPart
  • PHPWord_Writer_RTF
  • PHPWord_Writer_Word2007
  • PHPWord_Writer_Word2007_Base
  • PHPWord_Writer_Word2007_ContentTypes
  • PHPWord_Writer_Word2007_DocProps
  • PHPWord_Writer_Word2007_Document
  • PHPWord_Writer_Word2007_DocumentRels
  • PHPWord_Writer_Word2007_Footer
  • PHPWord_Writer_Word2007_Header
  • PHPWord_Writer_Word2007_Rels
  • PHPWord_Writer_Word2007_Styles
  • PHPWord_Writer_Word2007_WriterPart

Interfaces

  • PHPWord_Writer_IWriter

Exceptions

  • PHPWord_Exception
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * PHPWord
  4:  *
  5:  * Copyright (c) 2013 PHPWord
  6:  *
  7:  * This library is free software; you can redistribute it and/or
  8:  * modify it under the terms of the GNU Lesser General Public
  9:  * License as published by the Free Software Foundation; either
 10:  * version 2.1 of the License, or (at your option) any later version.
 11:  *
 12:  * This library is distributed in the hope that it will be useful,
 13:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 15:  * Lesser General Public License for more details.
 16:  *
 17:  * You should have received a copy of the GNU Lesser General Public
 18:  * License along with this library; if not, write to the Free Software
 19:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 20:  *
 21:  * @category   PHPWord
 22:  * @package    PHPWord
 23:  * @copyright  Copyright (c) 2013 PHPWord
 24:  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
 25:  * @version    0.7.0
 26:  */
 27: 
 28: /**
 29:  * Class PHPWord_Section
 30:  */
 31: class PHPWord_Section
 32: {
 33: 
 34:     /**
 35:      * Section count
 36:      *
 37:      * @var int
 38:      */
 39:     private $_sectionCount;
 40: 
 41:     /**
 42:      * Section settings
 43:      *
 44:      * @var PHPWord_Section_Settings
 45:      */
 46:     private $_settings;
 47: 
 48:     /**
 49:      * Section Element Collection
 50:      *
 51:      * @var array
 52:      */
 53:     private $_elementCollection = array();
 54: 
 55:     /**
 56:      * Section Headers
 57:      *
 58:      * @var array
 59:      */
 60:     private $_headers = array();
 61: 
 62:     /**
 63:      * Section Footer
 64:      *
 65:      * @var PHPWord_Section_Footer
 66:      */
 67:     private $_footer = null;
 68: 
 69: 
 70:     /**
 71:      * Create a new Section
 72:      *
 73:      * @param int $sectionCount
 74:      * @param mixed $settings
 75:      */
 76:     public function __construct($sectionCount, $settings = null)
 77:     {
 78:         $this->_sectionCount = $sectionCount;
 79:         $this->_settings = new PHPWord_Section_Settings();
 80: 
 81:         if (!is_null($settings) && is_array($settings)) {
 82:             foreach ($settings as $key => $value) {
 83:                 if (substr($key, 0, 1) != '_') {
 84:                     $key = '_' . $key;
 85:                 }
 86:                 $this->_settings->setSettingValue($key, $value);
 87:             }
 88:         }
 89:     }
 90: 
 91:     /**
 92:      * Get Section Settings
 93:      *
 94:      * @return PHPWord_Section_Settings
 95:      */
 96:     public function getSettings()
 97:     {
 98:         return $this->_settings;
 99:     }
100: 
101:     /**
102:      * Add a Text Element
103:      *
104:      * @param string $text
105:      * @param mixed $styleFont
106:      * @param mixed $styleParagraph
107:      * @return PHPWord_Section_Text
108:      */
109:     public function addText($text, $styleFont = null, $styleParagraph = null)
110:     {
111:         if (!PHPWord_Shared_String::IsUTF8($text)) {
112:             $text = utf8_encode($text);
113:         }
114:         $text = new PHPWord_Section_Text($text, $styleFont, $styleParagraph);
115:         $this->_elementCollection[] = $text;
116:         return $text;
117:     }
118: 
119:     /**
120:      * Add a Link Element
121:      *
122:      * @param string $linkSrc
123:      * @param string $linkName
124:      * @param mixed $styleFont
125:      * @param mixed $styleParagraph
126:      * @return PHPWord_Section_Link
127:      */
128:     public function addLink($linkSrc, $linkName = null, $styleFont = null, $styleParagraph = null)
129:     {
130:         if (!PHPWord_Shared_String::IsUTF8($linkSrc)) {
131:             $linkSrc = utf8_encode($linkSrc);
132:         }
133:         if (!is_null($linkName)) {
134:             if (!PHPWord_Shared_String::IsUTF8($linkName)) {
135:                 $linkName = utf8_encode($linkName);
136:             }
137:         }
138: 
139:         $link = new PHPWord_Section_Link($linkSrc, $linkName, $styleFont, $styleParagraph);
140:         $rID = PHPWord_Media::addSectionLinkElement($linkSrc);
141:         $link->setRelationId($rID);
142: 
143:         $this->_elementCollection[] = $link;
144:         return $link;
145:     }
146: 
147:     /**
148:      * Add a TextBreak Element
149:      *
150:      * @param int $count
151:      */
152:     public function addTextBreak($count = 1)
153:     {
154:         for ($i = 1; $i <= $count; $i++) {
155:             $this->_elementCollection[] = new PHPWord_Section_TextBreak();
156:         }
157:     }
158: 
159:     /**
160:      * Add a PageBreak Element
161:      */
162:     public function addPageBreak()
163:     {
164:         $this->_elementCollection[] = new PHPWord_Section_PageBreak();
165:     }
166: 
167:     /**
168:      * Add a Table Element
169:      *
170:      * @param mixed $style
171:      * @return PHPWord_Section_Table
172:      */
173:     public function addTable($style = null)
174:     {
175:         $table = new PHPWord_Section_Table('section', $this->_sectionCount, $style);
176:         $this->_elementCollection[] = $table;
177:         return $table;
178:     }
179: 
180:     /**
181:      * Add a ListItem Element
182:      *
183:      * @param string $text
184:      * @param int $depth
185:      * @param mixed $styleFont
186:      * @param mixed $styleList
187:      * @param mixed $styleParagraph
188:      * @return PHPWord_Section_ListItem
189:      */
190:     public function addListItem($text, $depth = 0, $styleFont = null, $styleList = null, $styleParagraph = null)
191:     {
192:         if (!PHPWord_Shared_String::IsUTF8($text)) {
193:             $text = utf8_encode($text);
194:         }
195:         $listItem = new PHPWord_Section_ListItem($text, $depth, $styleFont, $styleList, $styleParagraph);
196:         $this->_elementCollection[] = $listItem;
197:         return $listItem;
198:     }
199: 
200:     /**
201:      * Add a OLE-Object Element
202:      *
203:      * @param string $src
204:      * @param mixed $style
205:      * @return PHPWord_Section_Object
206:      */
207:     public function addObject($src, $style = null)
208:     {
209:         $object = new PHPWord_Section_Object($src, $style);
210: 
211:         if (!is_null($object->getSource())) {
212:             $inf = pathinfo($src);
213:             $ext = $inf['extension'];
214:             if (strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') {
215:                 $ext = substr($ext, 0, -1);
216:             }
217: 
218:             $iconSrc = PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/';
219:             if (!file_exists($iconSrc . '_' . $ext . '.png')) {
220:                 $iconSrc = $iconSrc . '_default.png';
221:             } else {
222:                 $iconSrc .= '_' . $ext . '.png';
223:             }
224: 
225:             $rIDimg = PHPWord_Media::addSectionMediaElement($iconSrc, 'image');
226:             $data = PHPWord_Media::addSectionMediaElement($src, 'oleObject');
227:             $rID = $data[0];
228:             $objectId = $data[1];
229: 
230:             $object->setRelationId($rID);
231:             $object->setObjectId($objectId);
232:             $object->setImageRelationId($rIDimg);
233: 
234:             $this->_elementCollection[] = $object;
235:             return $object;
236:         } else {
237:             trigger_error('Source does not exist or unsupported object type.');
238:         }
239:     }
240: 
241:     /**
242:      * Add a Image Element
243:      *
244:      * @param string $src
245:      * @param mixed $style
246:      * @return PHPWord_Section_Image
247:      */
248:     public function addImage($src, $style = null)
249:     {
250:         $image = new PHPWord_Section_Image($src, $style);
251: 
252:         if (!is_null($image->getSource())) {
253:             $rID = PHPWord_Media::addSectionMediaElement($src, 'image');
254:             $image->setRelationId($rID);
255: 
256:             $this->_elementCollection[] = $image;
257:             return $image;
258:         } else {
259:             trigger_error('Source does not exist or unsupported image type.');
260:         }
261:     }
262: 
263:     /**
264:      * Add a by PHP created Image Element
265:      *
266:      * @param string $link
267:      * @param mixed $style
268:      * @return PHPWord_Section_MemoryImage
269:      */
270:     public function addMemoryImage($link, $style = null)
271:     {
272:         $memoryImage = new PHPWord_Section_MemoryImage($link, $style);
273:         if (!is_null($memoryImage->getSource())) {
274:             $rID = PHPWord_Media::addSectionMediaElement($link, 'image', $memoryImage);
275:             $memoryImage->setRelationId($rID);
276: 
277:             $this->_elementCollection[] = $memoryImage;
278:             return $memoryImage;
279:         } else {
280:             trigger_error('Unsupported image type.');
281:         }
282:     }
283: 
284:     /**
285:      * Add a Table-of-Contents Element
286:      *
287:      * @param mixed $styleFont
288:      * @param mixed $styleTOC
289:      * @return PHPWord_TOC
290:      */
291:     public function addTOC($styleFont = null, $styleTOC = null)
292:     {
293:         $toc = new PHPWord_TOC($styleFont, $styleTOC);
294:         $this->_elementCollection[] = $toc;
295:         return $toc;
296:     }
297: 
298:     /**
299:      * Add a Title Element
300:      *
301:      * @param string $text
302:      * @param int $depth
303:      * @return PHPWord_Section_Title
304:      */
305:     public function addTitle($text, $depth = 1)
306:     {
307:         if (!PHPWord_Shared_String::IsUTF8($text)) {
308:             $text = utf8_encode($text);
309:         }
310:         $styles = PHPWord_Style::getStyles();
311:         if (array_key_exists('Heading_' . $depth, $styles)) {
312:             $style = 'Heading' . $depth;
313:         } else {
314:             $style = null;
315:         }
316: 
317:         $title = new PHPWord_Section_Title($text, $depth, $style);
318: 
319:         $data = PHPWord_TOC::addTitle($text, $depth);
320:         $anchor = $data[0];
321:         $bookmarkId = $data[1];
322: 
323:         $title->setAnchor($anchor);
324:         $title->setBookmarkId($bookmarkId);
325: 
326:         $this->_elementCollection[] = $title;
327:         return $title;
328:     }
329: 
330:     /**
331:      * Create a new TextRun
332:      *
333:      * @return PHPWord_Section_TextRun
334:      */
335:     public function createTextRun($styleParagraph = null)
336:     {
337:         $textRun = new PHPWord_Section_TextRun($styleParagraph);
338:         $this->_elementCollection[] = $textRun;
339:         return $textRun;
340:     }
341: 
342:     /**
343:      * Get all Elements
344:      *
345:      * @return array
346:      */
347:     public function getElements()
348:     {
349:         return $this->_elementCollection;
350:     }
351: 
352:     /**
353:      * Create a new Header
354:      *
355:      * @return PHPWord_Section_Header
356:      */
357:     public function createHeader()
358:     {
359:         $header = new PHPWord_Section_Header($this->_sectionCount);
360:         $this->_headers[] = $header;
361:         return $header;
362:     }
363: 
364:     /**
365:      * Get Headers
366:      *
367:      * @return array
368:      */
369:     public function getHeaders()
370:     {
371:         return $this->_headers;
372:     }
373: 
374:     /**
375:      * Is there a header for this section that is for the first page only?
376:      *
377:      * If any of the PHPWord_Section_Header instances have a type of
378:      * PHPWord_Section_Header::FIRST then this method returns true. False
379:      * otherwise.
380:      *
381:      * @return Boolean
382:      */
383:     public function hasDifferentFirstPage()
384:     {
385:         $value = array_filter($this->_headers, function (PHPWord_Section_Header &$header) {
386:             return $header->getType() == PHPWord_Section_Header::FIRST;
387:         });
388:         return count($value) > 0;
389:     }
390: 
391:     /**
392:      * Create a new Footer
393:      *
394:      * @return PHPWord_Section_Footer
395:      */
396:     public function createFooter()
397:     {
398:         $footer = new PHPWord_Section_Footer($this->_sectionCount);
399:         $this->_footer = $footer;
400:         return $footer;
401:     }
402: 
403:     /**
404:      * Get Footer
405:      *
406:      * @return PHPWord_Section_Footer
407:      */
408:     public function getFooter()
409:     {
410:         return $this->_footer;
411:     }
412: }
PHPWord API Docs API documentation generated by ApiGen 2.8.0