Redaguj PDF przez C#

Informacje o poufnej redakcji dokumentu PDF. Użyj pliku Aspose.PDF dla .NET, aby programowo modyfikować dokumenty PDF

Jak zredagować plik PDF za pomocą biblioteki C#

Aby zredagować plik PDF, użyjemy Aspose.PDF for .NET API, który jest bogatym w funkcje, potężnym i łatwym w użyciu interfejsem API do manipulacji dokumentami dla platformy net. Otwórz menedżera pakietów NuGet, wyszukaj Aspose.pdf i zainstaluj. Można również użyć następującego polecenia z konsoli Menedżera pakietów.

Redagowanie dokumentów PDF za pośrednictwem C#


Potrzebujesz Aspose.PDF for .NET, aby wypróbować kod w swoim środowisku.

  1. Załaduj plik PDF z wystąpieniem dokumentu.
  2. Tworzenie obiektu TextFragmentAbsorber z wyszukiwanych haseł jako argument.
  3. Ustaw opcje wyszukiwania.
  4. Pętla przez każdy fragment zbierać do redagowania.
  5. Zapisz plik PDF.

Redagowanie plików 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);