The removeContent method of Docxpresso allows you to remove unwanted content from a template.

It may well happen that the template you use may contain elements that should be removed upon certain situations, for example a clause of a legal document when certain conditions do not apply.

It may be simpler for you to work with a single template and remove some of its components with the help of the removeContent method rather that working with several templates that match all possible situations.

The public API of the removeContent method can be summarized as follows:

Signature

public removeContent ([$options])

Parameters

  • $options (type: array). This array has the following available keys and values:
    • needle (type: string). The text to be searched to select the content to be removed or the bookmark name if the element option is set to be equal to bookmark.
    • element (type: string, default: text). It sets the type of element to be removed, possible values are:
      • text: the text given in the needle is removed from the document,
      • paragraph: the paragraphs that contain the needle text are removed,
      • list: the lists that contain the needle text are removed,
      • list-item: the list items that contain the needle text are removed,
      • table: the table that contains the needle text is removed unless the text fall within a subtable (then only the subtable is removed),
      • table-row: the same that for table but just for a table row,
      • image: the images which title include the needle text are removed,
      • chart: the charts which title include the given text are removed,
      • section: all section content of a section that contains the needle text is removed,
      • textbox: all textbox content of a textbox that contains the needle text is removed,
      • heading: all the contents that fall under a given heading are removed whenever the heading contains the needle text.
      • bookmark: all the bookmarked elements will be removed. Warning: only elements at the same document depth are selected.
    • heading-level (type: integer, default: 1). It only applies if 'heading' is the chosen element value.
    • match (type: mixed, default: all). If an integer (defining its order of appareance) only that match will be removed if 'all'(default value)all the matches will be removed.
    • container (type: boolean, default: false). If true (default is false) the container paragraph (if any) will be removed in the case of charts, images and textboxes.
    • target (type: string, default: document). Restricts the scope of search. The possible values are: document, header or footer.

The element option allows us to carefully choose the chunk of content that should be removed from the template. If the heading option is chosen all content that will naturally fall within it in an ordinary table of contents will be removed.

A few illustrative examples

Let us just remove some general content from this template: text, paragraphs and a table row:

<?php
/**
 * This sample script removes some general content from the template
 */
require_once 'pathToDOCXPRESSO/CreateDocument.inc';
$doc = new Docxpresso\CreateDocument(array('template' => 'template_remove_general.odt'));
$format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf
//remove plain text
$doc->removeContent(array('needle' => 'remove me', 'element' => 'text'));
//remove the third paragraph
$doc->removeContent(array('needle' => '', 'element' => 'paragraph', 'match' => 3));
//remove a table row
$doc->removeContent(array('needle' => 'one', 'element' => 'table-row'));
//include in the render method the path where you want your document to be saved
$doc->render('remove_general_content' . $format); 
//echo a link to the generated document
echo 'You may download the generated document from the link below:<br/>';
echo '<a href="' . 'remove_general_content' . $format . '">Download document</a>';

DOWNLOAD:download pdfdownload docdownload docxdownload odtdownload rtf

Now we can just remove some bookmarked content from this template:

<?php
/**
 * This sample script removes some bookmarked content
 */
require_once 'pathToDOCXPRESSO/CreateDocument.inc';
$doc = new Docxpresso\CreateDocument(array('template' => 'template_remove_bookmark.odt')));
$format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf
//remove bookmarked text
$doc->removeContent(array('needle' => 'html52pdf', 'element' => 'bookmark'));
//include in the render method the path where you want your document to be saved
$doc->render('remove_bookmarked' . $format); 
//echo a link to the generated document
echo 'You may download the generated document from the link below:<br/>';
echo '<a href="' . 'remove_bookmarked' . $format . '">Download document</a>';

DOWNLOAD: download pdfdownload docdownload docxdownload odtdownload rtf

We finish by removing all the content that falls within a heading scope from this template:

<?php
/**
 * This sample script removes all the content associated with a heading
 */
require_once 'pathToDOCXPRESSO/CreateDocument.inc';
$doc = new Docxpresso\CreateDocument(array('template' => 'template_remove_heading.odt')));
$format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf
//remove heading
$doc->removeContent(array('needle' => 'Remove', 'element' => 'heading'));
//include in the render method the path where you want your document to be saved
$doc->render('remove_heading' . $format); 
//echo a link to the generated document
echo 'You may download the generated document from the link below:<br/>';
echo '<a href="' . 'remove_heading' . $format . '">Download document</a>';

DOWNLOAD:download pdfdownload docdownload docxdownload odtdownload rtf