Add Text Watermark or Image Watermark to PDF using Python

Watermark PDF

One of the handy mechanisms to protect the confidentiality and integrity of information in PDF files is the addition of a watermark in PDF documents. Although, there are many PDF security options available, but the most common method of keeping confidential information secure is the addition of a watermark to the PDF files. In this article, we are going to discuss the steps on how to add text and image watermark in PDF files.

Info: Aspose provides free online PowerPoint tools that allow you to add watermarks to presentations and remove watermarks from presentations.

PDF Processing REST API

Aspose.PDF Cloud is our award-winning REST API providing the features to create, manipulate and render PDF files to various output formats. It also enables you to load EPUB, HTML, TeX, SVG, XML, etc files and save them in PDF format with fewer code lines. The most amazing part of this API is its platform-independent capabilities. Implement and utilize PDF processing capabilities on any platform including Desktop, Web, or Mobile. No need to install Adobe Acrobat or other applications to accomplish your requirements.

As our emphasis in this article is towards Python language, so we need to first install Aspose.PDF Cloud SDK for Python which is a wrapper around Aspose.PDF Cloud API. The SDK is available for download over PIP and GitHub repository. So please execute the following command on the terminal/command prompt to install the latest version of SDK on the system.

 pip install asposepdfcloud

MS Visual Studio

In case you need to directly add the reference in your Python project within Visual Studio IDE, please search asposepdfcloud as a package under the Python environment window. Please follow the steps numbered in the image below to complete the installation process.

Aspose.PDF Cloud Python
Image 1:- Aspose.PDF Cloud SDK for Python package.

Aspose.Cloud Dashboard

In order to get started with Cloud APIs, we need to create an account on Aspose.Cloud dashboard. If you have GitHub or Google account, simply Sign Up or, click on the Create a new Account button and provide the required information. Now login to the dashboard using credentials and expand the Applications section from the dashboard and scroll down towards the Client Credentials section to see Client ID and Client Secret details.

Client Credentials
Image 2:- Client credentials on Aspose.Cloud dashboard.

Add Text Watermark in PDF using Python

Please follow the instructions given below to add text watermark in PDF document using Python code snippet.

  • First, we need to create an instance of ApiClient class while providing Client ID Client Secret as arguments
  • Secondly, create an instance of PdfApi class which takes ApiClient object as input argument
  • Thirdly, specify the names of input PDF file, resultant PDF and page number where watermark needs to be inserted
  • Now create a Stamp object and specify properties related to rotate agngle, opacity, horizonal and vertical allignment, watermark value, font details, forground and background color information
  • Finally, call the post_page_text_stamps(..) method of PdfApi to add the watermark in PDF document
Text watermark preview
Image 3:- Text Watermark added in PDF.

Use cURL command to add Text Watermark

The cURL commands provide a convenient mechanism for accessing REST APIs through the command prompt terminal. So we can also access Aspose.PDF Cloud via the cURL command. However, before accessing the API, we need to generate a JSON Web Token (JWT) based on your individual client credentials specified over Aspose.Cloud dashboard. It is mandatory because our APIs are only accessible to registered users. Please execute the following command to generate the JWT token.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=88d1cda8-b12c-4a80-b1ad-c85ac483c5c5&client_secret=406b404b2df649611e508bbcfcd2a77f" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Please use the following command to add a text watermark in the PDF file with a rotate angle of 45 degrees, having horizontal alignment as Left and Vertical alignment as Bottom.

curl -v -X POST "https://api.aspose.cloud/v3.0/pdf/awesomeTable.pdf/pages/1/stamps/text" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>" \
-H  "Content-Type: application/json" \
-d "[  {    \"Background\": true,    \"HorizontalAlignment\": \"1\",    \"Opacity\": 0.5,    \"Rotate\": \"1\",    \"RotateAngle\": 45.,    \"XIndent\": 100,    \"YIndent\": 100,    \"Zoom\": 1.5,    \"TextAlignment\": \"0\",    \"Value\": \"Confidential\",    \"TextState\": {      \"FontSize\": 20,      \"Font\": \"Arial\",      \"ForegroundColor\": {        \"A\": 0,        \"R\": 200,        \"G\": 0,        \"B\": 0      },      \"BackgroundColor\": {        \"A\": 10,        \"R\": 0,        \"G\": 0,        \"B\": 0      },      \"FontStyle\": \"2\"    },    \"VerticalAlignment\": \"1\",    \"BottomMargin\": 10,    \"LeftMargin\": 10,    \"TopMargin\": 10,    \"RightMargin\": 10  }]"

Use cURL command to add Image Watermark

Please use the following cURL command to add an image as a stamp to a PDF document and save the output in cloud storage. In case you need to save on a distinct location, please try using -o argument.

curl -v -X POST "https://api.aspose.cloud/v3.0/pdf/awesomeTable.pdf/pages/1/stamps/image" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>" \
-H  "Content-Type: application/json" \
-d "[  {    \"Links\": [      {        \"Type\": \"Image\",        \"Title\": \"Image stamp\"      }    ],    \"Background\": true,    \"HorizontalAlignment\": \"LEFT\",    \"Opacity\": 1.0,    \"Rotate\": \"None\",    \"RotateAngle\": 0,    \"XIndent\": 0,    \"YIndent\": 0,    \"Zoom\": 0.5,    \"FileName\": \"confidential.jpg\",    \"Width\": 400,    \"Height\": 200,    \"VerticalAlignment\": \"TOP\",    \"BottomMargin\": 0,    \"LeftMargin\": 10,    \"TopMargin\": 10,    \"RightMargin\": 0  }]"
Image watermark preview
Image 4:- Image watermark in PDF.

The sample files used in the above example can be downloaded from awesomeTable.pdf, Text-Watermark.pdf, and Image-Watermark.pdf.

Conclusion

In this article, we have discussed the steps to add an Image and Text watermark in PDF files. We have learnt the steps to add watermark using Python SDK or using the cURL command. Please note that our Cloud SDKs are developed under MIT license, so their complete code snippet is available for free download over GitHub. Should you have any related queries or you encounter any issues while using our APIs, please feel free to contact us via the free customer support forum.

Related Articles

We recommend visiting the following articles to learn about: