diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index 686644eb..9b598906 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -22,6 +22,7 @@ namespace PhpOffice\PhpWord\Element; * * @method Text addText(string $text, mixed $fStyle = null, mixed $pStyle = null) * @method TextRun addTextRun(mixed $pStyle = null) + * @method Bookmark addBookmark(string $name) * @method Link addLink(string $target, string $text = null, mixed $fStyle = null, mixed $pStyle = null) * @method PreserveText addPreserveText(string $text, mixed $fStyle = null, mixed $pStyle = null) * @method void addTextBreak(int $count = 1, mixed $fStyle = null, mixed $pStyle = null) @@ -78,7 +79,7 @@ abstract class AbstractContainer extends AbstractElement public function __call($function, $args) { $elements = array( - 'Text', 'TextRun', 'Link', 'PreserveText', 'TextBreak', + 'Text', 'TextRun', 'Bookmark', 'Link', 'PreserveText', 'TextBreak', 'ListItem', 'ListItemRun', 'Table', 'Image', 'Object', 'Footnote', 'Endnote', 'CheckBox', 'TextBox', 'Field', 'Line', 'Shape', 'Title', 'TOC', 'PageBreak', @@ -189,6 +190,7 @@ abstract class AbstractContainer extends AbstractElement ); $validContainers = array( 'Text' => $allContainers, + 'Bookmark' => $allContainers, 'Link' => $allContainers, 'TextBreak' => $allContainers, 'Image' => $allContainers, diff --git a/src/PhpWord/Element/Bookmark.php b/src/PhpWord/Element/Bookmark.php new file mode 100644 index 00000000..39b6d69a --- /dev/null +++ b/src/PhpWord/Element/Bookmark.php @@ -0,0 +1,64 @@ +name = String::toUTF8($name); + return $this; + } + + /** + * Get Bookmark name + * + * @return string + */ + public function getName() + { + return $this->name; + } + +} diff --git a/src/PhpWord/Element/Link.php b/src/PhpWord/Element/Link.php index 4b00dc90..81349b5d 100644 --- a/src/PhpWord/Element/Link.php +++ b/src/PhpWord/Element/Link.php @@ -61,6 +61,13 @@ class Link extends AbstractElement */ protected $mediaRelation = true; + /** + * Has internal flag - anchor to internal bookmark + * + * @var bool + */ + protected $internal = false; + /** * Create a new Link Element * @@ -69,13 +76,13 @@ class Link extends AbstractElement * @param mixed $fontStyle * @param mixed $paragraphStyle */ - public function __construct($source, $text = null, $fontStyle = null, $paragraphStyle = null) + public function __construct($source, $text = null, $fontStyle = null, $paragraphStyle = null, $internal = false) { $this->source = String::toUTF8($source); $this->text = is_null($text) ? $this->source : String::toUTF8($text); $this->fontStyle = $this->setNewStyle(new Font('text'), $fontStyle); $this->paragraphStyle = $this->setNewStyle(new Paragraph(), $paragraphStyle); - + $this->internal = $internal; return $this; } @@ -154,4 +161,14 @@ class Link extends AbstractElement { return $this->getText(); } + + /** + * is internal + * + * @return bool + */ + public function isInternal() + { + return $this->internal; + } } diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index 3b991e23..93835d91 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -27,6 +27,7 @@ use PhpOffice\PhpWord\Exception\Exception; * @method Collection\Footnotes getFootnotes() * @method Collection\Endnotes getEndnotes() * @method Collection\Charts getCharts() + * @method int addBookmark(Element\Bookmark $bookmark) * @method int addTitle(Element\Title $title) * @method int addFootnote(Element\Footnote $footnote) * @method int addEndnote(Element\Endnote $endnote) @@ -82,7 +83,7 @@ class PhpWord public function __construct() { // Collection - $collections = array('Titles', 'Footnotes', 'Endnotes', 'Charts'); + $collections = array('Bookmarks', 'Titles', 'Footnotes', 'Endnotes', 'Charts'); foreach ($collections as $collection) { $class = 'PhpOffice\\PhpWord\\Collection\\' . $collection; $this->collections[$collection] = new $class(); @@ -113,7 +114,7 @@ class PhpWord $addCollection = array(); $addStyle = array(); - $collections = array('Title', 'Footnote', 'Endnote', 'Chart'); + $collections = array('Bookmark', 'Title', 'Footnote', 'Endnote', 'Chart'); foreach ($collections as $collection) { $getCollection[] = strtolower("get{$collection}s"); $addCollection[] = strtolower("add{$collection}"); @@ -218,10 +219,9 @@ class PhpWord } /** - * Set default font name. + * Set default font name * * @param string $fontName - * @return void */ public function setDefaultFontName($fontName) { @@ -239,10 +239,9 @@ class PhpWord } /** - * Set default font size. + * Set default font size * * @param int $fontSize - * @return void */ public function setDefaultFontSize($fontSize) { diff --git a/src/PhpWord/Writer/Word2007/Element/Bookmark.php b/src/PhpWord/Writer/Word2007/Element/Bookmark.php new file mode 100644 index 00000000..df5a104a --- /dev/null +++ b/src/PhpWord/Writer/Word2007/Element/Bookmark.php @@ -0,0 +1,49 @@ +getXmlWriter(); + $element = $this->getElement(); + if (!$element instanceof \PhpOffice\PhpWord\Element\Bookmark) { + return; + } + + $rId = $element->getRelationId(); + + $xmlWriter->startElement('w:bookmarkStart'); + $xmlWriter->writeAttribute('w:id', $rId); + $xmlWriter->writeAttribute('w:name', $element->getName()); + $xmlWriter->endElement(); + + $xmlWriter->startElement('w:bookmarkEnd'); + $xmlWriter->writeAttribute('w:id', $rId); + $xmlWriter->endElement(); + } +} diff --git a/src/PhpWord/Writer/Word2007/Element/Link.php b/src/PhpWord/Writer/Word2007/Element/Link.php index 2a5a5ae1..31bfa3fd 100644 --- a/src/PhpWord/Writer/Word2007/Element/Link.php +++ b/src/PhpWord/Writer/Word2007/Element/Link.php @@ -25,9 +25,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; class Link extends Text { /** - * Write link element. - * - * @return void + * Write link element */ public function write() { @@ -42,7 +40,11 @@ class Link extends Text $this->startElementP(); $xmlWriter->startElement('w:hyperlink'); - $xmlWriter->writeAttribute('r:id', 'rId' . $rId); + if($element->isInternal()) { + $xmlWriter->writeAttribute('w:anchor', $element->getSource()); + }else { + $xmlWriter->writeAttribute('r:id', 'rId' . $rId); + } $xmlWriter->writeAttribute('w:history', '1'); $xmlWriter->startElement('w:r'); diff --git a/src/PhpWord/Writer/Word2007/Element/Title.php b/src/PhpWord/Writer/Word2007/Element/Title.php index f48e932a..2c2fe0b0 100644 --- a/src/PhpWord/Writer/Word2007/Element/Title.php +++ b/src/PhpWord/Writer/Word2007/Element/Title.php @@ -25,9 +25,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; class Title extends AbstractElement { /** - * Write title element. - * - * @return void + * Write title element */ public function write() { @@ -50,10 +48,11 @@ class Title extends AbstractElement } $rId = $element->getRelationId(); + $bookmarkRId = $element->getPhpWord()->addBookmark(); // Bookmark start for TOC $xmlWriter->startElement('w:bookmarkStart'); - $xmlWriter->writeAttribute('w:id', $rId); + $xmlWriter->writeAttribute('w:id', $bookmarkRId); $xmlWriter->writeAttribute('w:name', "_Toc{$rId}"); $xmlWriter->endElement(); @@ -66,7 +65,7 @@ class Title extends AbstractElement // Bookmark end $xmlWriter->startElement('w:bookmarkEnd'); - $xmlWriter->writeAttribute('w:id', $rId); + $xmlWriter->writeAttribute('w:id', $bookmarkRId); $xmlWriter->endElement(); $xmlWriter->endElement();