Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support comments in docx documents #1059

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d52d9b8
Add support for XE and INDEX fields
troosan Oct 27, 2016
0953065
improve code coverage
troosan Oct 27, 2016
e7229fc
make FontStyle based on paragraph if it set
troosan Oct 28, 2016
77ed156
make FontStyle based on paragraph if it set
troosan Oct 28, 2016
8318b41
Merge remote-tracking branch 'github_troosan/support_for_xe_and_index…
troosan Oct 28, 2016
2216bc6
Merge branch 'fix_font_style_based_on' into develop
troosan Oct 28, 2016
18474c5
replace tab with spaces
troosan Oct 28, 2016
c50e3c5
replace tab with spaces
troosan Oct 28, 2016
1c04446
Merge branch 'fix_font_style_based_on' into develop
troosan Oct 28, 2016
46eebe2
add support for Image creation from string image data
troosan Nov 8, 2016
a070628
Merge remote-tracking branch 'origin/develop' into develop
troosan Nov 8, 2016
38a4e79
Merge branch 'image_from_string' into develop
troosan Dec 14, 2016
b6a9f7c
fix paper size and add tests for Paper class
troosan Jan 21, 2017
da60645
Merge remote-tracking branch 'github_troosan/paper_size_computation' …
troosan Jan 21, 2017
fc3bc29
fix default page size
troosan Jan 22, 2017
8d77755
Merge remote-tracking branch 'github_troosan/paper_size_computation' …
troosan Jan 22, 2017
d23409f
Add possibility to show/hide spelling and grammatical errors
troosan Jan 28, 2017
91f79d8
Merge branch 'show_hide_spelling_grammatical_errors' into develop
troosan Jan 28, 2017
517c432
fix image loading over https
troosan Jan 29, 2017
217fd6e
fix image loading over https
troosan Jan 29, 2017
7c08dd5
Merge remote-tracking branch 'github_troosan/fix_image_loading_over_h…
troosan Jan 29, 2017
aef7a0b
add possibility to write w:evenAndOddHeaders in settings.xml
troosan Jan 29, 2017
d8c5703
Merge remote-tracking branch
troosan Jan 29, 2017
759794d
fix twig dependency so build can succeed
troosan May 11, 2017
0e0817c
ignore the build directory
troosan May 11, 2017
1cfb62d
Add support for comments
troosan May 15, 2017
6e49e89
Merge branch 'support_comments' into develop
troosan May 15, 2017
09e669f
basedOn not correctly set if FontStyle is based on other FontStyle
troosan May 16, 2017
11c711b
Merge branch 'fix_font_style_based_on' into develop
troosan May 16, 2017
68dd3fd
rename attribute to match generated XML node name
troosan May 23, 2017
f21f797
fix sample
troosan May 25, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Thumbs.db
Desktop.ini
.idea
_build
/build
phpunit.xml
composer.lock
composer.phar
Expand All @@ -16,4 +17,4 @@ vendor
/.settings
phpword.ini
/.buildpath
/.project
/.project
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"require-dev": {
"phpunit/phpunit": "3.7.*",
"phpdocumentor/phpdocumentor":"2.*",
"twig/twig":"1.27",
"squizlabs/php_codesniffer": "1.*",
"phpmd/phpmd": "2.*",
"phploc/phploc": "2.*",
Expand Down
25 changes: 24 additions & 1 deletion docs/elements.rst
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,27 @@ Available line style attributes:
- ``endArrow``. End type of arrow: block, open, classic, diamond, oval.
- ``width``. Line-object width in pt.
- ``height``. Line-object height in pt.
- ``flip``. Flip the line element: true, false.
- ``flip``. Flip the line element: true, false.

Comments
---------

Comments can be added to a document by using ``addComment``.
The comment can contain formatted text. Once the comment has been added, it can be linked to any to any element.

.. code-block:: php

// first create a comment
$comment= new \PhpOffice\PhpWord\Element\Comment('Authors name', new \DateTime(), 'my_initials');
$comment->addText('Test', array('bold' => true));

// add it to the document
$phpWord->addComment($comment);

$textrun = $section->addTextRun();
$textrun->addText('This ');
$text = $textrun->addText('is');
// link the comment to the text you just created
$text->setCommentStart($comment);

If no end is set for a comment using the ``setCommentEnd``, the comment will be ended automatically at the end of the element it is started on.
13 changes: 13 additions & 0 deletions docs/general.rst
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,16 @@ points to twips.
$sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Converter::inchToTwip(.5));
// 2 cm right margin
$sectionStyle->setMarginRight(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2));

Language
--------

You can hide spelling errors:

.. code-block:: php
\PhpOffice\PhpWord\Settings::setSpellingErrorsHidden(true);

