Examples
The following sections show more examples of how the watermark API can be used.
Multiple Watermarks
This example will add multiple watermarks to the 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's opacity and font attributes can also be customized.
curl -X POST https://api.nutrient.io/build \
-H "Authorization: Bearer your_api_key_here" \
-o result.pdf \
--fail \
-F [email protected] \
-F [email protected] \
-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"
}
]
}'
curl -X POST https://api.nutrient.io/build ^
-H "Authorization: Bearer your_api_key_here" ^
-o result.pdf ^
--fail ^
-F [email protected] ^
-F [email protected] ^
-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\"}]}"
package com.example.pspdfkit;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import org.json.JSONArray;
import org.json.JSONObject;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public final class PspdfkitApiExample {
public static void main(final String[] args) throws IOException {
final RequestBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart(
"document",
"document.pdf",
RequestBody.create(
MediaType.parse("application/pdf"),
new File("document.pdf")
)
)
.addFormDataPart(
"logo",
"logo.png",
RequestBody.create(
MediaType.parse("image/png"),
new File("logo.png")
)
)
.addFormDataPart(
"instructions",
new JSONObject()
.put("parts", new JSONArray()
.put(new JSONObject()
.put("file", "document")
)
)
.put("actions", new JSONArray()
.put(new JSONObject()
.put("type", "watermark")
.put("image", "logo")
.put("width", "50%")
)
.put(new JSONObject()
.put("type", "watermark")
.put("text", "Property of Nutrient")
.put("width", 250)
.put("height", 200)
.put("left", 0)
.put("bottom", "100%")
.put("opacity", 0.5)
.put("rotation", 10)
.put("fontSize", 50)
.put("fontColor", "#FF0000")
.put("fontStyle", new JSONArray()
.put("italic")
.put("bold")
)
.put("fontFamily", "Helvetica")
)
).toString()
)
.build();
final Request request = new Request.Builder()
.url("https://api.nutrient.io/build")
.method("POST", body)
.addHeader("Authorization", "Bearer your_api_key_here")
.build();
final OkHttpClient client = new OkHttpClient()
.newBuilder()
.build();
final Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
Files.copy(
response.body().byteStream(),
FileSystems.getDefault().getPath("result.pdf"),
StandardCopyOption.REPLACE_EXISTING
);
} else {
// Handle the error
throw new IOException(response.body().string());
}
}
}
using System;
using System.IO;
using System.Net;
using RestSharp;
namespace PspdfkitApiDemo
{
class Program
{
static void Main(string[] args)
{
var client = new RestClient("https://api.nutrient.io/build");
var request = new RestRequest(Method.POST)
.AddHeader("Authorization", "Bearer your_api_key_here")
.AddFile("document", "document.pdf")
.AddFile("logo", "logo.png")
.AddParameter("instructions", new JsonObject
{
["parts"] = new JsonArray
{
new JsonObject
{
["file"] = "document"
}
},
["actions"] = new JsonArray
{
new JsonObject
{
["type"] = "watermark",
["image"] = "logo",
["width"] = "50%"
},
new JsonObject
{
["type"] = "watermark",
["text"] = "Property of Nutrient",
["width"] = 250,
["height"] = 200,
["left"] = 0,
["bottom"] = "100%",
["opacity"] = 0.5,
["rotation"] = 10,
["fontSize"] = 50,
["fontColor"] = "#FF0000",
["fontStyle"] = new JsonArray
{
"italic",
"bold"
},
["fontFamily"] = "Helvetica"
}
}
}.ToString());
request.AdvancedResponseWriter = (responseStream, response) =>
{
if (response.StatusCode == HttpStatusCode.OK)
{
using (responseStream)
{
using var outputFileWriter = File.OpenWrite("result.pdf");
responseStream.CopyTo(outputFileWriter);
}
}
else
{
var responseStreamReader = new StreamReader(responseStream);
Console.Write(responseStreamReader.ReadToEnd());
}
};
client.Execute(request);
}
}
}
// This code requires Node.js. Do not run this code directly in a web browser.
const axios = require('axios')
const FormData = require('form-data')
const fs = require('fs')
const formData = new FormData()
formData.append('instructions', JSON.stringify({
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"
}
]
}))
formData.append('document', fs.createReadStream('document.pdf'))
formData.append('logo', fs.createReadStream('logo.png'))
;(async () => {
try {
const response = await axios.post('https://api.nutrient.io/build', formData, {
headers: formData.getHeaders({
'Authorization': 'Bearer your_api_key_here'
}),
responseType: "stream"
})
response.data.pipe(fs.createWriteStream("result.pdf"))
} catch (e) {
const errorString = await streamToString(e.response.data)
console.log(errorString)
}
})()
function streamToString(stream) {
const chunks = []
return new Promise((resolve, reject) => {
stream.on("data", (chunk) => chunks.push(Buffer.from(chunk)))
stream.on("error", (err) => reject(err))
stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")))
})
}
import requests
import json
response = requests.request(
'POST',
'https://api.nutrient.io/build',
headers = {
'Authorization': 'Bearer your_api_key_here'
},
files = {
'document': open('document.pdf', 'rb'),
'logo': open('logo.png', 'rb')
},
data = {
'instructions': json.dumps({
'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'
}
]
})
},
stream = True
)
if response.ok:
with open('result.pdf', 'wb') as fd:
for chunk in response.iter_content(chunk_size=8096):
fd.write(chunk)
else:
print(response.text)
exit()
<?php
$FileHandle = fopen('result.pdf', 'w+');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.nutrient.io/build',
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_POSTFIELDS => array(
'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"
}
]
}',
'document' => new CURLFILE('document.pdf'),
'logo' => new CURLFILE('logo.png')
),
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer your_api_key_here'
),
CURLOPT_FILE => $FileHandle,
));
$response = curl_exec($curl);
curl_close($curl);
fclose($FileHandle);
POST https://api.nutrient.io/build HTTP/1.1
Content-Type: multipart/form-data; boundary=--customboundary
Authorization: Bearer your_api_key_here
--customboundary
Content-Disposition: form-data; name="instructions"
Content-Type: application/json
{
"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"
}
]
}
--customboundary
Content-Disposition: form-data; name="document"; filename="document.pdf"
Content-Type: application/pdf
(document data)
--customboundary
Content-Disposition: form-data; name="logo"; filename="logo.png"
Content-Type: image/png
(logo data)
--customboundary--
Single-Page Watermark
This example will watermark the last page of the document. To do so, declare a part consisting of all pages of the source document but the last one, and a part with the last page and an image watermark action.
curl -X POST https://api.nutrient.io/build \
-H "Authorization: Bearer your_api_key_here" \
-o result.pdf \
--fail \
-F [email protected] \
-F [email protected] \
-F instructions='{
"parts": [
{
"file": "document",
"pages": {
"end": -2
}
},
{
"file": "document",
"pages": {
"start": -1
},
"actions": [
{
"type": "watermark",
"image": "logo",
"width": "50%"
}
]
}
]
}'
curl -X POST https://api.nutrient.io/build ^
-H "Authorization: Bearer your_api_key_here" ^
-o result.pdf ^
--fail ^
-F [email protected] ^
-F [email protected] ^
-F instructions="{\"parts\": [{\"file\": \"document\", \"pages\": {\"end\": -2}}, {\"file\": \"document\", \"pages\": {\"start\": -1}, \"actions\": [{\"type\": \"watermark\", \"image\": \"logo\", \"width\": \"50%\"}]}]}"
package com.example.pspdfkit;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import org.json.JSONArray;
import org.json.JSONObject;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public final class PspdfkitApiExample {
public static void main(final String[] args) throws IOException {
final RequestBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart(
"document",
"document.pdf",
RequestBody.create(
MediaType.parse("application/pdf"),
new File("document.pdf")
)
)
.addFormDataPart(
"logo",
"logo.png",
RequestBody.create(
MediaType.parse("image/png"),
new File("logo.png")
)
)
.addFormDataPart(
"instructions",
new JSONObject()
.put("parts", new JSONArray()
.put(new JSONObject()
.put("file", "document")
.put("pages", new JSONObject()
.put("end", -2)
)
)
.put(new JSONObject()
.put("file", "document")
.put("pages", new JSONObject()
.put("start", -1)
)
.put("actions", new JSONArray()
.put(new JSONObject()
.put("type", "watermark")
.put("image", "logo")
.put("width", "50%")
)
)
)
).toString()
)
.build();
final Request request = new Request.Builder()
.url("https://api.nutrient.io/build")
.method("POST", body)
.addHeader("Authorization", "Bearer your_api_key_here")
.build();
final OkHttpClient client = new OkHttpClient()
.newBuilder()
.build();
final Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
Files.copy(
response.body().byteStream(),
FileSystems.getDefault().getPath("result.pdf"),
StandardCopyOption.REPLACE_EXISTING
);
} else {
// Handle the error
throw new IOException(response.body().string());
}
}
}
using System;
using System.IO;
using System.Net;
using RestSharp;
namespace PspdfkitApiDemo
{
class Program
{
static void Main(string[] args)
{
var client = new RestClient("https://api.nutrient.io/build");
var request = new RestRequest(Method.POST)
.AddHeader("Authorization", "Bearer your_api_key_here")
.AddFile("document", "document.pdf")
.AddFile("logo", "logo.png")
.AddParameter("instructions", new JsonObject
{
["parts"] = new JsonArray
{
new JsonObject
{
["file"] = "document",
["pages"] = new JsonObject
{
["end"] = -2
}
},
new JsonObject
{
["file"] = "document",
["pages"] = new JsonObject
{
["start"] = -1
},
["actions"] = new JsonArray
{
new JsonObject
{
["type"] = "watermark",
["image"] = "logo",
["width"] = "50%"
}
}
}
}
}.ToString());
request.AdvancedResponseWriter = (responseStream, response) =>
{
if (response.StatusCode == HttpStatusCode.OK)
{
using (responseStream)
{
using var outputFileWriter = File.OpenWrite("result.pdf");
responseStream.CopyTo(outputFileWriter);
}
}
else
{
var responseStreamReader = new StreamReader(responseStream);
Console.Write(responseStreamReader.ReadToEnd());
}
};
client.Execute(request);
}
}
}
// This code requires Node.js. Do not run this code directly in a web browser.
const axios = require('axios')
const FormData = require('form-data')
const fs = require('fs')
const formData = new FormData()
formData.append('instructions', JSON.stringify({
parts: [
{
file: "document",
pages: {
end: -2
}
},
{
file: "document",
pages: {
start: -1
},
actions: [
{
type: "watermark",
image: "logo",
width: "50%"
}
]
}
]
}))
formData.append('document', fs.createReadStream('document.pdf'))
formData.append('logo', fs.createReadStream('logo.png'))
;(async () => {
try {
const response = await axios.post('https://api.nutrient.io/build', formData, {
headers: formData.getHeaders({
'Authorization': 'Bearer your_api_key_here'
}),
responseType: "stream"
})
response.data.pipe(fs.createWriteStream("result.pdf"))
} catch (e) {
const errorString = await streamToString(e.response.data)
console.log(errorString)
}
})()
function streamToString(stream) {
const chunks = []
return new Promise((resolve, reject) => {
stream.on("data", (chunk) => chunks.push(Buffer.from(chunk)))
stream.on("error", (err) => reject(err))
stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")))
})
}
import requests
import json
response = requests.request(
'POST',
'https://api.nutrient.io/build',
headers = {
'Authorization': 'Bearer your_api_key_here'
},
files = {
'document': open('document.pdf', 'rb'),
'logo': open('logo.png', 'rb')
},
data = {
'instructions': json.dumps({
'parts': [
{
'file': 'document',
'pages': {
'end': -2
}
},
{
'file': 'document',
'pages': {
'start': -1
},
'actions': [
{
'type': 'watermark',
'image': 'logo',
'width': '50%'
}
]
}
]
})
},
stream = True
)
if response.ok:
with open('result.pdf', 'wb') as fd:
for chunk in response.iter_content(chunk_size=8096):
fd.write(chunk)
else:
print(response.text)
exit()
<?php
$FileHandle = fopen('result.pdf', 'w+');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.nutrient.io/build',
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_POSTFIELDS => array(
'instructions' => '{
"parts": [
{
"file": "document",
"pages": {
"end": -2
}
},
{
"file": "document",
"pages": {
"start": -1
},
"actions": [
{
"type": "watermark",
"image": "logo",
"width": "50%"
}
]
}
]
}',
'document' => new CURLFILE('document.pdf'),
'logo' => new CURLFILE('logo.png')
),
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer your_api_key_here'
),
CURLOPT_FILE => $FileHandle,
));
$response = curl_exec($curl);
curl_close($curl);
fclose($FileHandle);
POST https://api.nutrient.io/build HTTP/1.1
Content-Type: multipart/form-data; boundary=--customboundary
Authorization: Bearer your_api_key_here
--customboundary
Content-Disposition: form-data; name="instructions"
Content-Type: application/json
{
"parts": [
{
"file": "document",
"pages": {
"end": -2
}
},
{
"file": "document",
"pages": {
"start": -1
},
"actions": [
{
"type": "watermark",
"image": "logo",
"width": "50%"
}
]
}
]
}
--customboundary
Content-Disposition: form-data; name="document"; filename="document.pdf"
Content-Type: application/pdf
(document data)
--customboundary
Content-Disposition: form-data; name="logo"; filename="logo.png"
Content-Type: image/png
(logo data)
--customboundary--
Aligning Watermarks
This example will show some common watermark alignments by adding a text watermark in each of the corners.
curl -X POST https://api.nutrient.io/build \
-H "Authorization: Bearer your_api_key_here" \
-o result.pdf \
--fail \
-F [email protected] \
-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%"
}
]
}'
curl -X POST https://api.nutrient.io/build ^
-H "Authorization: Bearer your_api_key_here" ^
-o result.pdf ^
--fail ^
-F [email protected] ^
-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%\"}]}"
package com.example.pspdfkit;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import org.json.JSONArray;
import org.json.JSONObject;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public final class PspdfkitApiExample {
public static void main(final String[] args) throws IOException {
final RequestBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart(
"document",
"document.pdf",
RequestBody.create(
MediaType.parse("application/pdf"),
new File("document.pdf")
)
)
.addFormDataPart(
"instructions",
new JSONObject()
.put("parts", new JSONArray()
.put(new JSONObject()
.put("file", "document")
)
)
.put("actions", new JSONArray()
.put(new JSONObject()
.put("type", "watermark")
.put("text", "Top Left")
.put("width", 150)
.put("height", 20)
.put("left", 0)
.put("top", 0)
)
.put(new JSONObject()
.put("type", "watermark")
.put("text", "Top Center")
.put("width", 150)
.put("height", 20)
.put("top", 0)
)
.put(new JSONObject()
.put("type", "watermark")
.put("text", "Top Right")
.put("width", 150)
.put("height", 20)
.put("right", "100%")
.put("top", 0)
)
.put(new JSONObject()
.put("type", "watermark")
.put("text", "Center Left")
.put("width", 150)
.put("height", 20)
.put("left", 0)
)
.put(new JSONObject()
.put("type", "watermark")
.put("text", "Center")
.put("width", 150)
.put("height", 20)
)
.put(new JSONObject()
.put("type", "watermark")
.put("text", "Center Right")
.put("width", 150)
.put("height", 20)
.put("right", "100%")
)
.put(new JSONObject()
.put("type", "watermark")
.put("text", "Bottom Left")
.put("width", 150)
.put("height", 20)
.put("left", 0)
.put("bottom", "100%")
)
.put(new JSONObject()
.put("type", "watermark")
.put("text", "Bottom Center")
.put("width", 150)
.put("height", 20)
.put("bottom", "100%")
)
.put(new JSONObject()
.put("type", "watermark")
.put("text", "Bottom Right")
.put("width", 150)
.put("height", 20)
.put("right", "100%")
.put("bottom", "100%")
)
).toString()
)
.build();
final Request request = new Request.Builder()
.url("https://api.nutrient.io/build")
.method("POST", body)
.addHeader("Authorization", "Bearer your_api_key_here")
.build();
final OkHttpClient client = new OkHttpClient()
.newBuilder()
.build();
final Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
Files.copy(
response.body().byteStream(),
FileSystems.getDefault().getPath("result.pdf"),
StandardCopyOption.REPLACE_EXISTING
);
} else {
// Handle the error
throw new IOException(response.body().string());
}
}
}
using System;
using System.IO;
using System.Net;
using RestSharp;
namespace PspdfkitApiDemo
{
class Program
{
static void Main(string[] args)
{
var client = new RestClient("https://api.nutrient.io/build");
var request = new RestRequest(Method.POST)
.AddHeader("Authorization", "Bearer your_api_key_here")
.AddFile("document", "document.pdf")
.AddParameter("instructions", new JsonObject
{
["parts"] = new JsonArray
{
new JsonObject
{
["file"] = "document"
}
},
["actions"] = new JsonArray
{
new JsonObject
{
["type"] = "watermark",
["text"] = "Top Left",
["width"] = 150,
["height"] = 20,
["left"] = 0,
["top"] = 0
},
new JsonObject
{
["type"] = "watermark",
["text"] = "Top Center",
["width"] = 150,
["height"] = 20,
["top"] = 0
},
new JsonObject
{
["type"] = "watermark",
["text"] = "Top Right",
["width"] = 150,
["height"] = 20,
["right"] = "100%",
["top"] = 0
},
new JsonObject
{
["type"] = "watermark",
["text"] = "Center Left",
["width"] = 150,
["height"] = 20,
["left"] = 0
},
new JsonObject
{
["type"] = "watermark",
["text"] = "Center",
["width"] = 150,
["height"] = 20
},
new JsonObject
{
["type"] = "watermark",
["text"] = "Center Right",
["width"] = 150,
["height"] = 20,
["right"] = "100%"
},
new JsonObject
{
["type"] = "watermark",
["text"] = "Bottom Left",
["width"] = 150,
["height"] = 20,
["left"] = 0,
["bottom"] = "100%"
},
new JsonObject
{
["type"] = "watermark",
["text"] = "Bottom Center",
["width"] = 150,
["height"] = 20,
["bottom"] = "100%"
},
new JsonObject
{
["type"] = "watermark",
["text"] = "Bottom Right",
["width"] = 150,
["height"] = 20,
["right"] = "100%",
["bottom"] = "100%"
}
}
}.ToString());
request.AdvancedResponseWriter = (responseStream, response) =>
{
if (response.StatusCode == HttpStatusCode.OK)
{
using (responseStream)
{
using var outputFileWriter = File.OpenWrite("result.pdf");
responseStream.CopyTo(outputFileWriter);
}
}
else
{
var responseStreamReader = new StreamReader(responseStream);
Console.Write(responseStreamReader.ReadToEnd());
}
};
client.Execute(request);
}
}
}
// This code requires Node.js. Do not run this code directly in a web browser.
const axios = require('axios')
const FormData = require('form-data')
const fs = require('fs')
const formData = new FormData()
formData.append('instructions', JSON.stringify({
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%"
}
]
}))
formData.append('document', fs.createReadStream('document.pdf'))
;(async () => {
try {
const response = await axios.post('https://api.nutrient.io/build', formData, {
headers: formData.getHeaders({
'Authorization': 'Bearer your_api_key_here'
}),
responseType: "stream"
})
response.data.pipe(fs.createWriteStream("result.pdf"))
} catch (e) {
const errorString = await streamToString(e.response.data)
console.log(errorString)
}
})()
function streamToString(stream) {
const chunks = []
return new Promise((resolve, reject) => {
stream.on("data", (chunk) => chunks.push(Buffer.from(chunk)))
stream.on("error", (err) => reject(err))
stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")))
})
}
import requests
import json
response = requests.request(
'POST',
'https://api.nutrient.io/build',
headers = {
'Authorization': 'Bearer your_api_key_here'
},
files = {
'document': open('document.pdf', 'rb')
},
data = {
'instructions': json.dumps({
'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%'
}
]
})
},
stream = True
)
if response.ok:
with open('result.pdf', 'wb') as fd:
for chunk in response.iter_content(chunk_size=8096):
fd.write(chunk)
else:
print(response.text)
exit()
<?php
$FileHandle = fopen('result.pdf', 'w+');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.nutrient.io/build',
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_POSTFIELDS => array(
'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%"
}
]
}',
'document' => new CURLFILE('document.pdf')
),
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer your_api_key_here'
),
CURLOPT_FILE => $FileHandle,
));
$response = curl_exec($curl);
curl_close($curl);
fclose($FileHandle);
POST https://api.nutrient.io/build HTTP/1.1
Content-Type: multipart/form-data; boundary=--customboundary
Authorization: Bearer your_api_key_here
--customboundary
Content-Disposition: form-data; name="instructions"
Content-Type: application/json
{
"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%"
}
]
}
--customboundary
Content-Disposition: form-data; name="document"; filename="document.pdf"
Content-Type: application/pdf
(document data)
--customboundary--
Reference
The following schema shows all the options and valid inputs for the watermark action.
// 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.
};