Ajouter des filigranes au PDF en Python

Ajouter un artefact de filigrane à un PDF Document en utilisant Aspose.PDF for Python via .NET. Un filigrane est une superposition visuelle appliquée aux pages à des fins de marque, de sécurité ou d’information. L’exemple montre comment configurer TextState apparence, positionnement avec HorizontalAlignment et VerticalAlignment, rotation, et transparence avant d’appliquer le filigrane à un Page.

Extraire les filigranes du PDF

  1. Chargez le document PDF.
  2. Accéder aux artefacts de page.
  3. Filtrer les artefacts de filigrane.
  4. Collecter les éléments de filigrane.
  5. Extraire les propriétés du filigrane.
  6. Informations de filigrane en sortie.
from os import path
import sys
import aspose.pdf as ap

def extract_watermark_from_pdf(infile):
    with ap.Document(infile) as document:
        watermarks = [
            artifact
            for artifact in document.pages[1].artifacts
            if artifact.type == ap.Artifact.ArtifactType.PAGINATION
            and artifact.subtype == ap.Artifact.ArtifactSubtype.WATERMARK
        ]

        for watermark in watermarks:
            print(f"{watermark.text} {watermark.rectangle}")

Ajouter un filigrane au PDF

Ajouter un filigrane de texte à un document PDF à l’aide d’Aspose.PDF for Python :

  1. Chargez le document PDF.
  2. Créer un état de texte.
  3. Créer un artefact de filigrane.
  4. Définir le texte et le style du filigrane.
  5. Configurer le positionnement et la rotation.
  6. Définir l’opacité et le comportement d’arrière-plan.
  7. Attacher le filigrane à une page.
  8. Enregistrez le document mis à jour.
from os import path
import sys
import aspose.pdf as ap

def add_watermark_artifact(infile, outfile):
    with ap.Document(infile) as document:
        text_state = ap.text.TextState()
        text_state.font_size = 72
        text_state.foreground_color = ap.Color.blue_violet
        text_state.font_style = ap.text.FontStyles.BOLD
        text_state.font = ap.text.FontRepository.find_font("Arial")

        watermark = ap.WatermarkArtifact()
        watermark.set_text_and_state("WATERMARK", text_state)
        watermark.artifact_horizontal_alignment = ap.HorizontalAlignment.CENTER
        watermark.artifact_vertical_alignment = ap.VerticalAlignment.CENTER
        watermark.rotation = 60
        watermark.opacity = 0.2
        watermark.is_background = True

        document.pages[1].artifacts.append(watermark)
        document.save(outfile)

Supprimer les artefacts de filigrane d’une page PDF

Supprimer les artefacts de filigrane d’une page spécifique d’un document PDF à l’aide de l’API Aspose.PDF for Python. L’approche cible les éléments de filigrane stockés comme artefacts de page (plus précisément ceux classés comme sous-types de filigrane de pagination), les parcourt et les supprime avant d’enregistrer le document mis à jour.

  1. Chargez le document PDF.
  2. Accéder aux artefacts de page.
  3. Filtrer les artefacts de filigrane.
  4. Supprimer les artefacts de filigrane.
  5. Enregistrez le document mis à jour.
from os import path
import sys
import aspose.pdf as ap

def delete_watermark_artifact(infile, outfile):
    with ap.Document(infile) as document:
        watermarks = [
            artifact
            for artifact in document.pages[1].artifacts
            if artifact.type == ap.Artifact.ArtifactType.PAGINATION
            and artifact.subtype == ap.Artifact.ArtifactSubtype.WATERMARK
        ]

        for watermark in watermarks:
            document.pages[1].artifacts.delete(watermark)

        document.save(outfile)

Sujets d’artefacts associés