This guide demonstrates how to use the watermark API to add text and image watermarks to PDFs.

Multiple watermarks

The example below shows how to add multiple watermarks to a document. The image watermark will be placed in the center of the screen, while the text will be positioned in the bottom-left corner. The text watermark supports customization of opacity and font attributes:

Terminal window
curl -X POST https://api.nutrient.io/build \
-H "Authorization: Bearer your_api_key_here" \
-o result.pdf \
--fail \
-F document=@document.pdf \
-F logo=@logo.png \
-F instructions='{
"parts": [
{
"file": "document"
}
],
"actions": [
{
"type": "watermark",
"image": "logo",
"width": "50%"
},
{
"type": "watermark",
"text": "Property of Nutrient",
"width": 250,
"height": 200,
"left": 0,
"bottom": "100%",
"opacity": 0.5,
"rotation": 10,
"fontSize": 50,
"fontColor": "#FF0000",
"fontStyle": [
"italic",
"bold"
],
"fontFamily": "Helvetica"
}
]
}'

Single-page watermark

The example below shows how to watermark the last page of a document. Declare a part consisting of all pages of the source document except the last one, and a part with the last page and an image watermark action:

Terminal window
curl -X POST https://api.nutrient.io/build \
-H "Authorization: Bearer your_api_key_here" \
-o result.pdf \
--fail \
-F document=@document.pdf \
-F logo=@logo.png \
-F instructions='{
"parts": [
{
"file": "document",
"pages": {
"end": -2
}
},
{
"file": "document",
"pages": {
"start": -1
},
"actions": [
{
"type": "watermark",
"image": "logo",
"width": "50%"
}
]
}
]
}'

Aligning watermarks

The example below demonstrates watermark alignments by adding a text watermark in each of the four corners:

Terminal window
curl -X POST https://api.nutrient.io/build \
-H "Authorization: Bearer your_api_key_here" \
-o result.pdf \
--fail \
-F document=@document.pdf \
-F instructions='{
"parts": [
{
"file": "document"
}
],
"actions": [
{
"type": "watermark",
"text": "Top Left",
"width": 150,
"height": 20,
"left": 0,
"top": 0
},
{
"type": "watermark",
"text": "Top Center",
"width": 150,
"height": 20,
"top": 0
},
{
"type": "watermark",
"text": "Top Right",
"width": 150,
"height": 20,
"right": "100%",
"top": 0
},
{
"type": "watermark",
"text": "Center Left",
"width": 150,
"height": 20,
"left": 0
},
{
"type": "watermark",
"text": "Center",
"width": 150,
"height": 20
},
{
"type": "watermark",
"text": "Center Right",
"width": 150,
"height": 20,
"right": "100%"
},
{
"type": "watermark",
"text": "Bottom Left",
"width": 150,
"height": 20,
"left": 0,
"bottom": "100%"
},
{
"type": "watermark",
"text": "Bottom Center",
"width": 150,
"height": 20,
"bottom": "100%"
},
{
"type": "watermark",
"text": "Bottom Right",
"width": 150,
"height": 20,
"right": "100%",
"bottom": "100%"
}
]
}'

Reference

The watermark action schema supports the following options and valid inputs:

TypeScript
// The size of the watermark can either be a positive integer or a percentage (e.g. representing a ratio of the page dimension).
type Dimension = number | string;
// The position of the watermark can either be a non-negative integer or a percentage (e.g. representing a ratio of the page dimension).
type Position = number | string;
// Represents one part that was sent in the multipart request. Should be the
// `name` that was specified for the part.
type MultipartReference = string;
type WatermarkAction = {
type: "watermark",
// Only either "text" or "image" can be specified.
text?: string, // When specified, this is the text that the watermark will contain.
image?: MultipartReference, // When specified, this is the image that will be used as the watermark.
// For images, one dimension needs to be specified. For text watermarks, both dimensions need to be specified.
width?: Dimension,
height?: Dimension,
// The position defaults to the center of the page.
// You can only specify one position per axis, so either:
// top or bottom
// and either
// left or right.
top?: Position,
right?: Position,
bottom?: Position,
left?: Position,
// The value of opacity should be between 0 and 1.
opacity?: float,
// Angle of rotation in degrees. Should be between 0 and 360.
rotation?: integer,
// Font and styling (only valid for text watermarks).
fontSize?: integer,
fontColor?: string, // Font color as hex code. Example: "#FF0000".
fontStyle?: string[], // Either ["italic", "bold"], or any one of them.
fontFamily?: string, // String describing the font family.
};