Skip to content

Commit 5be8414

Browse files
committed
Settings: OOXML compatibility
1 parent 0ea2193 commit 5be8414

File tree

8 files changed

+167
-67
lines changed

8 files changed

+167
-67
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ This release added form fields (textinput, checkbox, and dropdown), drawing shap
1818
- Chart: Basic 2D chart (pie, doughnut, bar, line, area, scatter, radar) - @ivanlanin GH-278
1919
- Chart: 3D charts and ability to set width and height - @ivanlanin
2020
- FormField: Ability to add textinput, checkbox, and dropdown form elements - @ivanlanin GH-266
21-
- Security: Ability to define document protection (readOnly, comments, trackedChanges, forms) - @ivanlanin
21+
- Setting: Ability to define document protection (readOnly, comments, trackedChanges, forms) - @ivanlanin
22+
- Setting: Ability to remove [Compatibility Mode] text in the MS Word title bar - @ivanlanin
2223

2324
### Bugfixes
2425

docs/recipes.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,12 @@ Apply 'HeadingN' paragraph style to TextRun or Link. Sample code:
8787
8888
// Link
8989
$section->addLink('https://github.com/', 'GitHub', 'Link', 'Heading2');
90+
91+
Remove [Compatibility Mode] text in the MS Word title bar
92+
---------------------------------------------------------
93+
94+
Use the ``Metadata\Compatibility\setOoxmlVersion(n)`` method with ``n`` is the version of Office (14 = Office 2010, 15 = Office 2013).
95+
96+
.. code-block:: php
97+
98+
$phpWord->getCompatibility()->setOoxmlVersion(15);

