Removing content from an existing Word document with PHP
One of the main goals of the new DocXUtilities class (available in the PRO+ and CORPORATE versions of PHPDocX since v2.5.3) is to facilitate the (re)usage of pre-existing Word documents.
One of the more frequent requests of our (potential) users is how they can automatically remove part of the content (in the server side) of an already existing Word document so it could be reused for a different purpose.
This is why we have introduced two new methods specifically designed to cover that recurrent need:
- searchAndRemove: allows to search for a particular string of text and remove all the paragraphs containing it.
- removeChapter: allows for removing a (sub)chapter with a (sub)heading containing a particular string of text.
Their use is pretty straightforward.
For example, let us imagine that we want to remove a paragraph containing the string "to be removed" (sorry for our lack of imagination). In this case the required PHP code reads:
$newDocx = new DocxUtilities();
$options = array( 'document' => true,
'endnotes' => true,
'comments' => true,
'headersAndFooters' => true,
'footnotes' => true);
$newDocx->searchAndRemove('source.docx', 'target.docx', 'to be removed', $options);
The options parameter has been set so all appearances of the searched string are taken into account. By default only the body of the text is searched for.
But one may want to remove a whole chapter whose heading reads "Tech specs". In that case we may just run the following script:
require_once 'path_to_phpdocx/classes/DocxUtilities.inc'; $newDocx = new DocxUtilities(); $newDocx->removeChapter('source.docx', 'target.docx', 'Tech specs');
These methods may result particularly useful when handling with repositories of Word documents that could not be individually manipulated as PHPDocX templates.