Insert Watermarks via Webservice with wsclient
Minimum Requirements
- Java version: 7
- webPDF version: 7
- wsclient version: 1
Watermark Operation of the ToolboxWebService
This article introduces the Watermark operation of the webPDF ToolboxWebService and shows how to use it with the webPDF wsclient library.
Important note: The following coding example is based on the webPDF wsclient library.
Preparing a REST or SOAP Session
First, create a REST or SOAP session and use the WebServiceFactory to create either a ToolboxWebService object for SOAP or a ToolboxRestWebService object for REST.
SOAP:
ToolboxWebService toolboxWebService =
WebServiceFactory.createInstance(
session, WebServiceType.TOOLBOX
);
REST:
ToolboxRestWebService toolboxWebService =
WebServiceFactory.createInstance(
session, WebServiceType.TOOLBOX
);
After that, pass a RestDocument or SoapDocument via setDocument().
Details About Webservice Parameters
For write access, the current open and/or permission password is often required:
toolboxWebService.getPassword().setOpen("password");
toolboxWebService.getPassword().setPermission("password");
If the document is not password protected, you can skip this step.
Adding the Watermark Operation
The ToolboxWebService combines multiple operations for directly editing PDF documents. Add the Watermark operation like this:
WatermarkType watermark = new WatermarkType();
toolboxWebService.getOperation().add(watermark);
Parameters on the Watermark Object
pages (default: "")
Defines the page range. You can use single pages (1), lists (1,3,5), ranges (1-5), or combinations (1,3-5,6). Use * for all pages.
watermark.setPages("1,3-5,6");
angle (default: "45")
Defines the watermark rotation from 0 to 360 degrees clockwise.
watermark.setAngle(66);
The image Object
If an image should be used as a watermark, set a WatermarkImageType object.
WatermarkImageType image = new WatermarkImageType();
watermark.setImage(image);
opacity (default: 25)
Opacity in percent from 0 to 100:
image.setOpacity(32);
scale (default: 0)
Scaling in percent:
image.setScale(200);
The data Object
The image source is configured via WatermarkFileDataType.
WatermarkFileDataType data = new WatermarkFileDataType();
image.setData(data);
source
Defines the source:
value: pass the file directly as a byte arrayuri: pass a URI reference to the file
data.setSource(FileDataSourceType.VALUE);
value
Sets form data as a byte array when source = value:
data.setValue(FileUtils.readFileToByteArray(new File("...")));
uri
Sets a file reference as a URI when source = uri:
data.setUri(new File("...").toURI().toString());
The text Object
Alternatively, you can create a text watermark:
WatermarkTextType text = new WatermarkTextType();
watermark.setText(text);
text (default: "Confidential")
Defines the text to be rendered as the watermark:
text.setText("Confidential");
The font Object
Font settings are configured with a WatermarkFontType object:
WatermarkFontType font = new WatermarkFontType();
text.setFont(font);
Important parameters:
opacity(default:100):font.setOpacity(35);name(default:""): if empty, Helvetica is usedbold(default:false):font.setBold(true);color(default:"#A0A0A0"):font.setColor("#FFFFFF");italic(default:false):font.setItalic(true);outline(default:false):font.setOutline(true);size(default:24):font.setSize(12);
The position Object
Positioning is handled via WatermarkPositionType, which can be used for image or text watermarks:
WatermarkPositionType position = new WatermarkPositionType();
image.setPosition(position);
Parameters:
x(default:0):position.setX(15);y(default:0):position.setY(-6);width(default:0):position.setWidth(200);height(default:0):position.setHeight(16);metrics(default:"mm"):position.setMetrics(MetricsType.PX);position(default:"center_center"):position.setPosition(WatermarkPositionModeType.BOTTOM_RIGHT);aspectRatio(default:true):position.setAspectRatio(false);
SOAP Example
try (
// Set up a session with the webPDF server (SOAP in this example):
SoapSession session = SessionFactory.createInstance(
WebServiceProtocol.SOAP,
new URL("https://localhost:8080/webPDF/")
);
// Provide the source and target documents:
SoapDocument soapDocument = new SoapDocument(
new File("Path to the source document").toURI(),
new File("Path to the target document")
)
) {
// Execute webservice call and processing
} catch (ResultException | MalformedURLException ex) {
// Error handling
}
Tip
- More parameters are documented in the Watermark parameter structure.
- Many parameters already have defaults and only need to be set if you want different values.
More coding examples are available here.