Adding Image stamps in PDF using Python
Adding 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. The stamp can be positioned, resized, rotated, and made partially transparent, allowing for watermarking, branding, or annotations.
The following code snippet shows how to add image stamp in the PDF file.
- Load the PDF using ‘ap.Document()’.
- Create an image stamp with ‘ImageStamp()’.
- Configure stamp properties.
- Add the stamp to the target page.
- Save the modified PDF.
import os
import aspose.pdf as ap
# Global configuration
DATA_DIR = "your path here"
def add_image_stamp(infile, input_image_file, outfile):
document = ap.Document(infile)
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
document.pages[1].add_stamp(image_stamp)
document.save(outfile)
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). By setting the quality property, you can reduce the image resolution to optimize PDF size or maintain higher fidelity for clarity.
- Open the PDF document.
- Create an image stamp.
- Set image quality.
- Add the stamp to the target page.
- Save the modified PDF.
import os
import aspose.pdf as ap
# Global configuration
DATA_DIR = "your path here"
def add_image_stamp_image_control_image_quality(infile, input_image_file, outfile):
document = ap.Document(infile)
image_stamp = ap.ImageStamp(input_image_file)
image_stamp.quality = 10
document.pages[1].add_stamp(image_stamp)
document.save(outfile)
Image Stamp as Background in Floating Box
Create a FloatingBox in a PDF and apply an image as its background. It also shows how to add text, set borders, background color, and position the box precisely on the page. This is useful for creating visually rich PDF content like callouts, banners, or highlighted sections with text over images.
- Open or create a PDF document.
- Create a ‘FloatingBox’ object.
- Add text content to the box.
- Set box border and background color.
- Add a background image.
- Add the FloatingBox to the page.
- Save the PDF document.
import os
import aspose.pdf as ap
# Global configuration
DATA_DIR = "your path here"
def add_image_as_background_in_floating_box(infile, input_image_file, outfile):
document = ap.Document(infile)
# 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("Text in Floating Box"))
# 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(outfile)