Cookbook

How to add watermarks and background images

Word allows to add watermarks and backgrounds to a page. Each of them has its own strenghts and weaknesses. The main difference between these two formats is that a background doesn't appear in PDFs nor in print copies, while the watermark does.

phpdocx supports them both.

Add a background image in documents

The addBackgroundImage method lets you place a background image that covers the whole page. If the image doesn't cover it entirely, the method repeats it as many times as necessary to fill the page.

To insert a background image, run the following code:

Insert a watermark (Advanced and Premium licenses only)

The DocxUtilities class includes the watermarkDocx method, that places a watermark with an image format to a page. The image that works as a watermark keeps its original size and doesn't repeat itself along the page like the images added using the addBackgroundImage method.

You can place a watermark with this code:

Beside images, the watermarkDocx method also allows to add texts as watermarks.

Keeping the background image when generating a PDF with the conversion plugin

If you want to keep the background image when exporting the document to a PDF, it is necessary to add it as a watermark.

Before inserting the watermark, it is advisable to adjust the alpha channel of the image to obtain a result the closest to what you see in Word, instead of the default opaque image that LibreOffice shows. To edit the opacity of an image, use the PHP extension ImageMagick (http://php.net/manual/en/book.imagick.php):

When you add the new image with the edited opacity, the watermark in LibreOffice looks very similar as it appears in MS Word.

Tip

If you need to adapt the image to cover the biggest possible space of the page, you can change the height and width parameters of the watermarkDocx method.