Delete Stamp By ID

Contents
[ ]

When working with PDFs containing multiple stamps, it is often necessary to remove specific stamps without affecting others. Using ID-based deletion, you can precisely control which stamps to remove:

  • ‘delete_stamp_by_id(stamp_id, page_number)’ – deletes a single stamp by its ID on a specific page
  • ‘delete_stamp_by_ids(page_number, stamp_ids)’ – deletes multiple stamps by their IDs on a given page
  1. Create a PdfContentEditor instance.
  2. Bind the input PDF document.
  3. Add two rubber stamps with distinct IDs.
  4. Delete stamps using both single-ID and multiple-ID deletion methods.
  5. Save the updated PDF.
import aspose.pdf.facades as pdf_facades
import aspose.pydrawing as apd
from io import BytesIO
import sys
from os import path

sys.path.append(path.join(path.dirname(__file__), ".."))

from config import set_license, initialize_data_dir


def delete_stamp_by_ids_examples(infile, outfile):
    # Create PdfContentEditor object
    content_editor = pdf_facades.PdfContentEditor()
    # Bind document to PdfContentEditor
    content_editor.bind_pdf(infile)

    # Create two stamps on page 1 so they can be deleted by ID
    content_editor.create_rubber_stamp(
        1,
        apd.Rectangle(120, 320, 180, 60),
        "Draft",
        "Delete by single ID",
        apd.Color.orange,
    )
    content_editor.create_rubber_stamp(
        1,
        apd.Rectangle(120, 250, 180, 60),
        "Draft",
        "Delete by multiple IDs",
        apd.Color.orange,
    )

    # Delete by single ID overload and by IDs overload
    content_editor.delete_stamp_by_id(1, 1)
    content_editor.delete_stamp_by_ids(1, [2])

    # Save updated document
    content_editor.save(outfile)