Skip to content

Commit 0a4b25f

Browse files
author
Michaël Dupont
committed
Add support for whole-document FootnoteProperties
Also improves support of these properties for Endnotes.
1 parent 8fbd060 commit 0a4b25f

File tree

12 files changed

+517
-29
lines changed

12 files changed

+517
-29
lines changed

docs/elements.rst

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,17 +359,21 @@ The footnote numbering can be controlled by setting the FootnoteProperties on th
359359

360360
.. code-block:: php
361361
362-
$fp = new PhpWord\SimpleType\FootnoteProperties();
362+
$fp = new \PhpOffice\PhpWord\ComplexType\FootnoteProperties();
363363
//sets the position of the footnote (pageBottom (default), beneathText, sectEnd, docEnd)
364-
$fp->setPos(FootnoteProperties::POSITION_DOC_END);
364+
$fp->setPos(\PhpOffice\PhpWord\ComplexType\FootnoteProperties::POSITION_BENEATH_TEXT);
365365
//set the number format to use (decimal (default), upperRoman, upperLetter, ...)
366-
$fp->setNumFmt(FootnoteProperties::NUMBER_FORMAT_LOWER_ROMAN);
366+
$fp->setNumFmt(\PhpOffice\PhpWord\SimpleType\NumberFormat::LOWER_ROMAN);
367367
//force starting at other than 1
368368
$fp->setNumStart(2);
369369
//when to restart counting (continuous (default), eachSect, eachPage)
370-
$fp->setNumRestart(FootnoteProperties::RESTART_NUMBER_EACH_PAGE);
370+
$fp->setNumRestart(\PhpOffice\PhpWord\ComplexType\FootnoteProperties::RESTART_NUMBER_EACH_PAGE);
371371
//And finaly, set it on the Section
372-
$section->setFootnoteProperties($properties);
372+
$section->setFootnoteProperties($fp);
373+
374+
To set endnote properties, use the ``setEndnoteProperties`` method instead.
375+
You can also use ``$phpWord->setDefaultFootnoteProperties()`` and ``$phpWord->setDefaultEndnoteProperties()``
376+
to set the properties for all the footnotes/endnotes in the document.
373377

374378
Checkboxes
375379
----------

