Skip to content

Commit d3ac5b1

Browse files
committed
Merge branch '#193' into develop
2 parents a57b28d + 5c2c687 commit d3ac5b1

File tree

7 files changed

+180
-4
lines changed

7 files changed

+180
-4
lines changed

docs/recipes.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,27 @@ Use ``php://output`` as the filename.
4141
header('Expires: 0');
4242
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
4343
$xmlWriter->save("php://output");
44+
45+
Create numbered headings
46+
------------------------
47+
48+
Define a numbering style and title styles, and match the two styles (with ``pStyle`` and ``numStyle``) like below.
49+
50+
.. code-block:: php
51+
52+
$phpWord->addNumberingStyle(
53+
'hNum',
54+
array('type' => 'multilevel', 'levels' => array(
55+
array('pStyle' => 'Heading1', 'format' => 'decimal', 'text' => '%1'),
56+
array('pStyle' => 'Heading2', 'format' => 'decimal', 'text' => '%1.%2'),
57+
array('pStyle' => 'Heading3', 'format' => 'decimal', 'text' => '%1.%2.%3'),
58+
)
59+
)
60+
);
61+
$phpWord->addTitleStyle(1, array('size' => 16), array('numStyle' => 'hNum', 'numLevel' => 0));
62+
$phpWord->addTitleStyle(2, array('size' => 14), array('numStyle' => 'hNum', 'numLevel' => 1));
63+
$phpWord->addTitleStyle(3, array('size' => 12), array('numStyle' => 'hNum', 'numLevel' => 2));
64+
65+
$section->addTitle('Heading 1', 1);
66+
$section->addTitle('Heading 2', 2);
67+
$section->addTitle('Heading 3', 3);

