Skip to content

Commit

Permalink
#193: Heading numbering
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanlanin committed May 30, 2014
1 parent 0fcea82 commit 5c2c687
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 4 deletions.
24 changes: 24 additions & 0 deletions docs/recipes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,27 @@ Use ``php://output`` as the filename.
header('Expires: 0');
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$xmlWriter->save("php://output");
Create numbered headings
------------------------

Define a numbering style and title styles, and match the two styles (with ``pStyle`` and ``numStyle``) like below.

.. code-block:: php
$phpWord->addNumberingStyle(
'hNum',
array('type' => 'multilevel', 'levels' => array(
array('pStyle' => 'Heading1', 'format' => 'decimal', 'text' => '%1'),
array('pStyle' => 'Heading2', 'format' => 'decimal', 'text' => '%1.%2'),
array('pStyle' => 'Heading3', 'format' => 'decimal', 'text' => '%1.%2.%3'),
)
)
);
$phpWord->addTitleStyle(1, array('size' => 16), array('numStyle' => 'hNum', 'numLevel' => 0));
$phpWord->addTitleStyle(2, array('size' => 14), array('numStyle' => 'hNum', 'numLevel' => 1));
$phpWord->addTitleStyle(3, array('size' => 12), array('numStyle' => 'hNum', 'numLevel' => 2));
$section->addTitle('Heading 1', 1);
$section->addTitle('Heading 2', 2);
$section->addTitle('Heading 3', 3);
23 changes: 23 additions & 0 deletions docs/src/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,29 @@ $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$xmlWriter->save("php://output");
```

## Create numbered headings

Define a numbering style and title styles, and match the two styles (with `pStyle` and `numStyle`) like below.

```php
$phpWord->addNumberingStyle(
'hNum',
array('type' => 'multilevel', 'levels' => array(
array('pStyle' => 'Heading1', 'format' => 'decimal', 'text' => '%1'),
array('pStyle' => 'Heading2', 'format' => 'decimal', 'text' => '%1.%2'),
array('pStyle' => 'Heading3', 'format' => 'decimal', 'text' => '%1.%2.%3'),
)
)
);
$phpWord->addTitleStyle(1, array('size' => 16), array('numStyle' => 'hNum', 'numLevel' => 0));
$phpWord->addTitleStyle(2, array('size' => 14), array('numStyle' => 'hNum', 'numLevel' => 1));
$phpWord->addTitleStyle(3, array('size' => 12), array('numStyle' => 'hNum', 'numLevel' => 2));

