Add Image to PDF using Python

Add Image in an Existing PDF File

This example demonstrates how to insert an image into a specific position on a PDF page using Aspose.PDF for Python via .NET.

  1. Load the PDF document with ‘ap.Document’.
  2. Select the target page ‘(document.pages[1]’ - the first page).
  3. Use ‘page.add_image()’ to place the image:
    • File path of the image.
    • A ‘Rectangle’ object defining the image’s coordinates (left=20, bottom=730, right=120, top=830).
  4. Save the updated PDF.

    import aspose.pdf as ap
    from io import FileIO
    from os import path

    path_infile = path.join(self.data_dir, infile)
    path_outfile = path.join(self.data_dir, "python", outfile)

    document = ap.Document(path_infile)
    page = document.pages[1]
    page.add_image(
        path.join(self.data_dir, image_file),
        ap.Rectangle(20, 730, 120, 830, True),
    )
    document.save(path_outfile)

Add an Image Using Operators

Next code snippet shows a low-level approach to adding an image to a PDF page by manually working with PDF operators rather than high-level helper methods.

Steps:

  1. Create a new blank ‘Document’.
  2. Add a page and set its size (842 × 595 - landscape A4).
  3. Access the page’s image resources (page.resources.images).
  4. Load the image file into a stream and add it to the resources.
    • The method returns an ‘image_id’.
    • The newly added image object is retrieved from the resources.
  5. Define a rectangle that maintains the aspect ratio of the image:
  6. Build an operator sequence:
    • ‘GSave()’ - Save the current graphics state.
    • ‘ConcatenateMatrix(matrix)’ - Apply transformation (scale and center the image vertically on the page).
    • ‘Do(image_id)’ - Render the image.
    • ‘GRestore()’ - Restore graphics state.
  7. Add the operator sequence to ‘page.contents’.
  8. Save the resulting PDF.

    import aspose.pdf as ap
    from io import FileIO
    from os import path

    path_infile = path.join(self.data_dir, image_file)
    path_outfile = path.join(self.data_dir, "python", outfile)

    document = ap.Document()
    page = document.pages.add()
    page.set_page_size(842,595)

    # Get page resources
    resources_images = page.resources.images

    # Add image to resources
    image_stream = FileIO(path.join(self.data_dir, path_infile), "rb")
    image_id = resources_images.add(image_stream)

    x_image = list(resources_images)[-1]

    rectangle = ap.Rectangle(
        0,
        0,
        page.media_box.width,
        (page.media_box.width * x_image.height) / x_image.width,
        True,
    )

    # Create operator sequence for adding image
    operators = []

    # Save graphics state
    operators.append(ap.operators.GSave())

    # Set transformation matrix (position and size)
    matrix = ap.Matrix(
        rectangle.urx - rectangle.llx,
        0,
        0,
        rectangle.ury - rectangle.lly,
        rectangle.llx,
        rectangle.llx + (page.media_box.height - rectangle.height) / 2,
    )
    operators.append(ap.operators.ConcatenateMatrix(matrix))

    # Draw the image
    operators.append(ap.operators.Do(image_id))

    # Restore graphics state
    operators.append(ap.operators.GRestore())

    # Add operators to page contents
    page.contents.add(operators)

    document.save(path_outfile)

Add Image with Alternative Text

This example shows how to add an image to a PDF page and assign alternative text (alt text) for accessibility compliance (such as PDF/UA).

  1. Create a new ‘Document’ and add a page (842 × 595, landscape A4).
  2. Place the image on the page using ‘page.add_image()’ with a rectangle that spans the full page.
  3. Access the page’s image resources (‘page.resources.images’).
  4. Define an alternative text string (e.g., ‘Alternative text for image’).
  5. Retrieve the first image object from resources (‘x_image = resources_images[1]’).
  6. Use ’try_set_alternative_text(alt_text, page)’ to assign alt text to the image.
  7. Save the resulting PDF.

    import aspose.pdf as ap
    from io import FileIO
    from os import path

    path_image_file = path.join(self.data_dir, image_file)
    path_outfile = path.join(self.data_dir, "python", outfile)

    document = ap.Document()
    page = document.pages.add()
    page.set_page_size(842,595)

    page.add_image(
        path_image_file,
        ap.Rectangle(0, 0, 842, 595, True),
    )

    resources_images = page.resources.images
    alt_text = "Alternative text for image"
    x_image = resources_images[1]
    result = x_image.try_set_alternative_text(alt_text, page)

    # If set is successful, then get the alternative text for the image
    if (result):
        print ("Text has been added successfuly")
    document.save(path_outfile)