Skip to content

Commit 4b1a160

Browse files
committed
#196: Ability to add links and page breaks in RTF
1 parent f8f98cc commit 4b1a160

File tree

8 files changed

+101
-8
lines changed

8 files changed

+101
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r
1717
- Table: Ability to add table inside a cell (nested table) - @ivanlanin GH-149
1818
- RTF: UTF8 support for RTF: Internal UTF8 text is converted to Unicode before writing - @ivanlanin GH-158
1919
- Table: Ability to define table width (in percent and twip) and position - @ivanlanin GH-237
20+
- RTF: Ability to add links and page breaks in RTF - @ivanlanin GH-196
2021

2122
### Bugfixes
2223

docs/intro.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ Writers
7373
+---------------------------+----------------------+--------+-------+-------+--------+-------+
7474
| | Title || | |||
7575
+---------------------------+----------------------+--------+-------+-------+--------+-------+
76-
| | Link ||| |||
76+
| | Link ||| |||
7777
+---------------------------+----------------------+--------+-------+-------+--------+-------+
7878
| | Preserve Text || | | | |
7979
+---------------------------+----------------------+--------+-------+-------+--------+-------+
8080
| | Text Break ||||||
8181
+---------------------------+----------------------+--------+-------+-------+--------+-------+
82-
| | Page Break || | | | |
82+
| | Page Break || | | | |
8383
+---------------------------+----------------------+--------+-------+-------+--------+-------+
8484
| | List || | | | |
8585
+---------------------------+----------------------+--------+-------+-------+--------+-------+

docs/src/documentation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ Below are the supported features for each file formats.
8484
| **Element Type** | Text ||||||
8585
| | Text Run ||||||
8686
| | Title || | |||
87-
| | Link ||| |||
87+
| | Link ||| |||
8888
| | Preserve Text || | | | |
8989
| | Text Break ||||||
90-
| | Page Break || | | | |
90+
| | Page Break || | | | |
9191
| | List || | | | |
9292
| | Table ||| |||
9393
| | Image ||| || |

samples/Sample_01_SimpleText.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
$section->addText('I am styled by a font style definition.', 'rStyle');
2323
$section->addText('I am styled by a paragraph style definition.', null, 'pStyle');
2424
$section->addText('I am styled by both font and paragraph style.', 'rStyle', 'pStyle');
25-
$section->addTextBreak();
25+
26+
$section->addPageBreak();
2627

2728
// Inline font style
2829
$fontStyle['name'] = 'Times New Roman';
@@ -36,10 +37,11 @@
3637
$fontStyle['fgColor'] = 'yellow';
3738
$fontStyle['smallCaps'] = true;
3839
$section->addText('I am inline styled.', $fontStyle);
40+
3941
$section->addTextBreak();
4042

4143
// Link
42-
$section->addLink('http://www.google.com', null, 'NLink');
44+
$section->addLink('http://www.google.com', 'Google');
4345
$section->addTextBreak();
4446

4547
// Image
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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\Writer\RTF\Element;
19+
20+
use PhpOffice\PhpWord\Shared\String;
21+
22+
/**
23+
* Link element RTF writer
24+
*
25+
* @since 0.11.0
26+
*/
27+
class Link extends AbstractElement
28+
{
29+
/**
30+
* Write element
31+
*
32+
* @return string
33+
*/
34+
public function write()
35+
{
36+
$element = $this->element;
37+
if (!$element instanceof \PhpOffice\PhpWord\Element\Link) {
38+
return;
39+
}
40+
41+
$content = '';
42+
if (!$this->withoutP) {
43+
$content .= '{';
44+
}
45+
$content .= '{\field {\*\fldinst {HYPERLINK "' . $element->getTarget() . '"}}{\\fldrslt {';
46+
$content .= String::toUnicode($element->getText());
47+
$content .= '}}}';
48+
if (!$this->withoutP) {
49+
$content .= '}';
50+
}
51+
52+
return $content;
53+
}
54+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\Writer\RTF\Element;
19+
20+
/**
21+
* PageBreak element RTF writer
22+
*
23+
* @since 0.11.0
24+
*/
25+
class PageBreak extends AbstractElement
26+
{
27+
/**
28+
* Write element
29+
*
30+
* @return string
31+
*/
32+
public function write()
33+
{
34+
return '\page' . PHP_EOL;
35+
}
36+
}

src/PhpWord/Writer/RTF/Element/Title.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function write()
3939

4040
$content = '';
4141

42-
$content .= '\pard\nowidctlpar';
42+
$content .= '\pard\nowidctlpar ';
4343
$content .= String::toUnicode($this->element->getText());
4444
$content .= '\par' . PHP_EOL;
4545

tests/PhpWord/Tests/Writer/RTF/ElementTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ElementTest extends \PHPUnit_Framework_TestCase
2828
*/
2929
public function testUnmatchedElements()
3030
{
31-
$styles = array('Container', 'Text', 'Title');
31+
$styles = array('Container', 'Text', 'Title', 'Link');
3232
foreach ($styles as $style) {
3333
$objectClass = 'PhpOffice\\PhpWord\\Writer\\RTF\\Element\\' . $style;
3434
$parentWriter = new RTF();

0 commit comments

Comments
 (0)