Webservice insert watermark with wsclient
Minimum technical requirements
- Java version: 7
- webPDF version: 7
- wsclient version: 1
Our link tips for preparation in advance:
- Further parameters are also documented in our user manual (without examples for use with the wsclient library). Click here for the watermark parameter structure
- Please also note: The parameters are preset with certain default values. If a default value is specified and does not deviate from your desired value, it is not mandatory to set this parameter.
- To use the REST interface with webPDF wsclient
- To use the SOAP interface
Important note:
The following coding example is based on the use of the webPDF wsclient library. In order to understand and apply the example, the following blog post should be considered first:
Watermark Operation of the ToolboxWebService
We want to introduce here the Watermark operation of the webPDF ToolboxWebService and the use with the webPDF wsclient library.
First step: First, you should create a REST or SOAP session and thus be able to create either a ToolboxWebService object (for a SOAP session) by calling the WebserviceFactory:
ToolboxWebService toolboxWebService = WebServiceFactory.createInstance( session, WebServiceType.TOOLBOX );
..or a ToolboxRestWebService object (for a REST session):
ToolboxRestWebService toolboxWebService = WebServiceFactory.createInstance( session, WebServiceType.TOOLBOX );
As well as pass either a RestDocument or a SoapDocument object to this WebService object by calling the method setDocument().
Details about the Webservice Parameter
If you want to get changing access to a document, it is necessary to give the current open and/or permission password of the document to the webservice call.
You can do this directly at the created ToolboxWebService object:
toolboxWebService.getPassword().setOpen("password"); toolboxWebService.getPassword().setPermission("password");
Info: If the document is not password protected, you can skip this point.
ToolboxWebService: Example Watermark Operation
Explanation: The ToolboxWebService is an endpoint of your webPDF server that combines several operations with which you can directly customize your PDF document. One of these operations is the Watermark operation. This allows you to watermark pages of your document.
You add a Watermark operation to your WebService object as follows:
WatermarkType watermark = new WatermarkType(); toolboxWebService.getOperation().add(watermark);
The following parameters can be set for the Watermark object:
pages (default value: „“)
Defines the page area from whose pages images are to be generated. Either a single page („1“), a list of pages („1,3,5“), a page range („1-5“) or a combination of these elements („1,3-5,6“) can be specified. All pages of the document can be selected using „*“.
watermark.setPages("1,3-5,6");
angle (default value: „45“)
Defines any rotation for the watermark, in the value range from 0 to 360 degrees. The rotation is clockwise.
watermark.setAngle(66);
The objekt „image“
If a graphic is to be used for the watermark, it can be set in the form of a WatermarkImageType object on the Watermark object.
WatermarkImageType image = new WatermarkImageType(); watermark.setImage(image);
The following parameters can be set on the Image object:
opacity (default value: 25)
A percentage value that determines the opacity of the graphic. The higher the value, the less transparent the watermark will be. A value between 0 and 100 can be selected.
image.setOpacity(32);
scale (default value: 0)
A percentage value that determines the scaling of the graphic.
image.setScale(200);
The object „data“
The selection of the graphic for the Watermark is done by adding a WatermarkFileDataType object to the image object.
WatermarkFileDataType data = new WatermarkFileDataType(); image.setData(data);
The following parameters can be set for the Data object:
source
Specifies the source from which the graphic is to be obtained. The following values can be set here:
- value = The file is passed directly as byte array.
- uri = A URI reference to the file is passed.
data.setSource(FileDataSourceType.VALUE);
value
Passes the form data in the form of a byte array. (source should be set to value.)
Uri
Transfers a URI reference to the document to be attached. (source should be set to uri.)
The object „text“
If a text is to be used for the watermark, it can be set in the form of a MergeFileDataType object on the Watermark object.
WatermarkTextType text = new WatermarkTextType(); watermark.setText(text);
The following parameters can be set for the Text object:
text (default value: „Confidential“)
Specifies the text to be output as a watermark.
position.setAspectRatio(false);
The object „font“
The watermark font is selected by adding a WatermarkFontType object to the text object.
WatermarkFontType font = new WatermarkFontType(); text.setFont(font);
The following parameters can be set for the Font object:
opacity (default value: 100)
A percentage value that determines the opacity of the text. The higher the value, the less transparent the watermark will be. A value between 0 and 100 can be selected.
font.setOpacity(35);
name (default value: „“)
Specifies the name of the font to be used. If no font is specified, Helvetica is used.
font.setName("FontName");
bold (default value: false)
If this value is set to true, a higher font size is selected for the text.
font.setBold(true);
color (default value: „#A0A0A0“)
Selects the color of the text via a HEX-RGB value.
font.setColor("#FFFFFF");
italic (default value: false)
If this value is set to true, an italic font is selected for the text.
font.setItalic(true);
outline (default value: false)
If this value is set to true, an outline font is selected for the text.
font.setOutline(true);
size (default value: 24)
Sets the font size of the text.
font.setSize(12);
The objekt „ position “
The watermark position is selected by adding a WatermarkPositionType object to the image or text object.
WatermarkPositionType position = new WatermarkPositionType(); image.setPosition(position);
The following parameters can be set for the Position object:
x (default value: 0)
Sets the x-position of the watermark in relation to the selected position using the selected metrics.
position.setX(15);
y (Default value: 0)
Determines the y-position of the watermark in relation to the selected position using the selected metrics.
position.setY(-6);
width (default value: 0)
Sets the width of the watermark in using the selected metrics when the value differs from 0. (May cause quality loss due to distortion.)
position.setWidth(200);
height (default value: 0)
Sets the height of the watermark in using the selected metrics when the value differs from 0. (May cause quality loss due to distortion.)
position.setHeight(16);
metrics (default value: „mm“)
Specifies the unit of measurement in which the sizes of other parameters are specified. The following values can be set here:
- mm = millimeter
- px = Pixel
position.setMetrics(MetricsType.PX);
position (default value: „center_center “)
Uses one of several preset positions to place the watermark on the page. The following values can be set here:
- custom = Disable the use of a preset position.
- top_left = Position the watermark in the upper left corner.
- top_center = Position the watermark in the middle of the top edge.
- top_right = Position the watermark in the upper right corner.
- center_left = Position the watermark in the center left of the page.
- center_center = Position the watermark in the middle of the page.
- center_right = Position the watermark at the center right of the page.
- bottom_left = Position the watermark in the lower left corner.
- bottom_center = Position the watermark in the middle at the bottom edge.
- bottom_right = Position the watermark in the lower right corner.
position.setPosition(WatermarkPositionModeType.BOTTOM_RIGHT);
aspectRatio (default value: true)
If this value is set to true, the system tries to keep the watermark aspect ratio if a fixed height or width is set. This prevents distortion of the graphic.
position.setAspectRatio(false);
In the descriptions of the webservice-calls we give a somewhat more detailed example of how to address the SOAP interface:
try ( // Setup of a session with the webPDF server (here SOAP): SoapSession session = SessionFactory.createInstance( WebServiceProtocol.SOAP, new URL("https://localhost:8080/webPDF/") ); // Make available the document to be processed // and the file in which the result is to be written: SoapDocument soapDocument = new SoapDocument( new File("Path of the source document").toURI(), new File("Path of the target document") ) ) { // Selection of the webservice via a factory:: ToolboxWebService toolboxWebService = WebServiceFactory.createInstance( session, WebServiceType.TOOLBOX ); toolboxWebService.setDocument(soapDocument); toolboxWebService.getPassword().setOpen("password"); toolboxWebService.getPassword().setPermission("password"); // Object oriented parameterization of the call:: WatermarkType watermark = new WatermarkType(); toolboxWebService.getOperation().add(watermark); watermark.setPages("1,3-5,6"); watermark.setAngle(66); WatermarkTextType text = new WatermarkTextType(); watermark.setText(text); WatermarkFontType font = new WatermarkFontType(); text.setFont(font); font.setOpacity(35); font.setName("FontName"); font.setBold(true); font.setColor("#FFFFFF"); font.setItalic(true); font.setOutline(true); font.setSize(12); WatermarkPositionType position = new WatermarkPositionType(); text.setPosition(position); position.setX(15); position.setY(-6); position.setWidth(200); position.setHeight(16); position.setMetrics(MetricsType.PX); position.setPosition(WatermarkPositionModeType.BOTTOM_RIGHT); position.setAspectRatio(false); // execution. toolboxWebService.process(); } catch (ResultException | MalformedURLException ex) { // To evaluate possible errors that may have occurred, the // wsclient library appropriate methods are available: }
More coding examples for webservices that you can use with the ws-client library can be found here.