Welcome to the Add watermarks to PDF guide. This guide will teach you how add watermarks to PDF files easily.
Remember that all Tool Guides takes into account that you have previous knowledge about the Basics to process a PDF with our REST API but if not, we strongly recommend you to read the related guide before start with this one.
Basic Add watermarks to PDF
You can add a watermark to your PDF files as shown in this code:
The watermark tool is composed by 2 different functions, one for add text watermarks addText('myWatermarkText') and another to add image based watermarks setImage('path/to/watermark_filename.png') which is limited to PNG and JPG format files no bigger than 1Mb. There is also a third function, addWatermark(), which is the same than addWatermarkText(), adding text by default. From now we will refer to this function.
Both functions has a list of default settings which can be customized as you will see later:
- The watermark will be stamped to all pages
- The transparency of the watermark is set by default at 100, that is no transparency, full opacity
- The watermark will be stamped over the PDF content
- Its vertical position will be middle page and its horizontal position will be centered
- There won't be rotation of the watermark with a rotation angle of 0 degrees
- The default font used for numbers will be Arial Unicode with a 14p size
- Their color will be #000000, Black.
ll this settings and more can be customized with advanced options of addWatermark().
Advanced watermark to PDF
Pages to add a watermark
setPages() has plenty of parameters to set which pages has to be stamped by a watermark. See them:
- 'all' Means that all pages will be stamped with the watermark. This is the default value
- '3-end' Means that pages from third to the last will be stamped
- '1,3,4-9' Means that pages 1 and 3 and in range of 4 to 9, will be stamped with the watermark
- '-2-end' Means that 2 pages less since the last will be stamped with the watermark
- '3-234' Means that the range of 3 to 234 pages will be stamped with the watermark
Setting watermark opacity
opacity(50) will set the opacity intensity of our watermark to 50% even if it’s a text (as it is on the code example) or an image file.
Setting watermark layer depth
setLayer('below') will place your watermark below the PDF content. It is set to setLayer('above') by default.
Vertical position of watermark
setVerticalPosition() let you set the vertical position of your watermark at the following places:
- 'bottom' places the watermark at the bottom end of the page with a default margin.
- 'top' places the watermark at the top end of the page with a default margin.
- 'middle' places the watermark at the middle of the page.
Horizontal position of watermark
setHorizontalPosition() let you set the vertical position of your watermark at the following places:
- 'left' places the watermark at the left side of the page with a default margin.
- 'right' places the watermark at the right side of the page with a default margin.
- 'center' places the watermark at the center of the page.
setMosaic(true) will stamp a 3x3 matrix mosaic of your watermark.
setRotation(180) will stamp your watermark rotated by 30 degrees.
Set a watermark font format
Note that this function only works with text based functions of watermark as setText('iLovePDF') or addWatermarkText('iLovePDF'). Trying to customize a font on a setImage() process will get an error.
setFontFamily(), setFontSize() and setFontColor() are functions to customize setText() font format. With setFontFamily() can change which font will be used, with setFontSize() change the font size and with setFontColor(), change the color of the watermark text.