Busca archivos PDF en C#

Búsqueda de documentos PDF nativos y de alto rendimiento mediante Aspose.PDF del lado del servidor para C# API, sin el uso de ningún software como Microsoft o Adobe PDF.

Cómo buscar un archivo PDF con C#

Para buscar archivos PDF, usaremos la API Aspose.PDF for .NET, que es una API de manipulación de documentos rica en funciones, potente y fácil de usar para la plataforma net. Abra el administrador de paquetes NuGet, busque Aspose.pdf e instálelo. También puede usar el siguiente comando desde la consola de Package Manager.

Package Manager Console

PM > Install-Package Aspose.PDF

Buscar archivo PDF a través de C#


Necesita Aspose.PDF for .NET para probar el código en su entorno.

  1. Cargue el PDF con una instancia de Document.
  2. Cree el objeto TextFragmentAbsorber con texto para encontrarlo como parámetro.
  3. Obtenga toda la colección de fragmentos de texto extraídos.
  4. Recorre cada fragmento para obtener toda su información.

Buscar archivos PDF: C#


//Search Text from All the Pages of PDF Document
Document pdfDocument = new Document("SearchAndGetTextFromAll.pdf");

// Create TextAbsorber object to find all instances of the input search phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("text");

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

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

// Loop through the fragments
foreach (TextFragment textFragment in textFragmentCollection)
{

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