docs/src/documentation.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,29 @@ $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
986986
$xmlWriter->save("php://output");
987987
```
988988

989+
## Create numbered headings
990+
991+
Define a numbering style and title styles, and match the two styles (with `pStyle` and `numStyle`) like below.
992+
993+
```php
994+
$phpWord->addNumberingStyle(
995+
'hNum',
996+
array('type' => 'multilevel', 'levels' => array(
997+
array('pStyle' => 'Heading1', 'format' => 'decimal', 'text' => '%1'),
998+
array('pStyle' => 'Heading2', 'format' => 'decimal', 'text' => '%1.%2'),
999+
array('pStyle' => 'Heading3', 'format' => 'decimal', 'text' => '%1.%2.%3'),
1000+
)
1001+
)
1002+
);
1003+
$phpWord->addTitleStyle(1, array('size' => 16), array('numStyle' => 'hNum', 'numLevel' => 0));
1004+
$phpWord->addTitleStyle(2, array('size' => 14), array('numStyle' => 'hNum', 'numLevel' => 1));
1005+
$phpWord->addTitleStyle(3, array('size' => 12), array('numStyle' => 'hNum', 'numLevel' => 2));
1006+
1007+
$section->addTitle('Heading 1', 1);
1008+
$section->addTitle('Heading 2', 2);
1009+
$section->addTitle('Heading 3', 3);
1010+
```
1011+
9891012
# Frequently asked questions
9901013

9911014
## Is this the same with PHPWord that I found in CodePlex?

samples/Sample_14_ListItem.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
array('format' => 'decimal', 'text' => '%1.', 'left' => 360, 'hanging' => 360, 'tabPos' => 360),
1919
array('format' => 'upperLetter', 'text' => '%2.', 'left' => 720, 'hanging' => 360, 'tabPos' => 720),
2020
)
21-
)
21+
)
2222
);
2323
$predefinedMultilevel = array('listType' => \PhpOffice\PhpWord\Style\ListItem::TYPE_NUMBER_NESTED);
2424

@@ -66,6 +66,25 @@
6666
$listItemRun->addText(' underlined', array('underline'=>'dash'));
6767
$section->addTextBreak(2);
6868

69+
// Numbered heading
70+
71+
$phpWord->addNumberingStyle(
72+
'headingNumbering',
73+
array('type' => 'multilevel', 'levels' => array(
74+
array('pStyle' => 'Heading1', 'format' => 'decimal', 'text' => '%1'),
75+
array('pStyle' => 'Heading2', 'format' => 'decimal', 'text' => '%1.%2'),
76+
array('pStyle' => 'Heading3', 'format' => 'decimal', 'text' => '%1.%2.%3'),
77+
)
78+
)
79+
);
80+
$phpWord->addTitleStyle(1, array('size' => 16), array('numStyle' => 'headingNumbering', 'numLevel' => 0));
81+
$phpWord->addTitleStyle(2, array('size' => 14), array('numStyle' => 'headingNumbering', 'numLevel' => 1));
82+
$phpWord->addTitleStyle(3, array('size' => 12), array('numStyle' => 'headingNumbering', 'numLevel' => 2));
83+
84+
$section->addTitle('Heading 1', 1);
85+
$section->addTitle('Heading 2', 2);
86+
$section->addTitle('Heading 3', 3);
87+
6988
// Save file
7089
echo write($phpWord, basename(__FILE__, '.php'), $writers);
7190
if (!CLI) {

src/PhpWord/Style/NumberingLevel.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ class NumberingLevel extends AbstractStyle
5656
*/
5757
private $restart;
5858

59+
/**
60+
* Related paragraph style
61+
*
62+
* @var string
63+
* @link http://www.schemacentral.com/sc/ooxml/e-w_pStyle-2.html
64+
*/
65+
private $pStyle;
66+
5967
/**
6068
* Content between numbering symbol and paragraph text
6169
*
@@ -205,6 +213,28 @@ public function setRestart($value)
205213
return $this;
206214
}
207215

216+
/**
217+
* Get related paragraph style
218+
*
219+
* @return string
220+
*/
221+
public function getPStyle()
222+
{
223+
return $this->pStyle;
224+
}
225+
226+
/**
227+
* Set related paragraph style
228+
*
229+
* @param string $value
230+
* @return self
231+
*/
232+
public function setPStyle($value)
233+
{
234+
$this->pStyle = $value;
235+
return $this;
236+
}
237+
208238
/**
209239
* Get suffix
210240
*

src/PhpWord/Style/Paragraph.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,20 @@ class Paragraph extends AbstractStyle
114114
*/
115115
private $alignment;
116116

117+
/**
118+
* Numbering style name
119+
*
120+
* @var string
121+
*/
122+
private $numStyle;
123+
124+
/**
125+
* Numbering level
126+
*
127+
* @var int
128+
*/
129+
private $numLevel = 0;
130+
117131
/**
118132
* Create new instance
119133
*/
@@ -532,6 +546,52 @@ public function setSpace($value = null)
532546
return $this;
533547
}
534548

549+
/**
550+
* Get numbering style name
551+
*
552+
* @return string
553+
*/
554+
public function getNumStyle()
555+
{
556+
return $this->numStyle;
557+
}
558+
559+
/**
560+
* Set numbering style name
561+
*
562+
* @param string $value
563+
* @return self
564+
*/
565+
public function setNumStyle($value)
566+
{
567+
$this->numStyle = $value;
568+
569+
return $this;
570+
}
571+
572+
/**
573+
* Get numbering level
574+
*
575+
* @return int
576+
*/
577+
public function getNumLevel()
578+
{
579+
return $this->numLevel;
580+
}
581+
582+
/**
583+
* Set numbering level
584+
*
585+
* @param int $value
586+
* @return self
587+
*/
588+
public function setNumLevel($value = 0)
589+
{
590+
$this->numLevel = $this->setIntVal($value, $this->numLevel);
591+
592+
return $this;
593+
}
594+
535595
/**
536596
* Get allow first/last line to display on a separate page setting
537597
*

src/PhpWord/Writer/Word2007/Part/Numbering.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function write()
5858
$levels = $style->getLevels();
5959

6060
$xmlWriter->startElement('w:abstractNum');
61-
$xmlWriter->writeAttribute('w:abstractNumId', $style->getNumId());
61+
$xmlWriter->writeAttribute('w:abstractNumId', $style->getIndex());
6262

6363
$xmlWriter->startElement('w:nsid');
6464
$xmlWriter->writeAttribute('w:val', $this->getRandomHexNumber());
@@ -81,9 +81,9 @@ public function write()
8181
foreach ($styles as $style) {
8282
if ($style instanceof NumberingStyle) {
8383
$xmlWriter->startElement('w:num');
84-
$xmlWriter->writeAttribute('w:numId', $style->getNumId());
84+
$xmlWriter->writeAttribute('w:numId', $style->getIndex());
8585
$xmlWriter->startElement('w:abstractNumId');
86-
$xmlWriter->writeAttribute('w:val', $style->getNumId());
86+
$xmlWriter->writeAttribute('w:val', $style->getIndex());
8787
$xmlWriter->endElement(); // w:abstractNumId
8888
$xmlWriter->endElement(); // w:num
8989
}
@@ -107,6 +107,7 @@ private function writeLevel(XMLWriter $xmlWriter, NumberingLevel $level)
107107
'start' => 'start',
108108
'format' => 'numFmt',
109109
'restart' => 'lvlRestart',
110+
'pStyle' => 'pStyle',
110111
'suffix' => 'suff',
111112
'text' => 'lvlText',
112113
'align' => 'lvlJc'

src/PhpWord/Writer/Word2007/Style/Paragraph.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
1919

20+
use PhpOffice\PhpWord\Style;
2021
use PhpOffice\PhpWord\Style\Alignment as AlignmentStyle;
2122

2223
/**
@@ -117,6 +118,24 @@ private function writeStyle()
117118
$xmlWriter->endElement();
118119
}
119120

121+
// Numbering
122+
$numStyleName = $style->getNumStyle();
123+
$numStyleObject = Style::getStyle($numStyleName);
124+
if ($numStyleName !== null && $numStyleObject !== null) {
125+
$xmlWriter->startElement('w:numPr');
126+
$xmlWriter->startElement('w:numId');
127+
$xmlWriter->writeAttribute('w:val', $numStyleObject->getIndex());
128+
$xmlWriter->endElement(); // w:numId
129+
$xmlWriter->startElement('w:ilvl');
130+
$xmlWriter->writeAttribute('w:val', $style->getNumLevel());
131+
$xmlWriter->endElement(); // w:ilvl
132+
$xmlWriter->endElement(); // w:numPr
133+
134+
$xmlWriter->startElement('w:outlineLvl');
135+
$xmlWriter->writeAttribute('w:val', $style->getNumLevel());
136+
$xmlWriter->endElement(); // w:outlineLvl
137+
}
138+
120139
if (!$this->withoutPPR) {
121140
$xmlWriter->endElement(); // w:pPr
122141
}

0 commit comments

Comments
 (0)