C# を使って PDF を編集

PDF ドキュメントの機密編集情報。プログラムで PDF ドキュメントを変更するには、.NET の Aspose.PDF を使用してください

C# ライブラリを使ってPDFファイルを編集する方法

PDF ファイルを編集するために、net プラットフォーム用の機能豊富で強力で使いやすいドキュメント操作 API である Aspose.PDF for .NET API を使用します。NuGet パッケージマネージャーを開き、aspose.pdf を検索してインストールします。パッケージマネージャーコンソールから次のコマンドを使用することもできます。

C# 経由でPDFドキュメントを墨消し


お使いの環境でコードを試すには Aspose.PDF for .NET が必要です。

  1. PDF を Document のインスタンスとともに読み込みます。
  2. 検索用語を引数として TextFragmentAbsorber オブジェクトを作成します。
  3. [検索オプション] を設定します。
  4. 各フラグメントコレクションをループして編集します。
  5. PDF ファイルを保存します。

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);