Add Image stamps in PDF using Python
Contents
[
Hide
]
Add Image Stamp in PDF File
You can use the ImageStamp class to add an image stamp to a PDF file. The ImageStamp class provides the properties necessary for creating an image-based stamp, such as height, width, opacity and so on.
To add an image stamp:
- Create a Document object and an ImageStamp object using required properties.
- Call the Page class add_stamp() method to add the stamp to the PDF.
The following code snippet shows how to add image stamp in the PDF file.
import aspose.pdf as ap
# Open document
document = ap.Document(input_pdf)
# Create image stamp
image_stamp = ap.ImageStamp(input_image_file)
image_stamp.background = True
image_stamp.x_indent = 100
image_stamp.y_indent = 100
image_stamp.height = 300
image_stamp.width = 300
image_stamp.rotate = ap.Rotation.ON270
image_stamp.opacity = 0.5
# Add stamp to particular page
document.pages[1].add_stamp(image_stamp)
# Save output document
document.save(output_pdf)
Control Image Quality when Adding Stamp
When adding an image as a stamp object, you can control the quality of the image. The quality property of the ImageStamp class is used for this purpose. It indicates the quality of image in percents (valid values are 0..100).
import aspose.pdf as ap
# Open document
document = ap.Document(input_pdf)
# Create image stamp
image_stamp = ap.ImageStamp(input_jpg)
image_stamp.quality = 10
# Add stamp to particular page
document.pages[1].add_stamp(image_stamp)
# Save output document
document.save(output_pdf)
Image Stamp as Background in Floating Box
Aspose.PDF for Python API lets you add image stamp as background in a floating box. The background property of FloatingBox class can be used to set the background image stamp for a floating box as shown in following code sample.
import aspose.pdf as ap
# Instantiate Document object
document = ap.Document()
# Add page to PDF document
page = document.pages.add()
# Create FloatingBox object
box = ap.FloatingBox(200.0, 100.0)
# Set left position for FloatingBox
box.left = 40
# Set Top position for FloatingBox
box.top = 80
# Set the Horizontal alignment for FloatingBox
box.horizontal_alignment = ap.HorizontalAlignment.CENTER
# Add text fragment to paragraphs collection of FloatingBox
box.paragraphs.add(ap.text.TextFragment("main text"))
# Set border for FloatingBox
box.border = ap.BorderInfo(ap.BorderSide.ALL, ap.Color.red)
img = ap.Image()
img.file = input_image_file
# Add background image
box.background_image = img
# Set background color for FloatingBox
box.background_color = ap.Color.yellow
# Add FloatingBox to paragraphs collection of page object
page.paragraphs.add(box)
# Save the PDF document
document.save(output_pdf)