diff --git a/docs/changes/1.x/1.5.0.md b/docs/changes/1.x/1.5.0.md index b96865bada..f4620c201b 100644 --- a/docs/changes/1.x/1.5.0.md +++ b/docs/changes/1.x/1.5.0.md @@ -7,6 +7,7 @@ ### Bug fixes - Set writeAttribute return type by [@radarhere](https://github.com/radarhere) fixing [#2204](https://github.com/PHPOffice/PHPWord/issues/2204) in [#2776](https://github.com/PHPOffice/PHPWord/pull/2776) +- Writer RTF: Create PreserveText by [@rasamassen](https://github.com/rasamassen) in [#2826](https://github.com/PHPOffice/PHPWord/pull/2826) ### Miscellaneous @@ -16,4 +17,4 @@ ### BC Breaks -### Notes \ No newline at end of file +### Notes diff --git a/src/PhpWord/Writer/RTF/Element/PreserveText.php b/src/PhpWord/Writer/RTF/Element/PreserveText.php new file mode 100644 index 0000000000..5422f0d8fa --- /dev/null +++ b/src/PhpWord/Writer/RTF/Element/PreserveText.php @@ -0,0 +1,64 @@ +element; + if (!$element instanceof \PhpOffice\PhpWord\Element\PreserveText) { + return ''; + } + + $this->getStyles(); + + $content = ''; + $content .= $this->writeOpening(); + $content .= '{'; + $content .= $this->writeFontStyle(); + if (is_array($element->getText())) { + foreach ($element->getText() as $text) { + if (preg_match('/[{}]/', $text) == 1) { + $text = str_replace(['{', '}'], '', $text); + $content .= '{\field {\*\fldinst {' . $text . '}}{\\fldrslt {}}}'; + } else { + $content .= $this->writeText($text); + } + } + } else { + $content .= $this->writeText($element->getText()); + } + $content .= '}'; + $content .= $this->writeClosing(); + + return $content; + } +}