Add Stamp to PDF in Python SDK
API for working with Stamps in PDF documents using Cloud Python SDK.
Get StartedHow to add Text Stamp to PDF via Cloud Python SDK
To add stamps into PDF, we’ll use Aspose.PDF Cloud Python SDK. This Cloud SDK assists Python programmers in developing cloud-based PDF creator, annotator, editor, and converter apps using Python programming language via Aspose.PDF REST API. Simply create an account at Aspose for Cloud and get your application information. Once you have the App SID & key, you are ready to give the Aspose.PDF Cloud Python SDK. If the python package is hosted on Github, you can install directly from Github:
Installation from Github
pip install git+https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-python.git
Package Manager Console Command
pip install asposepdfcloud
Steps to add Stamps into PDF via Python SDK
Aspose.PDF Cloud developers can easily load & add stamps to PDF in just a few lines of code.
- Install Python SDK
- Upload a PDF document to the Aspose Cloud server
- Upload Image file to the Aspose Cloud server
- Call API to insert Stamps
- Download the processed PDF document from the Aspose Cloud server
Add Stamps to PDF using Python
from asposepdfcloud import PdfApi, ApiClient, Stamp, StampType, HorizontalAlignment, AsposeResponse
import shutil
import os
import json
from pathlib import Path
import logging
# Configure logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
class PdfAddStamps:
"""Class for inserting Stamps to PDF using Aspose PDF Cloud API."""
def insert(self):
"""Add Stamps to PDF document."""
localFolder = "C:\Samples"
storagePDFName = "sample.pdf"
storageTempFolder = "TempPdfCloud"
outputFileName = "output_add_stamps.pdf"
imageStampFile ="sample.png"
# Get your AppSid and AppSecret from https://dashboard.aspose.cloud (free registration required).
self.pdf_api = PdfApi(ApiClient(AppSecret, AppSid))
if self.pdf_api:
file_path = localFolder + "/" + storagePDFName
self.pdf_api.upload_file(os.path.join(storageTempFolder, storagePDFName), file_path)
self.pdf_api.upload_file(os.path.join(storageTempFolder, imageStampFile), os.path.join(localFolder, imageStampFile))
text_stamp: Stamp = Stamp(
type = StampType.TEXT,
background = True,
horizontal_alignment = HorizontalAlignment.CENTER,
text_alignment = HorizontalAlignment.CENTER,
value="Your TEXT Stamp Value here"
)
image_stamp: Stamp = Stamp(
type = StampType.IMAGE,
background = True,
horizontal_alignment = HorizontalAlignment.CENTER,
text_alignment = HorizontalAlignment.CENTER,
value = "Your IMAGE Stamp Description here",
file_name = os.path.join(storageTempFolder, imageStampFile),
y_indent = 400,
width = 64,
height = 64
)
opts = { "folder": storageTempFolder }
try:
responseTextStamp: AsposeResponse = self.pdf_api.post_document_text_stamps(storagePDFName, [ text_stamp ], **opts)
responseImageStamp: AsposeResponse = self.pdf_api.post_document_image_stamps(storagePDFName, [ image_stamp ], **opts)
if responseTextStamp.code == 200 and responseImageStamp.code == 200:
temp_file = self.pdf_api.download_file(storageTempFolder + '/' + storagePDFName)
local_path = localFolder + '/' + outputFileName
shutil.move(temp_file, local_path)
logging.info(f"PdfAddStamps(): Stamps inserted successfully to PDF: '{outputFileName}'.")
else:
logging.error(f"PdfAddStamps(): Failed to insert Stamps to PDF. Response code:test = {responseTextStamp.code}, image = {responseImageStamp}")
except Exception as e:
logging.error(f"PdfAddStamps(): Error while converting image: {e}")
Work with Stamps in PDF
Adding stamps to PDF documents serves multiple purposes, enhancing both the functionality and security of digital files. As a developer, understanding these benefits can inform the implementation of features that streamline document workflows and reinforce data integrity. In collaborative environments, documents often require multiple reviews and approvals. Stamps can deter unauthorized distribution and modification. Stamping functionality into PDF management systems can significantly enhance document workflows, security, and compliance. By effectively leveraging stamps, organizations can ensure their documents are professional and protected. Add the Stamps to PDF documents with Aspose.PDF Cloud Python SDK.
With our Python library you can:
- Combine PDF documents.
- Split PDF Files.
- Convert PDF to other formats, and vice versa.
- Manipulate Annotations.
- Work with Images in PDF, etc.
- You can try out our free App to test the functionality online.