Watermark with Text

Purpose

Using this API, you can provide the watermark details in the form of text.

HTTP Request URL

https://{api.office-integrator_domain}/writer/officeapi/v1/document/watermark

Request Parameters

ParameterData TypeDescription
Mandatory Parameters
apikey423s*****Uniquely identifies the web application in which the Writer editor is integrated.

Body Parameters

ParameterValueDescription
Mandatory Parameters
document

or

url
File

or

String
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"

}

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

ParameterTypePossible ValuesDefault Value
orientationstringdiagonal / horizontal / verticaldiagonal
font_namestringAll fonts are supportedArial
font_sizeinteger8 to 14436 (in points)
font_colorstringCan be represented as #04AB8F. RGB format is also allowed.#000000 (black)
opacitydouble0.0 to 100.00.0

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();

Sample Response

The response will be in the form of bytes.