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.
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());
}
}
}
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.
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());
}
}
}
Aligning Watermarks
This example will show some common watermark alignments by adding a text watermark in each of the corners.
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());
}
}
}
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.
};