Ajouter des arrière-plans PDF en Python
Contents
[
Hide
]
Utiliser BackgroundArtifact pour placer une image ou une couleur unie derrière le contenu d’une page PDF. Ces exemples modifient la première page.
Ajouter une image d’arrière-plan à un PDF
Définissez background_image à partir d’un flux d’image et ajoutez l’artefact à la page.
from io import FileIO
import aspose.pdf as ap
def add_background_image_to_pdf(infile, imagefile, outfile, opacity=1.0):
"""Add a background image to a PDF document as an artifact."""
with ap.Document(infile) as document:
artifact = ap.BackgroundArtifact()
artifact.background_image = FileIO(imagefile, "rb")
artifact.opacity = opacity
document.pages[1].artifacts.append(artifact)
document.save(outfile)
Ajouter une image d’arrière-plan avec opacité à un PDF
Passez à la même fonction une valeur d’opacité comprise entre 0 et 1. Utilisez 0.5 pour obtenir une image semi-transparente.
add_background_image_to_pdf("input.pdf", "background.png", "output.pdf", opacity=0.5)
Ajouter une couleur d’arrière-plan à un PDF
Définissez background_color sur l’artefact pour appliquer une couleur unie.
import aspose.pdf as ap
def add_background_color_to_pdf(infile, outfile):
"""Add a solid color background to a PDF document as an artifact."""
with ap.Document(infile) as document:
artifact = ap.BackgroundArtifact()
artifact.background_color = ap.Color.dark_khaki
document.pages[1].artifacts.append(artifact)
document.save(outfile)