2017-11-08 11 views
1

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の正規表現や何らかの設定が問題を解決できるかどうかをご提案ください。

+1

私はあなたのコメントを観察し、私たちはこのシナリオをテストするために、その特定の文書を必要とするので、私たちとソースファイルを共有するためにあなたを要求したいとしています。あなたは –

+0

@FarhanRazaがアップロードなど、Googleドライブ、Dropboxのようなサービスをホストしている無料のファイルを使用してファイルを共有することがあります。https://drive.google.com/open?id=1PALgqgXIltrAKcZuZ2ron_I2pD-8Wgqg –

+0

が要求されたファイルを共有していただきありがとうございます。 私はあなたが共有しているデータを扱っていますが、TextFragmentAbsorberは単一のページにまたがってテキストを認識していません。このPDFからどの文字列を抽出したいのですか? 。 注:私はAspose with Developer Evangelistを使用しています。 –

答えて

2

@Jose Francis、 テキストが複数のページにまたがっている場合、TextFragmentAbsorberで問題を確認できました。そのため、ID の調査チケットPDFNET-43671が問題管理システムに記録されています。問題が調査され次第、調査結果をお客様にお知らせします。

ご迷惑をおかけして申し訳ありません。

+0

チケットのステータスを確認するにはどうすればよいですか? –

+2

この問題を調査し、TextFragmentAbsorberのアーキテクチャ上の制限であることを改めてお伝えしたいと思います。以前に記録され優先度の高い問題を考慮すると、近い将来に最終的な解決策を約束することはできません。 さらに、バグ追跡システムは内部の問題管理システムであり、アクセスできないことがあります。 –

1

以前に共有したように、アーキテクチャの制限のため、お客様から報告された問題の早期解決を約束することはできません。ただし、要件に合わせてコードスニペットを変更しました。

考え方は、ドキュメントページの1つに「#START#」から始まるテキストを見つけることです。その後、次のページの「#END#」で終わるテキストを探します。また、その2つのページの間にあるページに配置されているすべてのテキスト断片を処理します(存在する場合)。

private string DeleteMachineReadableCodeUpdated(string inputFilePath) 
    { 
    string 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 absorber = new TextFragmentAbsorber("#START#((.|\r\n)*?)#END#"); 

    // Set text search option to specify regular expression usage 
    TextSearchOptions textSearchOptions = new TextSearchOptions(true); 

    absorber.TextSearchOptions = textSearchOptions; 

    // Accept the absorber for all pages 
    pdfDocument.Pages.Accept(absorber); 

    // Get the extracted text fragments 
    TextFragmentCollection textFragmentCollection = absorber.TextFragments; 

    // If pattern found on one of the pages 
    if (textFragmentCollection.Count > 0) 
    { 
     RemoveTextFromFragmentCollection(textFragmentCollection); 
    } 
    else 
    { 
     // In case nothing was found tries to find by parts 
     string startingPattern = "#START#((.|\r\n)*?)\\z"; 
     string endingPattern = "\\A((.|\r\n)*?)#END#"; 
     bool isStartingPatternFound = false; 
     bool isEndingPatternFound = false; 
     ArrayList fragmentsToRemove = new ArrayList(); 

     foreach (Page page in pdfDocument.Pages) 
     { 
      // If ending pattern was already found - do nothing 
      if (isEndingPatternFound) 
       continue; 

      // If starting pattern was already found - activate textFragmentAbsorber with ending pattern 
      absorber.Phrase = !isStartingPatternFound ? startingPattern : endingPattern; 

      page.Accept(absorber); 
      if (absorber.TextFragments.Count > 0) 
      { 
       // In case something is found - add it to list 
       fragmentsToRemove.AddRange(absorber.TextFragments); 

       if (isStartingPatternFound) 
       { 
        // Both starting and ending patterns found - the document processing 
        isEndingPatternFound = true;       
        RemoveTextFromFragmentCollection(fragmentsToRemove); 
       } 
       else 
       { 
        // Only starting pattern found yet - continue 
        isStartingPatternFound = true;       
       } 
      } 
      else 
      { 
       // In case neither starting nor ending pattern are found on current page 
       // If starting pattern was found previously - get all fragments from the page 
       if (isStartingPatternFound) 
       { 
        absorber.Phrase = String.Empty; 
        page.Accept(absorber); 
        fragmentsToRemove.AddRange(absorber.TextFragments); 
       } 
       // Otherwise do nothing (continue) 
      } 
     } 
    } 

    pdfDocument.Save(outputFilePath); 
} 
finally 
{ 
    if (File.Exists(inputFilePath)) 
     File.Delete(inputFilePath); 
} 

return outputFilePath; 
} 

private void RemoveTextFromFragmentCollection(ICollection fragmentCollection) 
{ 
// Loop through the fragments 
foreach (TextFragment textFragment in fragmentCollection) 
{ 
    textFragment.Text = string.Empty; 
} 
} 

  • このコードは、1つのテキストブロック「#開始#」から開始し、「#エンド#」で終わるが文書内にあると仮定しました。しかし、上記のコードは、いくつかのブロックを処理するように簡単に変更することができます。
  • 中間ページにテキストを処理する代わりに、ページ番号を保存し、保存文書の前にpdfDocument.Pages.Delete(pageNumber)を使用して削除することができます。それは望ましくない場合、「空白の」ページを避けることができます。
関連する問題