From 43d5aa345e45a672a12166583efe753a0fcd1686 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Wed, 23 Apr 2014 04:08:02 +0700 Subject: [PATCH] DOCX Reader: Read titles --- CHANGELOG.md | 2 +- src/PhpWord/Reader/Word2007.php | 37 ++++++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62cfe1a9..7a62003c 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,7 +27,7 @@ This release marked heavy refactorings on internal code structure with the creat - Object: Ability to add object in header, footer, textrun, and footnote - @ivanlanin GH-187 - Media: Add `Media::resetElements()` to reset all media data - @juzi GH-19 - General: Add `Style::resetStyles()`, `Footnote::resetElements()`, and `TOC::resetTitles()` - @ivanlanin GH-187 -- DOCX Reader: Ability to read header, footer, footnotes, link, preservetext, textbreak, pagebreak, table, list, image - @ivanlanin +- DOCX Reader: Ability to read header, footer, footnotes, link, preservetext, textbreak, pagebreak, table, list, image, and title - @ivanlanin - Endnote: Ability to add endnotes - @ivanlanin - ListItem: Ability to create custom list and reset list number - @ivanlanin GH-10 GH-198 - ODT Writer: Basic table writing support - @ivanlanin diff --git a/src/PhpWord/Reader/Word2007.php b/src/PhpWord/Reader/Word2007.php index 8cb081cd..1ce9ed1a 100644 --- a/src/PhpWord/Reader/Word2007.php +++ b/src/PhpWord/Reader/Word2007.php @@ -20,7 +20,7 @@ use PhpOffice\PhpWord\Element\Section; * Reader for Word2007 * * @since 0.8.0 - * @todo title, list, watermark, checkbox, toc + * @todo watermark, checkbox, toc * @todo Partly done: image, object */ class Word2007 extends AbstractReader implements ReaderInterface @@ -216,11 +216,14 @@ class Word2007 extends AbstractReader implements ReaderInterface switch ($node->nodeName) { case 'w:p': // Paragraph + // Page break + // @todo if ($xmlReader->getAttribute('w:type', $node, 'w:r/w:br') == 'page') { $section->addPageBreak(); // PageBreak - } else { - $this->readParagraph($xmlReader, $node, $section, 'document'); } + + // Paragraph + $this->readParagraph($xmlReader, $node, $section, 'document'); // Section properties if ($xmlReader->elementExists('w:pPr/w:sectPr', $node)) { $settingsNode = $xmlReader->getElement('w:pPr/w:sectPr', $node); @@ -269,18 +272,23 @@ class Word2007 extends AbstractReader implements ReaderInterface if (is_null($name)) { $name = $xmlReader->getAttribute('w:val', $node, 'w:name'); } + preg_match('/Heading(\d)/', $name, $headingMatches); // $default = ($xmlReader->getAttribute('w:default', $node) == 1); switch ($type) { case 'paragraph': $pStyle = $this->readParagraphStyle($xmlReader, $node); $fStyle = $this->readFontStyle($xmlReader, $node); - if (empty($fStyle)) { - if (is_array($pStyle)) { - $this->phpWord->addParagraphStyle($name, $pStyle); - } + if (!empty($headingMatches)) { + $this->phpWord->addTitleStyle($headingMatches[1], $fStyle, $pStyle); } else { - $this->phpWord->addFontStyle($name, $fStyle, $pStyle); + if (empty($fStyle)) { + if (is_array($pStyle)) { + $this->phpWord->addParagraphStyle($name, $pStyle); + } + } else { + $this->phpWord->addFontStyle($name, $fStyle, $pStyle); + } } break; @@ -477,8 +485,12 @@ class Word2007 extends AbstractReader implements ReaderInterface { // Paragraph style $pStyle = null; + $headingMatches = array(); if ($xmlReader->elementExists('w:pPr', $domNode)) { $pStyle = $this->readParagraphStyle($xmlReader, $domNode); + if (is_string($pStyle)) { + preg_match('/Heading(\d)/', $pStyle, $headingMatches); + } } // PreserveText @@ -518,6 +530,15 @@ class Word2007 extends AbstractReader implements ReaderInterface } $parent->addListItem($textContent, $levelId, null, "PHPWordList{$numId}", $pStyle); + // Heading + } elseif (!empty($headingMatches)) { + $textContent = ''; + $nodes = $xmlReader->getElements('w:r', $domNode); + foreach ($nodes as $node) { + $textContent .= $xmlReader->getValue('w:t', $node); + } + $parent->addTitle($textContent, $headingMatches[1]); + // Text and TextRun } else { $runCount = $xmlReader->countElements('w:r', $domNode);