Watermark with Text
Purpose
Using this API, you can provide the watermark details in the form of text to a document.
Quick Reference
| Property | Value |
|---|---|
| Method | POST |
| Request URL | https://{api.office-integrator_domain}/writer/officeapi/v1/document/watermark |
Parameters
Body Parameters
| Parameter | Value | Mandatory/Optional | Description |
|---|---|---|---|
| apikey | 423s***** | Mandatory | Uniquely identifies the web application in which the Writer editor is integrated. |
| document or url | File or String | Mandatory | Method of providing the input file depending on its location. document - if the input file is from your local drive or desktop. url - if the input file is from a publicly accessible Web URL. |
| watermark_settings | { "text": "DRAFT", "type": "text" } | Mandatory | Specify the details of the watermark as JSON. Specify the watermark content inside the 'text' key. Provide the default value -> 'text' (as the watermark is a text) inside the 'type' key. You can also set or control other optional functions listed below inside the JSON. |
watermark_settings
| Parameter/Keys | Type | Mandatory/Optional | Possible Values | Default Value |
|---|---|---|---|---|
| text | String | Mandatory | e.g., DRAFT / CONFIDENTIAL | — |
| type | String | Mandatory | text | text |
| orientation | String | Optional | diagonal / horizontal / vertical | diagonal |
| font_name | String | Optional | All fonts are supported | Arial |
| font_size | Integer | Optional | 8 to 144 (in points) | 36 |
| font_color | String | Optional | Hex (#04AB8F) or RGB format | #000000 (black) |
| opacity | Double | Optional | 0.0 to 100.0 | 0.0 |
Notes and Limits
- The text and type keys inside watermark_settings are mandatory.
- The type key must always be set to text for text-based watermarks.
- All fonts are supported for the font_name parameter.
- font_size accepts values from 8 to 144 in points.
- font_color can be in hex format (e.g., #04AB8F) or RGB format.
- opacity accepts a double value ranging from 0.0 to 100.0.
- For possible error codes, refer to: Error Codes
- For other server-side SDK sample code, refer to: Server-Side SDKs
Sample Request
Copiedcurl --location --request POST "https://api.office-integrator.com/writer/officeapi/v1/document/watermark" \
--form "apikey=423s*****" \
--form "document=@" \
--form "watermark_settings={\"text\":\"draft\",\"type\":\"text\"}"
Copiedimport * as SDK from "@zoho-corp/office-integrator-sdk";
import { readFileSync, writeFileSync } from 'fs';
const __dirname = import.meta.dirname;
class WatermarkDocument {
static async execute() {
//Initializing SDK once is enough. Calling here since code sample will be tested standalone.
//You can place SDK initializer code in you application and call once while your application start-up.
await this.initializeSdk();
try {
var sdkOperations = new SDK.V1.V1Operations();
var watermarkParameters = new SDK.V1.WatermarkParameters();
//Either use url as document source or attach the document in request body use below methods
watermarkParameters.setUrl("https://demo.office-integrator.com/zdocs/Graphic-Design-Proposal.docx");
// var fileName = "Graphic-Design-Proposal.docx";
// var filePath = __dirname + "/sample_documents/Graphic-Design-Proposal.docx";
// var fileStream = readFileSync(filePath);
// var streamWrapper = new SDK.StreamWrapper(fileName, fileStream, filePath)
// watermarkParameters.setDocument(streamWrapper);
var watermarkSettings = new SDK.V1.WatermarkSettings();
watermarkSettings.setType("text");
watermarkSettings.setFontSize(18);
watermarkSettings.setOpacity(70.00);
watermarkSettings.setFontName("Arial");
watermarkSettings.setFontColor("#cd4544");
watermarkSettings.setOrientation("horizontal");
watermarkSettings.setText("Sample Water Mark Text");
watermarkParameters.setWatermarkSettings(watermarkSettings);
var responseObject = await sdkOperations.createWatermarkDocument(watermarkParameters);
//SDK Error: TypeError: Cannot read properties of null (reading 'getWrappedResponse')
if(responseObject != null) {
//Get the status code from response
console.log("\nStatus Code: " + responseObject.statusCode);
//Get the api response object from responseObject
let writerResponseObject = responseObject.object;
if(writerResponseObject != null) {
//Check if expected FileBodyWrapper instance is received
if(writerResponseObject instanceof SDK.V1.FileBodyWrapper) {
var watermarkDocument = writerResponseObject.getFile();
var outputFilePath = __dirname + "/sample_documents/watermark_output.docx";
if (watermarkDocument instanceof SDK.StreamWrapper) {
writeFileSync(outputFilePath, watermarkDocument.getStream());
console.log("\nCheck watermarked pdf output file in file path - ", outputFilePath);
}
} else if (writerResponseObject instanceof SDK.V1.InvalidConfigurationException) {
console.log("\nInvalid configuration exception. Exception json - ", writerResponseObject);
} else {
console.log("\nRequest not completed successfullly");
}
}
}
} catch (error) {
console.log("\nException while running sample code", error);
}
}
//Include office-integrator-sdk package in your package json and the execute this code.
static async initializeSdk() {
// Refer this help page for api end point domain details - https://www.zoho.com/officeintegrator/api/v1/getting-started.html
let environment = await new SDK.DataCenter.Production("https://api.office-integrator.com");
let auth = new SDK.AuthBuilder()
.addParam("apikey", "2ae438cf864488657cc97*******") //Update this apikey with your own apikey signed up in office integrator service
.authenticationSchema(await new SDK.V1.Authentication().getTokenFlow())
.build();
let tokens = [ auth ];
//Sdk application log configuration
let logger = new SDK.LogBuilder()
.level(SDK.Levels.INFO)
//.filePath("<file absolute path where logs would be written>") //No I18N
.build();
let initialize = await new SDK.InitializeBuilder();
await initialize.environment(environment).tokens(tokens).logger(logger).initialize();
console.log("SDK initialized successfully.");
}
}
WatermarkDocument.execute();