Working with Backgrounds as Artifacts with Python
Contents
[
Hide
]
Background images can be used to add a watermark, or other subtle design, to documents. In Aspose.PDF for Python via .NET, each PDF document is a collection of pages and each page contains a collection of artifacts. The BackgroundArtifact class can be used to add a background image to a page object.
The following code snippet shows how to add a background image to PDF pages using the BackgroundArtifact object with Python.
import aspose.pdf as ap
import io
def add_background_image(input_image_file, output_pdf):
# Create a new PDF document
document = ap.Document()
# Add a blank page to the document
page = document.pages.add()
# Create a BackgroundArtifact object
background = ap.BackgroundArtifact()
# Load the image as a binary stream
with open(input_image_file, "rb") as image_stream:
background.background_image = image_stream
# Add the background artifact to the page
page.artifacts.append(background)
# Save the resulting PDF
document.save(output_pdf)