Add Image to PDF using Python
Contents
[
Hide
]
Add Image in an Existing PDF File
The following code snippet shows how to add image in the PDF file.
- Load the input PDF file.
- Specify the page number on which the picture will be placed.
- To define the position of the image on the page call the Page class add_image method.
- Call the Document class save() method.
import aspose.pdf as ap
# Open document
document = ap.Document(input_file)
document.pages[1].add_image(image_file, ap.Rectangle(20, 730, 120, 830, True))
document.save(output_pdf)
Add Image in an Existing PDF File (Facades)
There is also an alternative, easier way to add a Image to a PDF file. You can use AddImage method of the PdfFileMend class. The add_image() method requires the image to be added, the page number at which the image needs to be added and the coordinate information. After that, save the updated PDF file, and close the PdfFileMend object using close() method. The following code snippet shows you how to add image in an existing PDF file.
import aspose.pdf as ap
# Open document
mender = ap.facades.PdfFileMend()
# Create PdfFileMend object to add text
mender.bind_pdf(input_file)
# Add image in the PDF file
mender.add_image(image_file, 1, 100.0, 600.0, 200.0, 700.0)
# Save changes
mender.save(output_pdf)
# Close PdfFileMend object
mender.close()