在 C# 中搜索 PDF 文件

使用伺服器端 Aspose 進行本機和高性能 PDF 文件搜索.PDF C# API,而無需使用任何軟體(如微軟或 Adobe PDF)。

如何使用C#搜索PDF檔

為了搜索PDF檔,我們將使用[Aspose.PDF用於.NET](https://products.aspose.com/pdf/net)API,這是一個功能豐富,功能強大且易於使用的文檔操作API,適用於 net 平臺。打開 [NuGet](https://www.nuget.org/packages/aspose.pdf) 包管理器,搜索“.PDF”並安裝。您也可以從程式包管理器主控台使用以下命令。

Package Manager Console

PM > Install-Package Aspose.PDF

通過C#搜索 PDF 檔


您需要 [Aspose.PDF用於.NET](https://releases.aspose.com/pdf/net) 在您的環境中嘗試代碼。

  1. 載入包含文件實例的 PDF。 創建文本碎片用文本作為參數查找的吸收器物件。 獲取所有提取的文字片段集合。 1.迴圈遍歷每個片段以獲取其所有資訊。

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