diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bcc5a2f..8878e3db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r - Table: Ability to add table inside a cell (nested table) - @ivanlanin GH-149 - RTF: UTF8 support for RTF: Internal UTF8 text is converted to Unicode before writing - @ivanlanin GH-158 - Table: Ability to define table width (in percent and twip) and position - @ivanlanin GH-237 +- RTF: Ability to add links and page breaks in RTF - @ivanlanin GH-196 ### Bugfixes diff --git a/docs/intro.rst b/docs/intro.rst index 25a50205..0b0c1dfe 100644 --- a/docs/intro.rst +++ b/docs/intro.rst @@ -73,13 +73,13 @@ Writers +---------------------------+----------------------+--------+-------+-------+--------+-------+ | | Title | ✓ | | | ✓ | ✓ | +---------------------------+----------------------+--------+-------+-------+--------+-------+ -| | Link | ✓ | ✓ | | ✓ | ✓ | +| | Link | ✓ | ✓ | ✓ | ✓ | ✓ | +---------------------------+----------------------+--------+-------+-------+--------+-------+ | | Preserve Text | ✓ | | | | | +---------------------------+----------------------+--------+-------+-------+--------+-------+ | | Text Break | ✓ | ✓ | ✓ | ✓ | ✓ | +---------------------------+----------------------+--------+-------+-------+--------+-------+ -| | Page Break | ✓ | | | | | +| | Page Break | ✓ | | ✓ | | | +---------------------------+----------------------+--------+-------+-------+--------+-------+ | | List | ✓ | | | | | +---------------------------+----------------------+--------+-------+-------+--------+-------+ diff --git a/docs/src/documentation.md b/docs/src/documentation.md index a140c5ba..5b969687 100644 --- a/docs/src/documentation.md +++ b/docs/src/documentation.md @@ -84,10 +84,10 @@ Below are the supported features for each file formats. | **Element Type** | Text | ✓ | ✓ | ✓ | ✓ | ✓ | | | Text Run | ✓ | ✓ | ✓ | ✓ | ✓ | | | Title | ✓ | | | ✓ | ✓ | -| | Link | ✓ | ✓ | | ✓ | ✓ | +| | Link | ✓ | ✓ | ✓ | ✓ | ✓ | | | Preserve Text | ✓ | | | | | | | Text Break | ✓ | ✓ | ✓ | ✓ | ✓ | -| | Page Break | ✓ | | | | | +| | Page Break | ✓ | | ✓ | | | | | List | ✓ | | | | | | | Table | ✓ | ✓ | | ✓ | ✓ | | | Image | ✓ | ✓ | | ✓ | | diff --git a/samples/Sample_01_SimpleText.php b/samples/Sample_01_SimpleText.php index 7202ed7e..311f3350 100644 --- a/samples/Sample_01_SimpleText.php +++ b/samples/Sample_01_SimpleText.php @@ -22,7 +22,8 @@ $section->addTextBreak(2); $section->addText('I am styled by a font style definition.', 'rStyle'); $section->addText('I am styled by a paragraph style definition.', null, 'pStyle'); $section->addText('I am styled by both font and paragraph style.', 'rStyle', 'pStyle'); -$section->addTextBreak(); + +$section->addPageBreak(); // Inline font style $fontStyle['name'] = 'Times New Roman'; @@ -36,10 +37,11 @@ $fontStyle['color'] = 'FF0000'; $fontStyle['fgColor'] = 'yellow'; $fontStyle['smallCaps'] = true; $section->addText('I am inline styled.', $fontStyle); + $section->addTextBreak(); // Link -$section->addLink('http://www.google.com', null, 'NLink'); +$section->addLink('http://www.google.com', 'Google'); $section->addTextBreak(); // Image diff --git a/src/PhpWord/Writer/RTF/Element/Link.php b/src/PhpWord/Writer/RTF/Element/Link.php new file mode 100644 index 00000000..bb87e291 --- /dev/null +++ b/src/PhpWord/Writer/RTF/Element/Link.php @@ -0,0 +1,54 @@ +element; + if (!$element instanceof \PhpOffice\PhpWord\Element\Link) { + return; + } + + $content = ''; + if (!$this->withoutP) { + $content .= '{'; + } + $content .= '{\field {\*\fldinst {HYPERLINK "' . $element->getTarget() . '"}}{\\fldrslt {'; + $content .= String::toUnicode($element->getText()); + $content .= '}}}'; + if (!$this->withoutP) { + $content .= '}'; + } + + return $content; + } +} diff --git a/src/PhpWord/Writer/RTF/Element/PageBreak.php b/src/PhpWord/Writer/RTF/Element/PageBreak.php new file mode 100644 index 00000000..c8d16e06 --- /dev/null +++ b/src/PhpWord/Writer/RTF/Element/PageBreak.php @@ -0,0 +1,36 @@ +element->getText()); $content .= '\par' . PHP_EOL; diff --git a/tests/PhpWord/Tests/Writer/RTF/ElementTest.php b/tests/PhpWord/Tests/Writer/RTF/ElementTest.php index 9f597c15..b63c8a5e 100644 --- a/tests/PhpWord/Tests/Writer/RTF/ElementTest.php +++ b/tests/PhpWord/Tests/Writer/RTF/ElementTest.php @@ -28,7 +28,7 @@ class ElementTest extends \PHPUnit_Framework_TestCase */ public function testUnmatchedElements() { - $styles = array('Container', 'Text', 'Title'); + $styles = array('Container', 'Text', 'Title', 'Link'); foreach ($styles as $style) { $objectClass = 'PhpOffice\\PhpWord\\Writer\\RTF\\Element\\' . $style; $parentWriter = new RTF();