Replace Images in PDF

Contents
[ ]

PDFs often contain images that may need to be updated or replaced, such as logos, diagrams, or photos. With PdfContentEditor, you can replace a specific image on a given page by providing the page number, image index, and new image file path.

  1. Create a PdfContentEditor instance.
  2. Bind the input PDF document.
  3. Replace a specific image on a given page.
  4. Save the updated PDF document.
import aspose.pdf.facades as pdf_facades
import sys
from os import path

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

from config import set_license, initialize_data_dir


def replace_image(infile, image_file, outfile):
    # Create PdfContentEditor object
    content_editor = pdf_facades.PdfContentEditor()
    # Bind document to PdfContentEditor
    content_editor.bind_pdf(infile)
    # Replace image on page 1
    content_editor.replace_image(1, 1, image_file)
    # Save updated document
    content_editor.save(outfile)