Forum


Replies: 1   Views: 1687
Insert comments in existing .docx file
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by dev@ams  · 12-05-2020 - 12:58

Hi,

Is it possible to insert a comment in an existing .docx file on a specified fragment?

Posted by admin  · 12-05-2020 - 13:21

Hello,

You can generate the comment and use DOCXPath (https://www.phpdocx.com/documentation/introduction/docxpath) to insert it, for example:

<?php

require_once 'classes/CreateDocx.php';

$docx = new CreateDocxFromTemplate('SimpleExample.docx');

$comment = new WordFragment($docx, 'document');

$comment->addComment(
    array(
        'textDocument' => 'comment',
        'textComment' => 'The comment we want to insert.',
        'initials' => 'PT',
        'author' => 'PHPDocX Team',
        'date' => '10 September 2000'
    )
);
                    
$referenceNode = array(
        'type' => 'paragraph',
    'occurrence' => 2,
);

$docx->insertWordFragment($comment, $referenceNode, 'inlineAfter');

$docx->createDocx('output');

at specific positions. Please note that insertWordFragment allows working with existing tags, so it can add the comment after any MS Word tag (in body (document), header, and footer targets), but it doesn't allow applying the comment to an existing string.

Regards.