$section->addTitle('Heading 1', 1);
$section->addTitle('Heading 2', 2);
$section->addTitle('Heading 3', 3);
```

# Frequently asked questions

## Is this the same with PHPWord that I found in CodePlex?
Expand Down
21 changes: 20 additions & 1 deletion samples/Sample_14_ListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
array('format' => 'decimal', 'text' => '%1.', 'left' => 360, 'hanging' => 360, 'tabPos' => 360),
array('format' => 'upperLetter', 'text' => '%2.', 'left' => 720, 'hanging' => 360, 'tabPos' => 720),
)
)
)
);
$predefinedMultilevel = array('listType' => \PhpOffice\PhpWord\Style\ListItem::TYPE_NUMBER_NESTED);

Expand Down Expand Up @@ -66,6 +66,25 @@
$listItemRun->addText(' underlined', array('underline'=>'dash'));
$section->addTextBreak(2);

// Numbered heading

$phpWord->addNumberingStyle(
'headingNumbering',
array('type' => 'multilevel', 'levels' => array(
array('pStyle' => 'Heading1', 'format' => 'decimal', 'text' => '%1'),
array('pStyle' => 'Heading2', 'format' => 'decimal', 'text' => '%1.%2'),
array('pStyle' => 'Heading3', 'format' => 'decimal', 'text' => '%1.%2.%3'),
)
)
);
$phpWord->addTitleStyle(1, array('size' => 16), array('numStyle' => 'headingNumbering', 'numLevel' => 0));
$phpWord->addTitleStyle(2, array('size' => 14), array('numStyle' => 'headingNumbering', 'numLevel' => 1));
$phpWord->addTitleStyle(3, array('size' => 12), array('numStyle' => 'headingNumbering', 'numLevel' => 2));

$section->addTitle('Heading 1', 1);
$section->addTitle('Heading 2', 2);
$section->addTitle('Heading 3', 3);

// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);
if (!CLI) {
Expand Down
30 changes: 30 additions & 0 deletions src/PhpWord/Style/NumberingLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ class NumberingLevel extends AbstractStyle
*/
private $restart;

/**
* Related paragraph style
*
* @var string
* @link http://www.schemacentral.com/sc/ooxml/e-w_pStyle-2.html
*/
private $pStyle;

/**
* Content between numbering symbol and paragraph text
*
Expand Down Expand Up @@ -205,6 +213,28 @@ public function setRestart($value)
return $this;
}

/**
* Get related paragraph style
*
* @return string
*/
public function getPStyle()
{
return $this->pStyle;
}

/**
* Set related paragraph style
*
* @param string $value
* @return self
*/
public function setPStyle($value)
{
$this->pStyle = $value;
return $this;
}

/**
* Get suffix
*
Expand Down
60 changes: 60 additions & 0 deletions src/PhpWord/Style/Paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ class Paragraph extends AbstractStyle
*/
private $alignment;

/**
* Numbering style name
*
* @var string
*/
private $numStyle;

/**
* Numbering level
*
* @var int
*/
private $numLevel = 0;

/**
* Create new instance
*/
Expand Down Expand Up @@ -532,6 +546,52 @@ public function setSpace($value = null)
return $this;
}

/**
* Get numbering style name
*
* @return string
*/
public function getNumStyle()
{
return $this->numStyle;
}

/**
* Set numbering style name
*
* @param string $value
* @return self
*/
public function setNumStyle($value)
{
$this->numStyle = $value;

return $this;
}

/**
* Get numbering level
*
* @return int
*/
public function getNumLevel()
{
return $this->numLevel;
}

/**
* Set numbering level
*
* @param int $value
* @return self
*/
public function setNumLevel($value = 0)
{
$this->numLevel = $this->setIntVal($value, $this->numLevel);

return $this;
}

/**
* Get allow first/last line to display on a separate page setting
*
Expand Down
7 changes: 4 additions & 3 deletions src/PhpWord/Writer/Word2007/Part/Numbering.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function write()
$levels = $style->getLevels();

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

$xmlWriter->startElement('w:nsid');
$xmlWriter->writeAttribute('w:val', $this->getRandomHexNumber());
Expand All @@ -81,9 +81,9 @@ public function write()
foreach ($styles as $style) {
if ($style instanceof NumberingStyle) {
$xmlWriter->startElement('w:num');
$xmlWriter->writeAttribute('w:numId', $style->getNumId());
$xmlWriter->writeAttribute('w:numId', $style->getIndex());
$xmlWriter->startElement('w:abstractNumId');
$xmlWriter->writeAttribute('w:val', $style->getNumId());
$xmlWriter->writeAttribute('w:val', $style->getIndex());
$xmlWriter->endElement(); // w:abstractNumId
$xmlWriter->endElement(); // w:num
}
Expand All @@ -107,6 +107,7 @@ private function writeLevel(XMLWriter $xmlWriter, NumberingLevel $level)
'start' => 'start',
'format' => 'numFmt',
'restart' => 'lvlRestart',
'pStyle' => 'pStyle',
'suffix' => 'suff',
'text' => 'lvlText',
'align' => 'lvlJc'
Expand Down
19 changes: 19 additions & 0 deletions src/PhpWord/Writer/Word2007/Style/Paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace PhpOffice\PhpWord\Writer\Word2007\Style;

use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\Alignment as AlignmentStyle;

/**
Expand Down Expand Up @@ -117,6 +118,24 @@ private function writeStyle()
$xmlWriter->endElement();
}

// Numbering
$numStyleName = $style->getNumStyle();
$numStyleObject = Style::getStyle($numStyleName);
if ($numStyleName !== null && $numStyleObject !== null) {
$xmlWriter->startElement('w:numPr');
$xmlWriter->startElement('w:numId');
$xmlWriter->writeAttribute('w:val', $numStyleObject->getIndex());
$xmlWriter->endElement(); // w:numId
$xmlWriter->startElement('w:ilvl');
$xmlWriter->writeAttribute('w:val', $style->getNumLevel());
$xmlWriter->endElement(); // w:ilvl
$xmlWriter->endElement(); // w:numPr

$xmlWriter->startElement('w:outlineLvl');
$xmlWriter->writeAttribute('w:val', $style->getNumLevel());
$xmlWriter->endElement(); // w:outlineLvl
}

if (!$this->withoutPPR) {
$xmlWriter->endElement(); // w:pPr
}
Expand Down

0 comments on commit 5c2c687

Please sign in to comment.