Redazione del PDF tramite C#

Informazioni sensibili sulla redazione dei documenti PDF. Usa Aspose.PDF per .NET per modificare i documenti PDF a livello di codice

Come redigere il file PDF usando la libreria C#

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 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 C#


È 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 - C#

var inputFile = Path.Combine(dataDir, "input.pdf");
var outputFile = Path.Combine(dataDir, "output.pdf");
var pdfDocument = new Aspose.Pdf.Document(inputFile);
var searchTerm = "Secret";
var textFragmentAbsorber = new Aspose.Pdf.Text.TextFragmentAbsorber(searchTerm);
var textSearchOptions = new Aspose.Pdf.Text.TextSearchOptions(true);
textFragmentAbsorber.TextSearchOptions = textSearchOptions;

pdfDocument.Pages.Accept(textFragmentAbsorber);
var textFragmentCollection = textFragmentAbsorber.TextFragments;
        
foreach (var textFragment in textFragmentCollection)
{
    var page = textFragment.Page;
    var annotationRectangle = textFragment.Rectangle;

    var annot = new Aspose.Pdf.Annotations.RedactionAnnotation(page, annotationRectangle)
    {
        FillColor = Aspose.Pdf.Color.Black
    };
    pdfDocument.Pages[textFragment.Page.Number].Annotations.Add(annot, true);
    annot.Redact();
}
pdfDocument.Save(outputFile);