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.
Recent Posts
-
March 4, 2013We are happy to announce the release of PHPDocX v3.2. This new version includes some important changes that greatly improve the PHPDocX...
-
February 6, 2013Since v3.0 we have included the notion of Word (or WordML) fragments to simplify the process of creating sophisticated Word documents from scratch...
-
January 31, 2013We are happy to announce the release of PHPDocX v3.1 This new version includes quite a few new features that you may find interesting: It is now...
-
January 8, 2013We have just released PHPDocX v3.0. This new version includes substantial changes that have required that this new version were not fully backwards...
-
December 21, 2012Although one can easily introduce real checkboxes in a Word document generated by PHPDocX via the embedHTML method (just include the corresponding...
PHPDocx. Dinamic generation of reports in .docx format 