diff --git a/CHANGELOG.md b/CHANGELOG.md index 40805cb8..22f22dd9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,7 +18,8 @@ This release added form fields (textinput, checkbox, and dropdown), drawing shap - Chart: Basic 2D chart (pie, doughnut, bar, line, area, scatter, radar) - @ivanlanin GH-278 - Chart: 3D charts and ability to set width and height - @ivanlanin - FormField: Ability to add textinput, checkbox, and dropdown form elements - @ivanlanin GH-266 -- Security: Ability to define document protection (readOnly, comments, trackedChanges, forms) - @ivanlanin +- Setting: Ability to define document protection (readOnly, comments, trackedChanges, forms) - @ivanlanin +- Setting: Ability to remove [Compatibility Mode] text in the MS Word title bar - @ivanlanin ### Bugfixes diff --git a/docs/recipes.rst b/docs/recipes.rst index 1b529d7b..0be6b4e0 100644 --- a/docs/recipes.rst +++ b/docs/recipes.rst @@ -87,3 +87,12 @@ Apply 'HeadingN' paragraph style to TextRun or Link. Sample code: // Link $section->addLink('https://github.com/', 'GitHub', 'Link', 'Heading2'); + +Remove [Compatibility Mode] text in the MS Word title bar +--------------------------------------------------------- + +Use the ``Metadata\Compatibility\setOoxmlVersion(n)`` method with ``n`` is the version of Office (14 = Office 2010, 15 = Office 2013). + +.. code-block:: php + + $phpWord->getCompatibility()->setOoxmlVersion(15); diff --git a/docs/src/documentation.md b/docs/src/documentation.md index 60121d33..9c0c865b 100644 --- a/docs/src/documentation.md +++ b/docs/src/documentation.md @@ -1053,6 +1053,14 @@ $textrun->addLink('https://github.com/PHPOffice/PHPWord', 'PHPWord', 'Link'); $section->addLink('https://github.com/', 'GitHub', 'Link', 'Heading2'); ``` +## Remove [Compatibility Mode] text in the MS Word title bar + +Use the `Metadata\Compatibility\setOoxmlVersion(n)` method with `n` is the version of Office (14 = Office 2010, 15 = Office 2013). + +```php +$phpWord->getCompatibility()->setOoxmlVersion(15); +``` + # Frequently asked questions ## Is this the same with PHPWord that I found in CodePlex? diff --git a/src/PhpWord/Metadata/Compatibility.php b/src/PhpWord/Metadata/Compatibility.php new file mode 100644 index 00000000..d78b97f1 --- /dev/null +++ b/src/PhpWord/Metadata/Compatibility.php @@ -0,0 +1,62 @@ +ooxmlVersion; + } + + /** + * Set OOXML version + * + * @param int $value + * @return self + */ + public function setOoxmlVersion($value) + { + $this->ooxmlVersion = $value; + + return $this; + } +} diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index c62da82b..be808f01 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -81,17 +81,19 @@ class PhpWord */ public function __construct() { + // Collection $collections = array('Titles', 'Footnotes', 'Endnotes', 'Charts'); foreach ($collections as $collection) { $class = 'PhpOffice\\PhpWord\\Collection\\' . $collection; $this->collections[$collection] = new $class(); } - $metadata = 'PhpOffice\\PhpWord\\Metadata\\Protection'; - $this->metadata['Protection'] = new $metadata(); - - $metadata = 'PhpOffice\\PhpWord\\Metadata\\DocInfo'; - $this->metadata['DocInfo'] = new $metadata(); + // Metadata + $metadata = array('DocInfo', 'Protection', 'Compatibility'); + foreach ($metadata as $meta) { + $class = 'PhpOffice\\PhpWord\\Metadata\\' . $meta; + $this->metadata[$meta] = new $class(); + } } /** @@ -147,9 +149,6 @@ class PhpWord if (in_array($function, $addStyle)) { return forward_static_call_array(array('PhpOffice\\PhpWord\\Style', $function), $args); } - - // All other methods - return null; } /** @@ -173,6 +172,17 @@ class PhpWord return $this->metadata['Protection']; } + /** + * Get compatibility + * + * @return \PhpOffice\PhpWord\Metadata\Compatibility + * @since 0.12.0 + */ + public function getCompatibility() + { + return $this->metadata['Compatibility']; + } + /** * Get all sections * @@ -331,7 +341,7 @@ class PhpWord /** * Set document properties object * - * @param \PhpOffice\PhpWord\Metadata\DocInfo + * @param \PhpOffice\PhpWord\Metadata\DocInfo $documentProperties * @return self * @deprecated 0.12.0 * @codeCoverageIgnore diff --git a/src/PhpWord/Writer/Word2007/Part/Settings.php b/src/PhpWord/Writer/Word2007/Part/Settings.php index ac67b25d..d381d26a 100644 --- a/src/PhpWord/Writer/Word2007/Part/Settings.php +++ b/src/PhpWord/Writer/Word2007/Part/Settings.php @@ -24,6 +24,13 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; */ class Settings extends AbstractPart { + /** + * Settings value + * + * @var array + */ + private $settings = array(); + /** * Write part * @@ -31,7 +38,7 @@ class Settings extends AbstractPart */ public function write() { - $settings = $this->getSettings(); + $this->getSettings(); $xmlWriter = $this->getXmlWriter(); @@ -45,7 +52,7 @@ class Settings extends AbstractPart $xmlWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml'); $xmlWriter->writeAttribute('xmlns:w10', 'urn:schemas-microsoft-com:office:word'); - foreach ($settings as $settingKey => $settingValue) { + foreach ($this->settings as $settingKey => $settingValue) { $this->writeSetting($xmlWriter, $settingKey, $settingValue); } @@ -84,63 +91,19 @@ class Settings extends AbstractPart /** * Get settings - * - * @return array */ private function getSettings() { // Default settings - $settings = $this->getDefaultSettings(); - - // Protection - $protection = $this->getParentWriter()->getPhpWord()->getProtection(); - if ($protection->getEditing() !== null) { - $settings['w:documentProtection'] = array( - '@attributes' => array( - 'w:enforcement' => 1, - 'w:edit' => $protection->getEditing(), - ) - ); - } - - return $settings; - } - - /** - * Get default settings - * - * @return array - */ - private function getDefaultSettings() - { - return array( + $this->settings = array( 'w:zoom' => array('@attributes' => array('w:percent' => '100')), - 'w:view' => array('@attributes' => array('w:val' => 'print')), - 'w:embedSystemFonts' => '', 'w:defaultTabStop' => array('@attributes' => array('w:val' => '708')), 'w:hyphenationZone' => array('@attributes' => array('w:val' => '425')), - 'w:doNotHyphenateCaps' => '', 'w:characterSpacingControl' => array('@attributes' => array('w:val' => 'doNotCompress')), - 'w:doNotValidateAgainstSchema' => '', - 'w:doNotDemarcateInvalidXml' => '', - 'w:compat' => array( - 'w:useNormalStyleForList' => '', - 'w:doNotUseIndentAsNumberingTabStop' => '', - 'w:useAltKinsokuLineBreakRules' => '', - 'w:allowSpaceOfSameStyleInTable' => '', - 'w:doNotSuppressIndentation' => '', - 'w:doNotAutofitConstrainedTables' => '', - 'w:autofitToFirstFixedWidthCell' => '', - 'w:underlineTabInNumList' => '', - 'w:displayHangulFixedWidth' => '', - // Commented for GH-274 - // 'w:splitPgBreakAndParaMark' => '', - 'w:doNotVertAlignCellWithSp' => '', - 'w:doNotBreakConstrainedForcedTable' => '', - 'w:doNotVertAlignInTxbx' => '', - 'w:useAnsiKerningPairs' => '', - 'w:cachedColBalance' => '', - ), + 'w:themeFontLang' => array('@attributes' => array('w:val' => 'en-US')), + 'w:decimalSymbol' => array('@attributes' => array('w:val' => '.')), + 'w:listSeparator' => array('@attributes' => array('w:val' => ';')), + 'w:compat' => '', 'm:mathPr' => array( 'm:mathFont' => array('@attributes' => array('m:val' => 'Cambria Math')), 'm:brkBin' => array('@attributes' => array('m:val' => 'before')), @@ -154,8 +117,6 @@ class Settings extends AbstractPart 'm:intLim' => array('@attributes' => array('m:val' => 'subSup')), 'm:naryLim' => array('@attributes' => array('m:val' => 'undOvr')), ), - 'w:uiCompat97To2003' => '', - 'w:themeFontLang' => array('@attributes' => array('w:val' => 'de-DE')), 'w:clrSchemeMapping' => array( '@attributes' => array( 'w:bg1' => 'light1', @@ -172,10 +133,41 @@ class Settings extends AbstractPart 'w:followedHyperlink' => 'followedHyperlink', ), ), - 'w:doNotIncludeSubdocsInStats' => '', - 'w:doNotAutoCompressPictures' => '', - 'w:decimalSymbol' => array('@attributes' => array('w:val' => ',')), - 'w:listSeparator' => array('@attributes' => array('w:val' => ';')), ); + + // Other settings + $this->getProtection(); + $this->getCompatibility(); + } + + /** + * Get protection settings + */ + private function getProtection() + { + $protection = $this->getParentWriter()->getPhpWord()->getProtection(); + if ($protection->getEditing() !== null) { + $this->settings['w:documentProtection'] = array( + '@attributes' => array( + 'w:enforcement' => 1, + 'w:edit' => $protection->getEditing(), + ) + ); + } + } + + /** + * Get compatibility setting + */ + private function getCompatibility() + { + $compatibility = $this->getParentWriter()->getPhpWord()->getCompatibility(); + if ($compatibility->getOoxmlVersion() !== null) { + $this->settings['w:compat']['w:compatSetting'] = array('@attributes' => array( + 'w:name' => 'compatibilityMode', + 'w:uri' => 'http://schemas.microsoft.com/office/word', + 'w:val' => $compatibility->getOoxmlVersion(), + )); + } } } diff --git a/tests/PhpWord/Tests/StyleTest.php b/tests/PhpWord/Tests/StyleTest.php index 23af4b2e..6165e4fd 100644 --- a/tests/PhpWord/Tests/StyleTest.php +++ b/tests/PhpWord/Tests/StyleTest.php @@ -75,6 +75,8 @@ class StyleTest extends \PHPUnit_Framework_TestCase } /** + * Test default paragraph style + * * @covers ::setDefaultParagraphStyle * @test */ diff --git a/tests/PhpWord/Tests/Writer/Word2007/Part/SettingsTest.php b/tests/PhpWord/Tests/Writer/Word2007/Part/SettingsTest.php index de78c34a..7d4d1849 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/Part/SettingsTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/Part/SettingsTest.php @@ -49,4 +49,20 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $path = '/w:settings/w:documentProtection'; $this->assertTrue($doc->elementExists($path, $file)); } + + /** + * Test compatibility + */ + public function testCompatibility() + { + $phpWord = new PhpWord(); + $phpWord->getCompatibility()->setOoxmlVersion(15); + + $doc = TestHelperDOCX::getDocument($phpWord); + + $file = 'word/settings.xml'; + + $path = '/w:settings/w:compat/w:compatSetting'; + $this->assertTrue($doc->elementExists($path, $file)); + } }