Cerca PDF tramite C#

Ricerca avanzata di documenti PDF. Usa Aspose.PDF per .NET per modificare i documenti PDF a livello di codice

Come cercare file PDF usando C#

Per cercare file PDF, useremo l’API Aspose.PDF for .NET che è un’API di manipolazione dei documenti ricca di funzionalità, potente e facile da usare per la piattaforma net. Apri il gestore pacchetti NuGet, cerca Aspose.pdf e installa. È inoltre possibile utilizzare il seguente comando dalla console di Gestione pacchetti.

Package Manager Console

PM > Install-Package Aspose.PDF

Cerca file PDF tramite C#


È necessario Aspose.PDF for .NET per provare il codice nel proprio ambiente.

  1. Carica il PDF con un’istanza di Document.
  2. Creare un oggetto TextFragmentAbsorber con testo da trovare come parametro.
  3. Ottieni tutta la raccolta di frammenti di testo estratti.
  4. Esegui un ciclo di ogni frammento per ottenere tutte le sue informazioni.

Cerca file PDF - C#

var inputFile = Path.Combine(dataDir, "SearchAndGetTextFromAll.pdf");
var pdfDocument = new Aspose.Pdf.Document(inputFile);
var searchText = "text";
var textFragmentAbsorber = new Aspose.Pdf.Text.TextFragmentAbsorber(searchText);

pdfDocument.Pages.Accept(textFragmentAbsorber);

foreach (var textFragment in textFragmentAbsorber.TextFragments)
{
    Console.WriteLine("Text : {0} ", textFragment.Text);
    Console.WriteLine("Position : {0} ", textFragment.Position);
    Console.WriteLine("XIndent : {0} ", textFragment.Position.XIndent);
    Console.WriteLine("YIndent : {0} ", textFragment.Position.YIndent);
    Console.WriteLine("Font - Name : {0}", textFragment.TextState.Font.FontName);
    Console.WriteLine("Font - IsAccessible : {0} ", textFragment.TextState.Font.IsAccessible);
    Console.WriteLine("Font - IsEmbedded : {0} ", textFragment.TextState.Font.IsEmbedded);
    Console.WriteLine("Font - IsSubset : {0} ", textFragment.TextState.Font.IsSubset);
    Console.WriteLine("Font Size : {0} ", textFragment.TextState.FontSize);
    Console.WriteLine("Foreground Color : {0} ", textFragment.TextState.ForegroundColor);
}