#196: Ability to add links and page breaks in RTF

This commit is contained in:
Ivan Lanin 2014-05-13 02:43:44 +07:00
parent f8f98cccab
commit 4b1a16006d
8 changed files with 101 additions and 8 deletions

View File

@ -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 - 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 - 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 - 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 ### Bugfixes

View File

@ -73,13 +73,13 @@ Writers
+---------------------------+----------------------+--------+-------+-------+--------+-------+ +---------------------------+----------------------+--------+-------+-------+--------+-------+
| | Title | ✓ | | | ✓ | ✓ | | | Title | ✓ | | | ✓ | ✓ |
+---------------------------+----------------------+--------+-------+-------+--------+-------+ +---------------------------+----------------------+--------+-------+-------+--------+-------+
| | Link | ✓ | ✓ | | ✓ | ✓ | | | Link | ✓ | ✓ | | ✓ | ✓ |
+---------------------------+----------------------+--------+-------+-------+--------+-------+ +---------------------------+----------------------+--------+-------+-------+--------+-------+
| | Preserve Text | ✓ | | | | | | | Preserve Text | ✓ | | | | |
+---------------------------+----------------------+--------+-------+-------+--------+-------+ +---------------------------+----------------------+--------+-------+-------+--------+-------+
| | Text Break | ✓ | ✓ | ✓ | ✓ | ✓ | | | Text Break | ✓ | ✓ | ✓ | ✓ | ✓ |
+---------------------------+----------------------+--------+-------+-------+--------+-------+ +---------------------------+----------------------+--------+-------+-------+--------+-------+
| | Page Break | ✓ | | | | | | | Page Break | ✓ | | | | |
+---------------------------+----------------------+--------+-------+-------+--------+-------+ +---------------------------+----------------------+--------+-------+-------+--------+-------+
| | List | ✓ | | | | | | | List | ✓ | | | | |
+---------------------------+----------------------+--------+-------+-------+--------+-------+ +---------------------------+----------------------+--------+-------+-------+--------+-------+

View File

@ -84,10 +84,10 @@ Below are the supported features for each file formats.
| **Element Type** | Text | ✓ | ✓ | ✓ | ✓ | ✓ | | **Element Type** | Text | ✓ | ✓ | ✓ | ✓ | ✓ |
| | Text Run | ✓ | ✓ | ✓ | ✓ | ✓ | | | Text Run | ✓ | ✓ | ✓ | ✓ | ✓ |
| | Title | ✓ | | | ✓ | ✓ | | | Title | ✓ | | | ✓ | ✓ |
| | Link | ✓ | ✓ | | ✓ | ✓ | | | Link | ✓ | ✓ | | ✓ | ✓ |
| | Preserve Text | ✓ | | | | | | | Preserve Text | ✓ | | | | |
| | Text Break | ✓ | ✓ | ✓ | ✓ | ✓ | | | Text Break | ✓ | ✓ | ✓ | ✓ | ✓ |
| | Page Break | ✓ | | | | | | | Page Break | ✓ | | | | |
| | List | ✓ | | | | | | | List | ✓ | | | | |
| | Table | ✓ | ✓ | | ✓ | ✓ | | | Table | ✓ | ✓ | | ✓ | ✓ |
| | Image | ✓ | ✓ | | ✓ | | | | Image | ✓ | ✓ | | ✓ | |

View File

@ -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 font style definition.', 'rStyle');
$section->addText('I am styled by a paragraph style definition.', null, 'pStyle'); $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->addText('I am styled by both font and paragraph style.', 'rStyle', 'pStyle');
$section->addTextBreak();
$section->addPageBreak();
// Inline font style // Inline font style
$fontStyle['name'] = 'Times New Roman'; $fontStyle['name'] = 'Times New Roman';
@ -36,10 +37,11 @@ $fontStyle['color'] = 'FF0000';
$fontStyle['fgColor'] = 'yellow'; $fontStyle['fgColor'] = 'yellow';
$fontStyle['smallCaps'] = true; $fontStyle['smallCaps'] = true;
$section->addText('I am inline styled.', $fontStyle); $section->addText('I am inline styled.', $fontStyle);
$section->addTextBreak(); $section->addTextBreak();
// Link // Link
$section->addLink('http://www.google.com', null, 'NLink'); $section->addLink('http://www.google.com', 'Google');
$section->addTextBreak(); $section->addTextBreak();
// Image // Image

View File

@ -0,0 +1,54 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\RTF\Element;
use PhpOffice\PhpWord\Shared\String;
/**
* Link element RTF writer
*
* @since 0.11.0
*/
class Link extends AbstractElement
{
/**
* Write element
*
* @return string
*/
public function write()
{
$element = $this->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;
}
}

View File

@ -0,0 +1,36 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\RTF\Element;
/**
* PageBreak element RTF writer
*
* @since 0.11.0
*/
class PageBreak extends AbstractElement
{
/**
* Write element
*
* @return string
*/
public function write()
{
return '\page' . PHP_EOL;
}
}

View File

@ -39,7 +39,7 @@ class Title extends AbstractElement
$content = ''; $content = '';
$content .= '\pard\nowidctlpar'; $content .= '\pard\nowidctlpar ';
$content .= String::toUnicode($this->element->getText()); $content .= String::toUnicode($this->element->getText());
$content .= '\par' . PHP_EOL; $content .= '\par' . PHP_EOL;

View File

@ -28,7 +28,7 @@ class ElementTest extends \PHPUnit_Framework_TestCase
*/ */
public function testUnmatchedElements() public function testUnmatchedElements()
{ {
$styles = array('Container', 'Text', 'Title'); $styles = array('Container', 'Text', 'Title', 'Link');
foreach ($styles as $style) { foreach ($styles as $style) {
$objectClass = 'PhpOffice\\PhpWord\\Writer\\RTF\\Element\\' . $style; $objectClass = 'PhpOffice\\PhpWord\\Writer\\RTF\\Element\\' . $style;
$parentWriter = new RTF(); $parentWriter = new RTF();