Add Image to PDF using Python
Contents
[
Hide
]
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.
- Load the PDF document with ‘ap.Document’.
- Select the target page ‘(document.pages[1]’ - the first page).
- 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).
- 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:
- Create a new blank ‘Document’.
- Add a page and set its size (842 × 595 - landscape A4).
- Access the page’s image resources (page.resources.images).
- 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.
- Define a rectangle that maintains the aspect ratio of the image:
- 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.
- Add the operator sequence to ‘page.contents’.
- 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).
- Create a new ‘Document’ and add a page (842 × 595, landscape A4).
- Place the image on the page using ‘page.add_image()’ with a rectangle that spans the full page.
- Access the page’s image resources (‘page.resources.images’).
- Define an alternative text string (e.g., ‘Alternative text for image’).
- Retrieve the first image object from resources (‘x_image = resources_images[1]’).
- Use ’try_set_alternative_text(alt_text, page)’ to assign alt text to the image.
- 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)