And hide grammatical errors:

.. code-block:: php
\PhpOffice\PhpWord\Settings::setGrammaticalErrorsHidden(true);
5 changes: 5 additions & 0 deletions docs/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Features
- Insert drawing shapes (arc, curve, line, polyline, rect, oval)
- Insert charts (pie, doughnut, bar, line, area, scatter, radar)
- Insert form fields (textinput, checkbox, and dropdown)
- Insert comments
- Create document from templates
- Use XSL 1.0 style sheets to transform headers, main document part, and footers of an OOXML template
- ... and many more features on progress
Expand Down Expand Up @@ -102,6 +103,8 @@ Writers
+---------------------------+----------------------+--------+-------+-------+--------+-------+
| | Endnote | ✓ | | | ✓ | |
+---------------------------+----------------------+--------+-------+-------+--------+-------+
| | Comments | ✓ | | | | |
+---------------------------+----------------------+--------+-------+-------+--------+-------+
| **Graphs** | 2D basic graphs | ✓ | | | | |
+---------------------------+----------------------+--------+-------+-------+--------+-------+
| | 2D advanced graphs | | | | | |
Expand Down Expand Up @@ -161,6 +164,8 @@ Readers
+---------------------------+----------------------+--------+-------+-------+-------+-------+
| | Endnote | ✓ | | | | |
+---------------------------+----------------------+--------+-------+-------+-------+-------+
| | Comments | | | | | |
+---------------------------+----------------------+--------+-------+-------+-------+-------+
| **Graphs** | 2D basic graphs | | | | | |
+---------------------------+----------------------+--------+-------+-------+-------+-------+
| | 2D advanced graphs | | | | | |
Expand Down
6 changes: 6 additions & 0 deletions samples/Sample_13_Images.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
$section->addText("Remote image from: {$source}");
$section->addImage($source);

// Image from string
$source = 'resources/_mars.jpg';
$fileContent = file_get_contents($source);
$section->addText("Image from string");
$section->addImage($fileContent);

//Wrapping style
$text = str_repeat('Hello World! ', 15);
$wrappingStyles = array('inline', 'behind', 'infront', 'square', 'tight');
Expand Down
12 changes: 10 additions & 2 deletions samples/Sample_27_Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@
$section->addField('DATE', array('dateformat' => 'dddd d MMMM yyyy H:mm:ss'), array('PreserveFormat'));

$section->addText('Page field:');
$section->addField('PAGE', array('format' => 'ArabicDash'));
$section->addField('PAGE', array('format' => 'Arabic'));

$section->addText('Number of pages field:');
$section->addField('NUMPAGES', array('format' => 'Arabic', 'numformat' => '0,00'), array('PreserveFormat'));
$section->addField('NUMPAGES', array('numformat' => '0,00', 'format' => 'Arabic'), array('PreserveFormat'));

$textrun = $section->addTextRun();
$textrun->addText('An index field is ');
$textrun->addField('XE', array(), array('Bold'), 'FieldValue');
$textrun->addText('here:');

$section->addText('The actual index:');
$section->addField('INDEX', array(), array('PreserveFormat'));

$textrun = $section->addTextRun(array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER));
$textrun->addText('This is the date of lunar calendar ');
Expand Down
63 changes: 63 additions & 0 deletions samples/Sample_37_Comments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
include_once 'Sample_Header.php';

// New Word Document
echo date('H:i:s') , ' Create new PhpWord object' , EOL;
$phpWord = new \PhpOffice\PhpWord\PhpWord();

// A comment
$comment = new \PhpOffice\PhpWord\Element\Comment('Authors name', new \DateTime(), 'my_initials');
$comment->addText('Test', array('bold' => true));
$phpWord->addComment($comment);

$section = $phpWord->addSection();

$textrun = $section->addTextRun();
$textrun->addText('This ');
$text = $textrun->addText('is');
$text->setCommentRangeStart($comment);
$textrun->addText(' a test');

$section->addTextBreak(2);

// Let's create a comment that we will link to a start element and an end element
$commentWithStartAndEnd = new \PhpOffice\PhpWord\Element\Comment('Foo Bar', new \DateTime(), '');
$commentWithStartAndEnd->addText('A comment with a start and an end');
$phpWord->addComment($commentWithStartAndEnd);

$textrunWithEnd = $section->addTextRun();
$textrunWithEnd->addText('This ');
$textToStartOn = $textrunWithEnd->addText('is', array('bold' => true));
$textToStartOn->setCommentRangeStart($commentWithStartAndEnd);
$textrunWithEnd->addText(' another', array('italic' => true));
$textToEndOn = $textrunWithEnd->addText(' test');
$textToEndOn->setCommentRangeEnd($commentWithStartAndEnd);

