Add watermarks to PDFs
Watermarking embeds visual elements into a document’s pages — usually to mark ownership, add a disclaimer, or discourage redistribution. A watermark can be transparent, opaque, or invisible. Flattening it into the page content makes it a permanent part of the document.
Document Engine enables you to watermark documents using the watermark action.
Requirements
- Ensure Document Engine is up and running.
- Send a multipart POST request(opens in a new tab) with instructions to Document Engine’s
/api/buildendpoint.
For more information, refer to the API reference to learn about the /api/build endpoint and all the actions you can perform on PDFs with Document Engine.
For an overview of multipart requests, refer to the brief tour of multipart requests blog post.
Add a watermark
Set up the watermark action with the required options, which define where the watermark sits and how it looks. The options cover both text and image watermarks, and they hide the lower-level annotation details.
The action creates and flattens the watermark annotations itself, so there are no annotations to manage by hand.
For more control, create the annotations per page with the PDF Annotations API and flatten them with the Flatten PDF API.
Watermarking a file on disk
To add a TOP SECRET text watermark, send a request to the /api/build endpoint. Attach the document file and put the watermark instructions in the instructions JSON payload:
curl -X POST http://localhost:5000/api/build \ -H "Authorization: Token token=<API token>" \ -F document=@/path/to/example-document.pdf \ -F instructions='{ "parts": [ { "file": "document", "actions": [ { "type": "watermark", "text": "TOP SECRET", "width": 100, "height": 200 }, { "type": "flatten" } ] } ]}' \ -o result.pdfPOST /api/build HTTP/1.1Content-Type: multipart/form-data; boundary=customboundaryAuthorization: Token token=<API token>
--customboundaryContent-Disposition: form-data; name="document"; filename="example-document.pdf"Content-Type: application/pdf
<PDF data>--customboundaryContent-Disposition: form-data; name="instructions"Content-Type: application/json
{ "parts": [ { "file": "document", "actions": [ { "type": "watermark", "text": "TOP SECRET", "width": 100, "height": 200 }, { "type": "flatten" } ] } ]}--customboundary--The next example works on a four-page document. It puts an image watermark on the first page and a text annotation on the last. Flattening both into the output PDF means the watermark can’t be erased:
curl -X POST http://localhost:5000/api/build \ -H "Authorization: Token token=<API token>" \ -F document=@/path/to/example-document.pdf \ -F image-local=@/path/to/image-watermark.png \ -F instructions='{ "parts": [ { "file": "document", "pages": { "start": 0, "end": 0 }, "actions": { "type": "watermark", "image": "image-local", "width": 100 } }, { "file": "document", "pages": { "start": 1, "end": 2 } }, { "file": "document", "pages": { "start": 3, "end": 3 }, "actions": { "type": "watermark", "text": "TOP SECRET", "width": 100, "height": 200 } } ], "actions": [ { "type": "flatten" } ]}' \ -o result.pdfPOST /api/build HTTP/1.1Content-Type: multipart/form-data; boundary=customboundaryAuthorization: Token token=<API token>
--customboundaryContent-Disposition: form-data; name="document"; filename="example-document.pdf"Content-Type: application/pdf
<PDF data>--customboundaryContent-Disposition: form-data; name="image-local"; filename="image-watermark.png"Content-Type: application/pdf
<PDF data>--customboundaryContent-Disposition: form-data; name="instructions"Content-Type: application/json
{ "parts": [ { "file": "document", "pages": { "start": 0, "end": 0 }, "actions": { "type": "watermark", "image": "image-local", "width": 100 } }, { "file": "document", "pages": { "start": 1, "end": 2 } }, { "file": "document", "pages": { "start": 3, "end": 3 }, "actions": { "type": "watermark", "text": "TOP SECRET", "width": 100, "height": 200 } } ], "actions": [ { "type": "flatten" } ]}--customboundary--Watermarking a file from URL
URLs work anywhere a local file path does — for the documents in the file parts and for the images used as watermarks.
The next example adds an image annotation to every page and flattens it, so the watermark can’t be erased. The request to the /api/build endpoint attaches two URLs: one for the input file, one for the image.
curl -X POST http://localhost:5000/api/build \ -H "Authorization: Token token=<API token>" \ -F instructions='{ "parts": [ { "file": { "url": "https://pspdfkit.com/downloads/examples/paper.pdf" }, "pages": { "start": 0, "end": 0 }, "actions": { "type": "watermark", "image": { "url": "https://image-url.com/path-to-image-on-internet.png" }, "width": 100 } } ], "actions": [ { "type": "flatten" } ]}' \ -o result.pdfPOST /api/build HTTP/1.1Content-Type: multipart/form-data; boundary=customboundaryAuthorization: Token token=<API token>
--customboundaryContent-Disposition: form-data; name="instructions"Content-Type: application/json
{ "parts": [ { "file": { "url": "https://pspdfkit.com/downloads/examples/paper.pdf" }, "pages": { "start": 0, "end": 0 }, "actions": { "type": "watermark", "image": { "url": "https://image-url.com/path-to-image-on-internet.png" }, "width": 100 } } ], "actions": [ { "type": "flatten" } ]}--customboundary--A URL for an image annotation has to carry the image’s MIME type in the URL itself. Above, that MIME type is png.
Exporting watermarks added to a PDF document
A GET request to Document Engine’s /api/documents/:document_id/document.json endpoint exports a document’s current annotations, watermarks included, as an Instant JSON file. For more information, refer to the import and export Instant JSON PDF annotation data guide.