Skip to content

Commit 43d5aa3

Browse files
committed
DOCX Reader: Read titles
1 parent 0060e43 commit 43d5aa3

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ This release marked heavy refactorings on internal code structure with the creat
2727
- Object: Ability to add object in header, footer, textrun, and footnote - @ivanlanin GH-187
2828
- Media: Add `Media::resetElements()` to reset all media data - @juzi GH-19
2929
- General: Add `Style::resetStyles()`, `Footnote::resetElements()`, and `TOC::resetTitles()` - @ivanlanin GH-187
30-
- DOCX Reader: Ability to read header, footer, footnotes, link, preservetext, textbreak, pagebreak, table, list, image - @ivanlanin
30+
- DOCX Reader: Ability to read header, footer, footnotes, link, preservetext, textbreak, pagebreak, table, list, image, and title - @ivanlanin
3131
- Endnote: Ability to add endnotes - @ivanlanin
3232
- ListItem: Ability to create custom list and reset list number - @ivanlanin GH-10 GH-198
3333
- ODT Writer: Basic table writing support - @ivanlanin

src/PhpWord/Reader/Word2007.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* Reader for Word2007
2121
*
2222
* @since 0.8.0
23-
* @todo title, list, watermark, checkbox, toc
23+
* @todo watermark, checkbox, toc
2424
* @todo Partly done: image, object
2525
*/
2626
class Word2007 extends AbstractReader implements ReaderInterface
@@ -216,11 +216,14 @@ private function readDocument($xmlFile)
216216
switch ($node->nodeName) {
217217

218218
case 'w:p': // Paragraph
219+
// Page break
220+
// @todo <w:lastRenderedPageBreak>
219221
if ($xmlReader->getAttribute('w:type', $node, 'w:r/w:br') == 'page') {
220222
$section->addPageBreak(); // PageBreak
221-
} else {
222-
$this->readParagraph($xmlReader, $node, $section, 'document');
223223
}
224+
225+
// Paragraph
226+
$this->readParagraph($xmlReader, $node, $section, 'document');
224227
// Section properties
225228
if ($xmlReader->elementExists('w:pPr/w:sectPr', $node)) {
226229
$settingsNode = $xmlReader->getElement('w:pPr/w:sectPr', $node);
@@ -269,18 +272,23 @@ private function readStyles($xmlFile)
269272
if (is_null($name)) {
270273
$name = $xmlReader->getAttribute('w:val', $node, 'w:name');
271274
}
275+
preg_match('/Heading(\d)/', $name, $headingMatches);
272276
// $default = ($xmlReader->getAttribute('w:default', $node) == 1);
273277
switch ($type) {
274278

275279
case 'paragraph':
276280
$pStyle = $this->readParagraphStyle($xmlReader, $node);
277281
$fStyle = $this->readFontStyle($xmlReader, $node);
278-
if (empty($fStyle)) {
279-
if (is_array($pStyle)) {
280-
$this->phpWord->addParagraphStyle($name, $pStyle);
281-
}
282+
if (!empty($headingMatches)) {
283+
$this->phpWord->addTitleStyle($headingMatches[1], $fStyle, $pStyle);
282284
} else {
283-
$this->phpWord->addFontStyle($name, $fStyle, $pStyle);
285+
if (empty($fStyle)) {
286+
if (is_array($pStyle)) {
287+
$this->phpWord->addParagraphStyle($name, $pStyle);
288+
}
289+
} else {
290+
$this->phpWord->addFontStyle($name, $fStyle, $pStyle);
291+
}
284292
}
285293
break;
286294

@@ -477,8 +485,12 @@ private function readParagraph(XMLReader $xmlReader, \DOMElement $domNode, &$par
477485
{
478486
// Paragraph style
479487
$pStyle = null;
488+
$headingMatches = array();
480489
if ($xmlReader->elementExists('w:pPr', $domNode)) {
481490
$pStyle = $this->readParagraphStyle($xmlReader, $domNode);
491+
if (is_string($pStyle)) {
492+
preg_match('/Heading(\d)/', $pStyle, $headingMatches);
493+
}
482494
}
483495

484496
// PreserveText
@@ -518,6 +530,15 @@ private function readParagraph(XMLReader $xmlReader, \DOMElement $domNode, &$par
518530
}
519531
$parent->addListItem($textContent, $levelId, null, "PHPWordList{$numId}", $pStyle);
520532

533+
// Heading
534+
} elseif (!empty($headingMatches)) {
535+
$textContent = '';
536+
$nodes = $xmlReader->getElements('w:r', $domNode);
537+
foreach ($nodes as $node) {
538+
$textContent .= $xmlReader->getValue('w:t', $node);
539+
}
540+
$parent->addTitle($textContent, $headingMatches[1]);
541+
521542
// Text and TextRun
522543
} else {
523544
$runCount = $xmlReader->countElements('w:r', $domNode);

0 commit comments

Comments
 (0)