$section->addTextBreak(2);

// Let's add a comment on an image
$commentOnImage = new \PhpOffice\PhpWord\Element\Comment('Mr Smart', new \DateTime(), '');
$imageComment = $commentOnImage->addTextRun();
$imageComment->addText('Hey, Mars does look ');
$imageComment->addText('red', array('color' => 'FF0000'));
$phpWord->addComment($commentOnImage);
$image = $section->addImage('resources/_mars.jpg');
$image->setCommentRangeStart($commentOnImage);

$section->addTextBreak(2);

// We can also do things the other way round, link the comment to the element
$anotherText = $section->addText("another text");

$comment1 = new \PhpOffice\PhpWord\Element\Comment('Authors name', new \DateTime(), 'my_initials');
$comment1->addText('Test', array('bold' => true));
$comment1->setStartElement($anotherText);
$comment1->setEndElement($anotherText);
$phpWord->addComment($comment1);


// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);
if (!CLI) {
include_once 'Sample_Footer.php';
}
27 changes: 27 additions & 0 deletions src/PhpWord/Collection/Comments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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-2016 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpWord\Collection;

/**
* Comments collection
*
* @since 0.12.0
*/
class Comments extends AbstractCollection
{
}
9 changes: 5 additions & 4 deletions src/PhpWord/Element/AbstractContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ abstract class AbstractContainer extends AbstractElement
protected $elements = array();

/**
* Container type Section|Header|Footer|Footnote|Endnote|Cell|TextRun|TextBox|ListItemRun
* Container type Section|Header|Footer|Footnote|Endnote|Cell|TextRun|TextBox|ListItemRun|TrackChange
*
* @var string
*/
Expand All @@ -83,7 +83,7 @@ public function __call($function, $args)
'ListItem', 'ListItemRun', 'Table', 'Image', 'Object',
'Footnote', 'Endnote', 'CheckBox', 'TextBox', 'Field',
'Line', 'Shape', 'Title', 'TOC', 'PageBreak',
'Chart', 'FormField', 'SDT'
'Chart', 'FormField', 'SDT', 'Comment'
);
$functions = array();
foreach ($elements as $element) {
Expand Down Expand Up @@ -188,7 +188,7 @@ public function countElements()
private function checkValidity($method)
{
$generalContainers = array(
'Section', 'Header', 'Footer', 'Footnote', 'Endnote', 'Cell', 'TextRun', 'TextBox', 'ListItemRun',
'Section', 'Header', 'Footer', 'Footnote', 'Endnote', 'Cell', 'TextRun', 'TextBox', 'ListItemRun', 'TrackChange',
);

$validContainers = array(
Expand All @@ -203,7 +203,8 @@ private function checkValidity($method)
'Shape' => $generalContainers,
'FormField' => $generalContainers,
'SDT' => $generalContainers,
'TextRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
'TrackChange' => $generalContainers,
'TextRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox', 'TrackChange'),
'ListItem' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
'ListItemRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
'Table' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
Expand Down
65 changes: 64 additions & 1 deletion src/PhpWord/Element/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,26 @@ abstract class AbstractElement
protected $mediaRelation = false;

/**
* Is part of collection; true for Title, Footnote, Endnote, and Chart
* Is part of collection; true for Title, Footnote, Endnote, Chart, and Comment
*
* @var bool
*/
protected $collectionRelation = false;

/**
* The start position for the linked comment
*
* @var Comment
*/
protected $commentRangeStart;

/**
* The end position for the linked comment
*
* @var Comment
*/
protected $commentRangeEnd;

/**
* Get PhpWord
*
Expand Down Expand Up @@ -265,6 +279,55 @@ public function getNestedLevel()
return $this->nestedLevel;
}

/**
* Get comment start
*
* @return Comment
*/
public function getCommentRangeStart()
{
return $this->commentRangeStart;
}

/**
* Set comment start
*
* @param Comment $value
*/
public function setCommentRangeStart(Comment $value)
{
if ($this instanceof Comment) {
throw new \InvalidArgumentException("Cannot set a Comment on a Comment");
}
$this->commentRangeStart= $value;
$this->commentRangeStart->setStartElement($this);
}

/**
* Get comment end
*
* @return Comment
*/
public function getCommentRangeEnd()
{
return $this->commentRangeEnd;
}

/**
* Set comment end
*
* @param Comment $value
* @return void
*/
public function setCommentRangeEnd(Comment $value)
{
if ($this instanceof Comment) {
throw new \InvalidArgumentException("Cannot set a Comment on a Comment");
}
$this->commentRangeEnd= $value;
$this->commentRangeEnd->setEndElement($this);
}

/**
* Set parent container
*
Expand Down
Loading