private function _writeTOC(PHPWord_Shared_XMLWriter $objWriter = null) { $titles = PHPWord_TOC::getTitles(); $styleFont = PHPWord_TOC::getStyleFont(); $styleTOC = PHPWord_TOC::getStyleTOC(); $fIndent = $styleTOC->getIndent(); $tabLeader = $styleTOC->getTabLeader(); $tabPos = $styleTOC->getTabPos(); $isObject = $styleFont instanceof PHPWord_Style_Font ? true : false; for ($i = 0; $i < count($titles); $i++) { $title = $titles[$i]; $indent = ($title['depth'] - 1) * $fIndent; $objWriter->startElement('w:p'); $objWriter->startElement('w:pPr'); if ($isObject && !is_null($styleFont->getParagraphStyle())) { $this->_writeParagraphStyle($objWriter, $styleFont->getParagraphStyle()); } if ($indent > 0) { $objWriter->startElement('w:ind'); $objWriter->writeAttribute('w:left', $indent); $objWriter->endElement(); } if (!empty($styleFont) && !$isObject) { $objWriter->startElement('w:pPr'); $objWriter->startElement('w:pStyle'); $objWriter->writeAttribute('w:val', $styleFont); $objWriter->endElement(); $objWriter->endElement(); } $objWriter->startElement('w:tabs'); $objWriter->startElement('w:tab'); $objWriter->writeAttribute('w:val', 'right'); if (!empty($tabLeader)) { $objWriter->writeAttribute('w:leader', $tabLeader); } $objWriter->writeAttribute('w:pos', $tabPos); $objWriter->endElement(); $objWriter->endElement(); $objWriter->endElement(); // w:pPr if ($i == 0) { $objWriter->startElement('w:r'); $objWriter->startElement('w:fldChar'); $objWriter->writeAttribute('w:fldCharType', 'begin'); $objWriter->endElement(); $objWriter->endElement(); $objWriter->startElement('w:r'); $objWriter->startElement('w:instrText'); $objWriter->writeAttribute('xml:space', 'preserve'); $objWriter->writeRaw('TOC \\o "1-9" \\h \\z \\u'); $objWriter->endElement(); $objWriter->endElement(); $objWriter->startElement('w:r'); $objWriter->startElement('w:fldChar'); $objWriter->writeAttribute('w:fldCharType', 'separate'); $objWriter->endElement(); $objWriter->endElement(); } $objWriter->startElement('w:hyperlink'); $objWriter->writeAttribute('w:anchor', $title['anchor']); $objWriter->writeAttribute('w:history', '1'); $objWriter->startElement('w:r'); if ($isObject) { $this->_writeTextStyle($objWriter, $styleFont); } $objWriter->startElement('w:t'); $objWriter->writeRaw($title['text']); $objWriter->endElement(); $objWriter->endElement(); $objWriter->startElement('w:r'); $objWriter->writeElement('w:tab', null); $objWriter->endElement(); $objWriter->startElement('w:r'); $objWriter->startElement('w:fldChar'); $objWriter->writeAttribute('w:fldCharType', 'begin'); $objWriter->endElement(); $objWriter->endElement(); $objWriter->startElement('w:r'); $objWriter->startElement('w:instrText'); $objWriter->writeAttribute('xml:space', 'preserve'); $objWriter->writeRaw('PAGEREF ' . $title['anchor'] . ' \\h'); $objWriter->endElement(); $objWriter->endElement(); $objWriter->startElement('w:r'); $objWriter->startElement('w:fldChar'); $objWriter->writeAttribute('w:fldCharType', 'end'); $objWriter->endElement(); $objWriter->endElement(); $objWriter->endElement(); // w:hyperlink $objWriter->endElement(); // w:p } $objWriter->startElement('w:p'); $objWriter->startElement('w:r'); $objWriter->startElement('w:fldChar'); $objWriter->writeAttribute('w:fldCharType', 'end'); $objWriter->endElement(); $objWriter->endElement(); $objWriter->endElement(); }
/** * Add a Title Element * * @param string $text * @param int $depth * @return PHPWord_Section_Title */ public function addTitle($text, $depth = 1) { $styles = PHPWord_Style::getStyles(); if (array_key_exists('Heading_' . $depth, $styles)) { $style = 'Heading' . $depth; } else { $style = null; } $title = new PHPWord_Section_Title($text, $depth, $style); $data = PHPWord_TOC::addTitle($text, $depth); $anchor = $data[0]; $bookmarkId = $data[1]; $title->setAnchor($anchor); $title->setBookmarkId($bookmarkId); $this->_elementCollection[] = $title; return $title; }
/** * Create a new Table-of-Contents Element * * @param array $styleFont * @param array $styleTOC */ public function __construct($styleFont = null, $styleTOC = null) { self::$_styleTOC = new PHPWord_Style_TOC(); if (!is_null($styleTOC) && is_array($styleTOC)) { foreach ($styleTOC as $key => $value) { if (substr($key, 0, 1) != '_') { $key = '_' . $key; } self::$_styleTOC->setStyleValue($key, $value); } } if (!is_null($styleFont)) { if (is_array($styleFont)) { self::$_styleFont = new PHPWord_Style_Font(); foreach ($styleFont as $key => $value) { if (substr($key, 0, 1) != '_') { $key = '_' . $key; } self::$_styleFont->setStyleValue($key, $value); } } else { self::$_styleFont = $styleFont; } } }