为了搜索 PDF 文件,我们将使用 Aspose.PDF API,这是一个功能丰富、功能强大且易于使用的文档处理 API。打开 NuGet 包管理器,搜索 Aspose.PDF 并安装。您也可以使用软件包管理器控制台中的以下命令。使用签名对PDF文档进行签名时,您基本上是 “按原样” 确认其内容。因此,之后所做的任何其他更改都会使签名失效,因此,您将知道文档是否被更改。
使用.NET 在不打开的情况下搜索 PDF 文档:
1。使用要查找的文本作为参数创建 textFragmentAbsorber 对象。
1。获取所有提取的文本片段集合。
1。循环浏览每个片段以获取其所有信息。
搜索 PDF 文件
//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);
}