PDF文書からテキストを削除する必要があります。私は目的のためにAsposeを使用しています は現在TextFragmentAbsorber
を使用しています。Aspose.PDFライブラリを使用してPDF文書からテキストを削除しますか?
参考までに、他のサードパーティのライブラリを使用することはできません。以下は
は、私が使用していますコードです:
private string DeleteMachineReadableCode(string inputFilePath)
{
var outputFilePath = Path.Combine(Path.GetTempPath(), string.Format(@"{0}.pdf", Guid.NewGuid()));
try
{
// Open document
Document pdfDocument = new Document(inputFilePath);
// Create TextAbsorber object to find all the phrases matching the regular expression
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("#START#((.|\r\n)*?)#END#");
// Set text search option to specify regular expression usage
TextSearchOptions textSearchOptions = new TextSearchOptions(true);
textFragmentAbsorber.TextSearchOptions = textSearchOptions;
// Accept the absorber for all pages
pdfDocument.Pages.Accept(textFragmentAbsorber);
// Get the extracted text fragments
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
// Loop through the fragments
foreach (TextFragment textFragment in textFragmentCollection)
{
// Update text and other properties
textFragment.Text = string.Empty;
// Set to an instance of an object.
textFragment.TextState.Font = FontRepository.FindFont("Verdana");
textFragment.TextState.FontSize = 1;
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.White);
textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.White);
}
pdfDocument.Save(outputFilePath);
}
finally
{
if (File.Exists(inputFilePath))
File.Delete(inputFilePath);
}
return outputFilePath;
}
私は削除すべきコンテンツは、単一のページ上にある場合は、コンテンツを交換することができますよ。 私の問題は、テキストが複数のページにわたる場合、TextFragmentAbsorberは前述の正規表現パターン( "#START#((.|\r\n)*?)#END#
")のテキストを認識しないということです。
Asposeの正規表現や何らかの設定が問題を解決できるかどうかをご提案ください。
私はあなたのコメントを観察し、私たちはこのシナリオをテストするために、その特定の文書を必要とするので、私たちとソースファイルを共有するためにあなたを要求したいとしています。あなたは –
@FarhanRazaがアップロードなど、Googleドライブ、Dropboxのようなサービスをホストしている無料のファイルを使用してファイルを共有することがあります。https://drive.google.com/open?id=1PALgqgXIltrAKcZuZ2ron_I2pD-8Wgqg –
が要求されたファイルを共有していただきありがとうございます。 私はあなたが共有しているデータを扱っていますが、TextFragmentAbsorberは単一のページにまたがってテキストを認識していません。このPDFからどの文字列を抽出したいのですか? 。 注:私はAspose with Developer Evangelistを使用しています。 –