Add Image to PDF using Python
Contents
[
Hide
]
Add Image in an Existing PDF File
This example shows how to place an image at a fixed position on an existing PDF page using Aspose.PDF for Python via .NET.
Use these examples from this page when you need to place logos, photos, or other graphics at fixed coordinates inside an existing PDF layout.
- Load an existing PDF with
ap.Document(infile). - Select the target page (
document.pages[1]for the first page). - Call
page.add_image()with:- The image file path.
- A
Rectangledefining placement coordinates.
- Save the updated PDF.
import aspose.pdf as ap
def add_image(infile, image_file, outfile):
document = ap.Document(infile)
page = document.pages[1]
page.add_image(image_file, ap.Rectangle(20, 730, 120, 830, True))
document.save(outfile)
Add an Image Using Operators
This approach adds an image with low-level PDF operators instead of the high-level add_image() helper.
- Create a new
Documentand add a page. - Add the image to page resources (
page.resources.images). - Create transformation operators (
GSave,ConcatenateMatrix,Do,GRestore). - Add operators to page contents.
- Save the resulting PDF.
import aspose.pdf as ap
from io import FileIO
def add_image_using_operators(image_file, outfile):
document = ap.Document()
page = document.pages.add()
page.set_page_size(842, 595)
resources_images = page.resources.images
with FileIO(image_file, "rb") as image_stream:
image_id = resources_images.add(image_stream)
rectangle = ap.Rectangle(0, 0, page.media_box.width, page.media_box.height, True)
operators = [
ap.operators.GSave(),
ap.operators.ConcatenateMatrix(
ap.Matrix(
rectangle.urx - rectangle.llx,
0,
0,
rectangle.ury - rectangle.lly,
rectangle.llx,
rectangle.lly,
)
),
ap.operators.Do(image_id),
ap.operators.GRestore(),
]
page.contents.add(operators)
document.save(outfile)
Add Image with Alternative Text
This example adds an image and assigns alternative text for accessibility.
- Create a new
Documentand add a page. - Add the image to the page with
page.add_image(). - Get image resources from
page.resources.images. - Set alt text using
try_set_alternative_text(). - Save the resulting PDF.
import aspose.pdf as ap
def add_image_set_alternative_text(image_file, outfile):
document = ap.Document()
page = document.pages.add()
page.set_page_size(842, 595)
page.add_image(image_file, ap.Rectangle(0, 0, 842, 595, True))
resources_images = page.resources.images
x_image = resources_images[1]
result = x_image.try_set_alternative_text("Alternative text for image", page)
if result:
print("Alternative text has been added successfully")
document.save(outfile)
Add an Image to a PDF with Flate Compression
This example embeds an image using ImageFilterType.FLATE compression.
- Create a new
Documentand add a page. - Add the image to page resources with Flate compression.
- Use matrix operators to place and draw the image.
- Save the document.
import aspose.pdf as ap
from io import FileIO
def add_image_to_pdf_with_flate_compression(image_file, outfile):
document = ap.Document()
page = document.pages.add()
resources_images = page.resources.images
with FileIO(image_file, "rb") as image_stream:
image_id = resources_images.add(image_stream, ap.ImageFilterType.FLATE)
rectangle = ap.Rectangle(0, 0, 600, 600, True)
matrix = ap.Matrix(
rectangle.urx - rectangle.llx,
0,
0,
rectangle.ury - rectangle.lly,
rectangle.llx,
rectangle.lly,
)
page.contents.add([ap.operators.GSave()])
page.contents.add([ap.operators.ConcatenateMatrix(matrix)])
page.contents.add([ap.operators.Do(image_id)])
page.contents.add([ap.operators.GRestore()])
document.save(outfile)