DOCX Reader: Read titles

This commit is contained in:
Ivan Lanin 2014-04-23 04:08:02 +07:00
parent 0060e4316c
commit 43d5aa345e
2 changed files with 30 additions and 9 deletions

View File

@ -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

View File

@ -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 <w:lastRenderedPageBreak>
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);