docs/src/documentation.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,14 @@ $textrun->addLink('https://github.com/PHPOffice/PHPWord', 'PHPWord', 'Link');
10531053
$section->addLink('https://github.com/', 'GitHub', 'Link', 'Heading2');
10541054
```
10551055

1056+
## Remove [Compatibility Mode] text in the MS Word title bar
1057+
1058+
Use the `Metadata\Compatibility\setOoxmlVersion(n)` method with `n` is the version of Office (14 = Office 2010, 15 = Office 2013).
1059+
1060+
```php
1061+
$phpWord->getCompatibility()->setOoxmlVersion(15);
1062+
```
1063+
10561064
# Frequently asked questions
10571065

10581066
## Is this the same with PHPWord that I found in CodePlex?
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @link https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2014 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Metadata;
19+
20+
/**
21+
* Compatibility setting class
22+
*
23+
* @since 0.12.0
24+
* @link http://www.datypic.com/sc/ooxml/t-w_CT_Compat.html
25+
*/
26+
class Compatibility
27+
{
28+
/**
29+
* OOXML version
30+
*
31+
* 12 = 2007
32+
* 14 = 2010
33+
* 15 = 2013
34+
*
35+
* @var int
36+
* @link http://msdn.microsoft.com/en-us/library/dd909048%28v=office.12%29.aspx
37+
*/
38+
private $ooxmlVersion = 12;
39+
40+
/**
41+
* Get OOXML version
42+
*
43+
* @return int
44+
*/
45+
public function getOoxmlVersion()
46+
{
47+
return $this->ooxmlVersion;
48+
}
49+
50+
/**
51+
* Set OOXML version
52+
*
53+
* @param int $value
54+
* @return self
55+
*/
56+
public function setOoxmlVersion($value)
57+
{
58+
$this->ooxmlVersion = $value;
59+
60+
return $this;
61+
}
62+
}

src/PhpWord/PhpWord.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,19 @@ class PhpWord
8181
*/
8282
public function __construct()
8383
{
84+
// Collection
8485
$collections = array('Titles', 'Footnotes', 'Endnotes', 'Charts');
8586
foreach ($collections as $collection) {
8687
$class = 'PhpOffice\\PhpWord\\Collection\\' . $collection;
8788
$this->collections[$collection] = new $class();
8889
}
8990

90-
$metadata = 'PhpOffice\\PhpWord\\Metadata\\Protection';
91-
$this->metadata['Protection'] = new $metadata();
92-
93-
$metadata = 'PhpOffice\\PhpWord\\Metadata\\DocInfo';
94-
$this->metadata['DocInfo'] = new $metadata();
91+
// Metadata
92+
$metadata = array('DocInfo', 'Protection', 'Compatibility');
93+
foreach ($metadata as $meta) {
94+
$class = 'PhpOffice\\PhpWord\\Metadata\\' . $meta;
95+
$this->metadata[$meta] = new $class();
96+
}
9597
}
9698

9799
/**
@@ -147,9 +149,6 @@ public function __call($function, $args)
147149
if (in_array($function, $addStyle)) {
148150
return forward_static_call_array(array('PhpOffice\\PhpWord\\Style', $function), $args);
149151
}
150-
151-
// All other methods
152-
return null;
153152
}
154153

155154
/**
@@ -173,6 +172,17 @@ public function getProtection()
173172
return $this->metadata['Protection'];
174173
}
175174

175+
/**
176+
* Get compatibility
177+
*
178+
* @return \PhpOffice\PhpWord\Metadata\Compatibility
179+
* @since 0.12.0
180+
*/
181+
public function getCompatibility()
182+
{
183+
return $this->metadata['Compatibility'];
184+
}
185+
176186
/**
177187
* Get all sections
178188
*
@@ -331,7 +341,7 @@ public function getDocumentProperties()
331341
/**
332342
* Set document properties object
333343
*
334-
* @param \PhpOffice\PhpWord\Metadata\DocInfo
344+
* @param \PhpOffice\PhpWord\Metadata\DocInfo $documentProperties
335345
* @return self
336346
* @deprecated 0.12.0
337347
* @codeCoverageIgnore

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

Lines changed: 49 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,21 @@
2424
*/
2525
class Settings extends AbstractPart
2626
{
27+
/**
28+
* Settings value
29+
*
30+
* @var array
31+
*/
32+
private $settings = array();
33+
2734
/**
2835
* Write part
2936
*
3037
* @return string
3138
*/
3239
public function write()
3340
{
34-
$settings = $this->getSettings();
41+
$this->getSettings();
3542

3643
$xmlWriter = $this->getXmlWriter();
3744

@@ -45,7 +52,7 @@ public function write()
4552
$xmlWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml');
4653
$xmlWriter->writeAttribute('xmlns:w10', 'urn:schemas-microsoft-com:office:word');
4754

48-
foreach ($settings as $settingKey => $settingValue) {
55+
foreach ($this->settings as $settingKey => $settingValue) {
4956
$this->writeSetting($xmlWriter, $settingKey, $settingValue);
5057
}
5158

@@ -84,63 +91,19 @@ protected function writeSetting($xmlWriter, $settingKey, $settingValue)
8491

8592
/**
8693
* Get settings
87-
*
88-
* @return array
8994
*/
9095
private function getSettings()
9196
{
9297
// Default settings
93-
$settings = $this->getDefaultSettings();
94-
95-
// Protection
96-
$protection = $this->getParentWriter()->getPhpWord()->getProtection();
97-
if ($protection->getEditing() !== null) {
98-
$settings['w:documentProtection'] = array(
99-
'@attributes' => array(
100-
'w:enforcement' => 1,
101-
'w:edit' => $protection->getEditing(),
102-
)
103-
);
104-
}
105-
106-
return $settings;
107-
}
108-
109-
/**
110-
* Get default settings
111-
*
112-
* @return array
113-
*/
114-
private function getDefaultSettings()
115-
{
116-
return array(
98+
$this->settings = array(
11799
'w:zoom' => array('@attributes' => array('w:percent' => '100')),
118-
'w:view' => array('@attributes' => array('w:val' => 'print')),
119-
'w:embedSystemFonts' => '',
120100
'w:defaultTabStop' => array('@attributes' => array('w:val' => '708')),
121101
'w:hyphenationZone' => array('@attributes' => array('w:val' => '425')),
122-
'w:doNotHyphenateCaps' => '',
123102
'w:characterSpacingControl' => array('@attributes' => array('w:val' => 'doNotCompress')),
124-
'w:doNotValidateAgainstSchema' => '',
125-
'w:doNotDemarcateInvalidXml' => '',
126-
'w:compat' => array(
127-
'w:useNormalStyleForList' => '',
128-
'w:doNotUseIndentAsNumberingTabStop' => '',
129-
'w:useAltKinsokuLineBreakRules' => '',
130-
'w:allowSpaceOfSameStyleInTable' => '',
131-
'w:doNotSuppressIndentation' => '',
132-
'w:doNotAutofitConstrainedTables' => '',
133-
'w:autofitToFirstFixedWidthCell' => '',
134-
'w:underlineTabInNumList' => '',
135-
'w:displayHangulFixedWidth' => '',
136-
// Commented for GH-274
137-
// 'w:splitPgBreakAndParaMark' => '',
138-
'w:doNotVertAlignCellWithSp' => '',
139-
'w:doNotBreakConstrainedForcedTable' => '',
140-
'w:doNotVertAlignInTxbx' => '',
141-
'w:useAnsiKerningPairs' => '',
142-
'w:cachedColBalance' => '',
143-
),
103+
'w:themeFontLang' => array('@attributes' => array('w:val' => 'en-US')),
104+
'w:decimalSymbol' => array('@attributes' => array('w:val' => '.')),
105+
'w:listSeparator' => array('@attributes' => array('w:val' => ';')),
106+
'w:compat' => '',
144107
'm:mathPr' => array(
145108
'm:mathFont' => array('@attributes' => array('m:val' => 'Cambria Math')),
146109
'm:brkBin' => array('@attributes' => array('m:val' => 'before')),
@@ -154,8 +117,6 @@ private function getDefaultSettings()
154117
'm:intLim' => array('@attributes' => array('m:val' => 'subSup')),
155118
'm:naryLim' => array('@attributes' => array('m:val' => 'undOvr')),
156119
),
157-
'w:uiCompat97To2003' => '',
158-
'w:themeFontLang' => array('@attributes' => array('w:val' => 'de-DE')),
159120
'w:clrSchemeMapping' => array(
160121
'@attributes' => array(
161122
'w:bg1' => 'light1',
@@ -172,10 +133,41 @@ private function getDefaultSettings()
172133
'w:followedHyperlink' => 'followedHyperlink',
173134
),
174135
),
175-
'w:doNotIncludeSubdocsInStats' => '',
176-
'w:doNotAutoCompressPictures' => '',
177-
'w:decimalSymbol' => array('@attributes' => array('w:val' => ',')),
178-
'w:listSeparator' => array('@attributes' => array('w:val' => ';')),
179136
);
137+
138+
// Other settings
139+
$this->getProtection();
140+
$this->getCompatibility();
141+
}
142+
143+
/**
144+
* Get protection settings
145+
*/
146+
private function getProtection()
147+
{
148+
$protection = $this->getParentWriter()->getPhpWord()->getProtection();
149+
if ($protection->getEditing() !== null) {
150+
$this->settings['w:documentProtection'] = array(
151+
'@attributes' => array(
152+
'w:enforcement' => 1,
153+
'w:edit' => $protection->getEditing(),
154+
)
155+
);
156+
}
157+
}
158+
159+
/**
160+
* Get compatibility setting
161+
*/
162+
private function getCompatibility()
163+
{
164+
$compatibility = $this->getParentWriter()->getPhpWord()->getCompatibility();
165+
if ($compatibility->getOoxmlVersion() !== null) {
166+
$this->settings['w:compat']['w:compatSetting'] = array('@attributes' => array(
167+
'w:name' => 'compatibilityMode',
168+
'w:uri' => 'http://schemas.microsoft.com/office/word',
169+
'w:val' => $compatibility->getOoxmlVersion(),
170+
));
171+
}
180172
}
181173
}

tests/PhpWord/Tests/StyleTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ public function testStyles()
7575
}
7676

7777
/**
78+
* Test default paragraph style
79+
*
7880
* @covers ::setDefaultParagraphStyle
7981
* @test
8082
*/

tests/PhpWord/Tests/Writer/Word2007/Part/SettingsTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,20 @@ public function testDocumentProtection()
4949
$path = '/w:settings/w:documentProtection';
5050
$this->assertTrue($doc->elementExists($path, $file));
5151
}
52+
53+
/**
54+
* Test compatibility
55+
*/
56+
public function testCompatibility()
57+
{
58+
$phpWord = new PhpWord();
59+
$phpWord->getCompatibility()->setOoxmlVersion(15);
60+
61+
$doc = TestHelperDOCX::getDocument($phpWord);
62+
63+
$file = 'word/settings.xml';
64+
65+
$path = '/w:settings/w:compat/w:compatSetting';
66+
$this->assertTrue($doc->elementExists($path, $file));
67+
}
5268
}

0 commit comments

Comments
 (0)