Rédigez un PDF à l’aide de C#

Informations de rédaction sensibles pour les documents PDF. Utilisez Aspose.PDF pour .NET afin de modifier des documents PDF par programmation

Comment rédiger un fichier PDF à l'aide de la bibliothèque C#

Afin de rédiger un fichier PDF, nous utiliserons l’API Aspose.PDF pour .NET qui est une API de manipulation de documents riche en fonctionnalités, puissante et facile à utiliser pour la plate-forme net. Ouvrez le gestionnaire de packages NuGet, recherchez Aspose.PDF et installez-le. Vous pouvez également utiliser la commande suivante depuis la console Package Manager.

Rédigez des documents PDF via C#


Vous avez besoin de Aspose.PDF pour .NET pour essayer le code dans votre environnement.

  1. Chargez le PDF avec une instance de Document.
  2. Créez un objet TextFragmentAbsorber avec les termes de recherche comme argument.
  3. Définissez les options de recherche.
  4. Parcourez chaque fragment collecté pour le rédiger.
  5. Enregistrez le fichier PDF.

Réviser des fichiers 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);