docs/general.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,24 @@ default font by using the following two functions:
141141
$phpWord->setDefaultFontName('Times New Roman');
142142
$phpWord->setDefaultFontSize(12);
143143
144+
Default footnote and endnote properties
145+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
146+
147+
By default, footnotes are displayed at the bottom of the page, numbered continuously from 1.
148+
Endnotes are displayed at the end of the document, numbered continuously in lower roman numerals starting from 1.
149+
150+
Default properties will apply to all footnotes and endnotes in the document, unless overriden at the section level.
151+
152+
You can change the default properties using ``$phpWord->setDefaultFootnoteProperties()`` and ``$phpWord->setDefaultEndnoteProperties()``:
153+
154+
.. code-block:: php
155+
156+
$defaultProperties = new \PhpOffice\PhpWord\ComplexType\FootnoteProperties();
157+
$defaultProperties->setPos(\PhpOffice\PhpWord\ComplexType\FootnoteProperties::POSITION_SECTION_END);
158+
$defaultProperties->setNumFmt(\PhpOffice\PhpWord\SimpleType\NumberFormat::ORDINAL);
159+
$defaultProperties->setNumStart(4);
160+
$phpWord->setDefaultEndnoteProperties($defaultProperties);
161+
144162
Document settings
145163
-----------------
146164
Settings for the generated document can be set using ``$phpWord->getSettings()``
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
use PhpOffice\PhpWord\ComplexType\FootnoteProperties;
3+
use PhpOffice\PhpWord\SimpleType\NumberFormat;
4+
5+
include_once 'Sample_Header.php';
6+
7+
// New Word Document
8+
echo date('H:i:s'), ' Create new PhpWord object', EOL;
9+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
10+
\PhpOffice\PhpWord\Settings::setCompatibility(false);
11+
12+
$defaultFp = new FootnoteProperties();
13+
$defaultFp->setPos(FootnoteProperties::POSITION_BENEATH_TEXT);
14+
$defaultFp->setNumFmt(NumberFormat::DECIMAL);
15+
$defaultFp->setNumStart(3);
16+
$defaultFp->setNumRestart(FootnoteProperties::RESTART_NUMBER_CONTINUOUS);
17+
18+
$phpWord->setDefaultFootnoteProperties($defaultFp);
19+
20+
$defaultEp = new FootnoteProperties();
21+
$defaultEp->setPos(FootnoteProperties::POSITION_SECTION_END);
22+
$defaultEp->setNumFmt(NumberFormat::DECIMAL_ENCLOSED_CIRCLE);
23+
$defaultEp->setNumStart(8);
24+
$defaultEp->setNumRestart(FootnoteProperties::RESTART_NUMBER_CONTINUOUS);
25+
26+
$phpWord->setDefaultEndnoteProperties($defaultEp);
27+
28+
// First portrait section
29+
$section = $phpWord->addSection();
30+
31+
$textrun = $section->addTextrun();
32+
$textrun->addText('This is some lead text in a paragraph with a following endnote and footnote. ');
33+
34+
// First endnote, follows default properties.
35+
$endnote = $textrun->addEndnote();
36+
$endnote->addText('First endnote, should be on the second page, prefixed by a "8" enclosed in a circle.');
37+
38+
// First footnote, follows default properties.
39+
$footnote = $textrun->addFootnote();
40+
$footnote->addText('First footnote, should be on the first page beneath the text, prefixed by a "3".');
41+
42+
// Create a blank page before the next section
43+
$section->addPageBreak();
44+
$section->addPageBreak();
45+
46+
// Second portrait section
47+
$section = $phpWord->addSection();
48+
49+
// Custom endnote properties for this section
50+
$ep = new FootnoteProperties();
51+
$ep->setPos(FootnoteProperties::POSITION_DOC_END);
52+
$ep->setNumFmt(NumberFormat::DECIMAL_ZERO);
53+
$ep->setNumStart(2);
54+
$ep->setNumRestart(FootnoteProperties::RESTART_NUMBER_EACH_SECTION);
55+
56+
// Custom footnote properties for this section
57+
$fp = new FootnoteProperties();
58+
$fp->setPos(FootnoteProperties::POSITION_PAGE_BOTTOM);
59+
$fp->setNumFmt(NumberFormat::ORDINAL_TEXT);
60+
$fp->setNumStart(6);
61+
$fp->setNumRestart(FootnoteProperties::RESTART_NUMBER_EACH_PAGE);
62+
63+
$section->setEndnoteProperties($ep);
64+
$section->setFootnoteProperties($fp);
65+
66+
$textrun = $section->addTextrun();
67+
$textrun->addText('Second section on a new page with some text followed by an endnote and a footnote. ');
68+
69+
// Second endnote, follows custom properties
70+
$endnote = $textrun->addEndnote();
71+
$endnote->addText('Second endnote, should be on the third page at the end of the section
72+
– even though we told it to be at the end of the document, endnotes only obey default positioning –,
73+
prefixed by "01" – even though we told it to start at 2, the NumRestart value (each section) resets it to 1.');
74+
75+
// Second footnote, follows custom properties
76+
$footnote = $textrun->addFootnote();
77+
$footnote->addText('Second footnote, should be at the bottom of the third page, prefixed by "First"
78+
– even though we told it to start at 6, the NumRestart value (each page), resets it to 1.');
79+
80+
// Third portrait section
81+
$section = $phpWord->addSection();
82+
83+
$textrun = $section->addTextrun();
84+
$textrun->addText('Third and last section, with an endnote and a footnote. ');
85+
86+
// Third endnote, follows default properties
87+
$endnote = $textrun->addEndnote();
88+
$endnote->addText('Third endnote, should be at the bottom of the last section, prefixed by a "10" enclosed in a circle
89+
– note that the previous endnote was counted as part of the continuation, so "10" is displayed, not "9".');
90+
91+
// Third footnote, follows default properties
92+
$footnote = $textrun->addFootnote();
93+
$footnote->addText('Third footnote, should be beneath the text on the last page, prefixed by a "5"
94+
– note that the previous endnote was counted as part of the continuation, so "5" is displayed, not "4".');
95+
96+
// Save file
97+
echo write($phpWord, basename(__FILE__, '.php'), $writers);
98+
if (!CLI) {
99+
include_once 'Sample_Footer.php';
100+
}

src/PhpWord/Element/Section.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ class Section extends AbstractContainer
5555
*/
5656
private $footnoteProperties;
5757

58+
/**
59+
* The properties for the endnote of this section
60+
*
61+
* @var FootnoteProperties
62+
*/
63+
private $endnoteProperties;
64+
5865
/**
5966
* Create new instance
6067
*
@@ -146,6 +153,18 @@ public function getFooters()
146153
*
147154
* @return FootnoteProperties
148155
*/
156+
public function getFootnoteProperties()
157+
{
158+
return $this->footnoteProperties;
159+
}
160+
161+
/**
162+
* Get the footnote properties
163+
*
164+
* @deprecated Use the `getFootnoteProperties` method instead
165+
*
166+
* @return FootnoteProperties
167+
*/
149168
public function getFootnotePropoperties()
150169
{
151170
return $this->footnoteProperties;
@@ -161,6 +180,26 @@ public function setFootnoteProperties(FootnoteProperties $footnoteProperties = n
161180
$this->footnoteProperties = $footnoteProperties;
162181
}
163182

183+
/**
184+
* Get the endnote properties
185+
*
186+
* @return FootnoteProperties
187+
*/
188+
public function getEndnoteProperties()
189+
{
190+
return $this->endnoteProperties;
191+
}
192+
193+
/**
194+
* Set the endnote properties
195+
*
196+
* @param FootnoteProperties $endnoteProperties
197+
*/
198+
public function setEndnoteProperties(FootnoteProperties $endnoteProperties = null)
199+
{
200+
$this->endnoteProperties = $endnoteProperties;
201+
}
202+
164203
/**
165204
* Is there a header for this section that is for the first page only?
166205
*

src/PhpWord/PhpWord.php

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

1818
namespace PhpOffice\PhpWord;
1919

20+
use PhpOffice\PhpWord\ComplexType\FootnoteProperties;
2021
use PhpOffice\PhpWord\Element\Section;
2122
use PhpOffice\PhpWord\Exception\Exception;
2223

@@ -317,6 +318,46 @@ public function setDefaultParagraphStyle($styles)
317318
return Style::setDefaultParagraphStyle($styles);
318319
}
319320

321+
/**
322+
* Set default Footnote properties to settings.xml
323+
*
324+
* @param FootnoteProperties $properties
325+
*/
326+
public function setDefaultFootnoteProperties(FootnoteProperties $properties)
327+
{
328+
Settings::setDefaultFootnoteProperties($properties);
329+
}
330+
331+
/**
332+
* Get default Footnote properties
333+
*
334+
* @return FootnoteProperties
335+
*/
336+
public function getDefaultFootnoteProperties()
337+
{
338+
return Settings::getDefaultFootnoteProperties();
339+
}
340+
341+
/**
342+
* Set default Endnote properties to settings.xml
343+
*
344+
* @param FootnoteProperties $properties
345+
*/
346+
public function setDefaultEndnoteProperties(FootnoteProperties $properties)
347+
{
348+
Settings::setDefaultEndnoteProperties($properties);
349+
}
350+
351+
/**
352+
* Get default Endnote properties
353+
*
354+
* @return FootnoteProperties
355+
*/
356+
public function getDefaultEndnoteProperties()
357+
{
358+
return Settings::getDefaultEndnoteProperties();
359+
}
360+
320361
/**
321362
* Load template by filename
322363
*

src/PhpWord/Settings.php

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

1818
namespace PhpOffice\PhpWord;
1919

20+
use PhpOffice\PhpWord\ComplexType\FootnoteProperties;
21+
2022
/**
2123
* PHPWord settings class
2224
*
@@ -134,6 +136,20 @@ class Settings
134136
*/
135137
private static $outputEscapingEnabled = false;
136138

139+
/**
140+
* Default properties for the Footnotes
141+
*
142+
* @var FootnoteProperties
143+
*/
144+
private static $defaultFootnoteProperties = null;
145+
146+
/**
147+
* Default properties for the Endnotes
148+
*
149+
* @var FootnoteProperties
150+
*/
151+
private static $defaultEndnoteProperties = null;
152+
137153
/**
138154
* Return the compatibility option used by the XMLWriter
139155
*
@@ -443,4 +459,44 @@ public static function getCompatibility()
443459
{
444460
return self::hasCompatibility();
445461
}
462+
463+
/**
464+
* Set default Footnote properties.
465+
*
466+
* @param FootnoteProperties $properties
467+
*/
468+
public static function setDefaultFootnoteProperties(FootnoteProperties $properties)
469+
{
470+
self::$defaultFootnoteProperties = $properties;
471+
}
472+
473+
/**
474+
* Get default Footnote properties
475+
*
476+
* @return FootnoteProperties
477+
*/
478+
public static function getDefaultFootnoteProperties()
479+
{
480+
return self::$defaultFootnoteProperties;
481+
}
482+
483+
/**
484+
* Set default Endnote properties.
485+
*
486+
* @param FootnoteProperties $properties
487+
*/
488+
public static function setDefaultEndnoteProperties(FootnoteProperties $properties)
489+
{
490+
self::$defaultEndnoteProperties = $properties;
491+
}
492+
493+
/**
494+
* Get default Endnote properties
495+
*
496+
* @return FootnoteProperties
497+
*/
498+
public static function getDefaultEndnoteProperties()
499+
{
500+
return self::$defaultEndnoteProperties;
501+
}
446502
}

0 commit comments

Comments
 (0)