Redigere i formati PDF in Python

Informazioni di redazione sensibili ai documenti PDF nativi e ad alte prestazioni utilizzando Aspose.PDF lato server per le API Python, senza l’uso di software come Microsoft o Adobe PDF.

Come redigere il file PDF usando la libreria Python

Per oscurare il file PDF, useremo l’API Aspose.PDF for .NET che è un’API di manipolazione dei documenti ricca di funzionalità, potente e facile da usare per la piattaforma python-net. Apri il gestore pacchetti NuGet, cerca Aspose.pdf e installa. È inoltre possibile utilizzare il seguente comando dalla console di Gestione pacchetti.

Redigere documenti PDF tramite Python


È necessario Aspose.PDF for .NET per provare il codice nel proprio ambiente.

  1. Carica il PDF con un’istanza di Document.
  2. Creare un oggetto TextFragmentAbsorber con i termini di ricerca come argomento.
  3. Imposta le opzioni di ricerca.
  4. Esegui un ciclo attraverso ogni frammento raccolto per oscurare.
  5. Salva il file PDF.

Redigere i file PDF - Python


    doc = new Document(dataDir + "test.pdf")

    textFragmentAbsorber = TextFragmentAbsorber(searchTerm)
    textSearchOptions = TextSearchOptions(True)
    textFragmentAbsorber.TextSearchOptions = textSearchOptions

    doc.Pages.Accept(textFragmentAbsorber)
    textFragmentCollection = textFragmentAbsorber.TextFragments
    for textFragment in textFragmentCollection:
        page = textFragment.Page
        annotationRectangle = textFragment.Rectangle
        RedactionAnnotation annot = Annotations.RedactionAnnotation(page, annotationRectangle)
        annot.FillColor = Color.Black
        doc.Pages[textFragment.Page.Number].Annotations.Add(annot, true)
        annot.Redact()

        doc.Save(dataDir + "output.pdf")