Add a watermark to PDF or remove previously added watermarks using JavaScript

Using this Node library, stamp PDF documents with an image or text watermark or remove an already added watermark in a few steps

Add a text or image watermark to PDF documents using JavaScript. Load the PDF document and insert the required watermark into it. Then save the result in a convenient format such as DOCX, PDF, MD, HTML, TXT, DOC, RTF, EPUB, XPS and more, and share the watermarked document with another person. Also remove the watermark from PDF document if the watermark was previously added to the document, and the reasons for adding are not longer relevant.

View code snippet

PDF – add watermark in JavaScript

With this Node API, developers can add a watermark to PDF online. Just create the watermark using text or an image and then insert that watermark into your PDF document in JavaScript.

Note: there is no need to install any third party applications or libraries. Therefore, our Node software solution is very easy to use.

Remove watermark from PDF – JavaScript

The given Node SDK is also helpful to remove watermarks from PDF. Removing a watermark in PDF means that you can load the desired PDF document stamped with a text or image watermark, remove this watermark from the PDF using JavaScript, and then save the output file in a convenient format – DOC, DOCX, PDF, JPEG, PNG, TIFF and others.

Work with watermark in PDF

To evaluate how to add or remove a watermark in PDF with our Node API, load the PDF and the watermark as an image or text. Then select a convenient export file format – DOC, DOCX, PDF, HTML, RTF, XPS and others.

Use the following example to see how it works:

File needs to add watermarks
Run code
Upload an image you want insert
Select the target format from the list
import * as fs from "fs";

const wordsApi = new WordsApi("####-####-####-####-####", "##################");

const requestDocument = fs.createReadStream("Input1.pdf");
const requestImageStream = fs.createReadStream("Input2.jpg");
const requestWatermarkData = new model.WatermarkDataImage({
    image: model.FileReference.fromLocalFileContent(requestImageStream)
});
const insertWatermarkRequest = new model.InsertWatermarkOnlineRequest({
    document: requestDocument,
    watermarkData: requestWatermarkData
});

wordsApi.InsertWatermarkOnline(insertWatermarkRequest)
.then((insertWatermarkResult) => {
    console.log("Result of InsertWatermarkImageOnline: ", insertWatermarkResult);

    const convertRequest = new model.ConvertDocumentRequest({
        document: insertWatermarkResult.Document.Values(),
        format: "pdf"
    });

    wordsApi.convertDocument(convertRequest)
    .then((convertResult) => {
        console.log("Result of ConvertDocument: ", convertResult);
    });
});
import * as fs from "fs";

const wordsApi = new WordsApi("####-####-####-####-####", "##################");

const requestDocument = fs.createReadStream("Input.pdf");
const requestWatermarkData = new model.WatermarkDataText({
    text: "This is the watermark text",
    layout: model.WatermarkDataText.LayoutEnum.Diagonal
})
const insertWatermarkRequest = new model.InsertWatermarkOnlineRequest({
    document: requestDocument,
    watermarkData: requestWatermarkData
});

wordsApi.insertWatermarkOnline(insertWatermarkRequest)
.then((insertWatermarkResult) => {
    console.log("Result of InsertWatermarkTextOnline: ", insertWatermarkResult);

    const convertRequest = new model.ConvertDocumentRequest({
        document: insertWatermarkResult.Document.Values(),
        format: "pdf"
    });

    wordsApi.convertDocument(convertRequest)
    .then((convertResult) => {
        console.log("Result of ConvertDocument: ", convertResult);
    });
});
import * as fs from "fs";

const wordsApi = new WordsApi("####-####-####-####-####", "##################");

const requestDocument = fs.createReadStream("Input.pdf");
const deleteWatermarkOnline = new model.DeleteWatermarkOnlineRequest({
    document: requestDocument
});

wordsApi.deleteWatermarkOnline(deleteWatermarkOnline)
.then((deleteWatermarkOnlineResult) => {
    console.log("Result of DeleteWatermarkOnline: ", deleteWatermarkOnlineResult);

    const requestDocument = deleteWatermarkOnlineResult.Document.Values();
    const convertDocument = new model.ConvertDocumentRequest({
        document: requestDocument,
        format: "pdf"
    });

    wordsApi.convertDocument(convertDocument)
    .then((convertDocumentResult) => {
        console.log("Result of ConvertDocument: ", convertDocumentResult);
    });
});
Run code
  
Copy JavaScript code to the clipboard

How to remove watermark in PDF using JavaScript

  1. Install Aspose.Words Cloud for Node
  2. Add a library reference (import the library) to your JavaScript project
  3. Configure API using your app keys
  4. Load PDF document you want to remove the watermark from
  5. Create a request to remove the watermark
  6. Save the result to a convenient file format

JavaScript library to use watermarks in PDF

Install 'Aspose.Words Cloud SDK for Node.js' using NPM package manager. Run npm install asposewordscloud --save from the command line to install the SDK via the NPM package manager. As an alternative, you can manually clone Aspose.Words Cloud SDK for Node.js source code from GitHub and use it in your project. Please follow these Instructions to quickly get the necessary security credentials and access our REST API.

System Requirements

  • @types/request (version 2.48.3+)
  • lodash (version 4.17.15+)
  • lodash.template (version 4.5.0+)
  • request (version 2.88.0+)
  • request-debug (version 0.2.0+)

Refer to the Repository Documentation to see more details.

Other supported file formats

You can add a watermark to a document in other formats:

5%

Subscribe to Aspose Product Updates

Get monthly newsletters and offers directly delivered to your mailbox.

© Aspose Pty Ltd 2001-2024. All Rights Reserved.