在 PDF 文件中搜索以查找字符串
Search within PDF Files to Find a String
我需要在 pdf 文件中搜索以找到 string.I 知道 itextsharp 具有此功能,我可以使用此代码
public bool SearchPdfFile(string fileName, String searchText)
{
/* technically speaking this should not happen, since "you" are calling it
therefore this should be handled critically
if (!File.Exists(fileName)) return false; //original workflow
*/
if (!File.Exists(fileName))
throw new FileNotFoundException("File not found", fileName);
using (PdfReader reader = new PdfReader(fileName))
{
var strategy = new SimpleTextExtractionStrategy();
for (int page = 1; page <= pdfReader.NumberOfPages; page++)
{
var currentPageText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);
if (currentPageText.Contains(searchText))
return true;
}
}
return false;
}
但是我在 LGPL/MPL 许可证(版本 3.0/4.0)下使用 itext,如果我在 AGPL 下免费制作我自己的软件,则较新的版本 5.0 是免费的。 class SimpleTextExtractionStrategy 在此版本 itext.Is 中未定义,是否有其他方法可以使用旧版本的 itext 执行此操作?
PDF小丑。
一个愚蠢的名字,但它是一个非常详细和灵活的 PDF 库。我以前用过。它在 LGPL 下是免费的。 http://pdfclown.org/about/#TheLicense
从 PDFClown 网站修改的示例(他们的示例是 java)
File file = new File(myFilePath);
// Define the text pattern to look for!
String textRegEx = "rabbit";
Pattern pattern = Pattern.compile(textRegEx, Pattern.CASE_INSENSITIVE);
// Instantiate the extractor!
TextExtractor textExtractor = new TextExtractor(true, true);
for(final Page page : file.getDocument().getPages())
{
// Extract the page text!
Map<Rectangle2D,List<ITextString>> textStrings = textExtractor.extract(page);
// Find the text pattern matches!
final Matcher matcher = pattern.matcher(TextExtractor.toString(textStrings));
}
针对 C# 进行了更新
File file = new File(myFilePath);
// Define the text pattern to look for!
var pattern = new Regex("rabbit", RegexOptions.IgnoreCase);
// Instantiate the extractor!
TextExtractor textExtractor = new TextExtractor(true, true);
foreach (var page in file.Document.Pages)
{
// Extract the page text!
var textStrings = textExtractor.Extract(page);
// Find the text pattern matches!
var matches = pattern.Matches(TextExtractor.ToString(textStrings));
}
我需要在 pdf 文件中搜索以找到 string.I 知道 itextsharp 具有此功能,我可以使用此代码
public bool SearchPdfFile(string fileName, String searchText)
{
/* technically speaking this should not happen, since "you" are calling it
therefore this should be handled critically
if (!File.Exists(fileName)) return false; //original workflow
*/
if (!File.Exists(fileName))
throw new FileNotFoundException("File not found", fileName);
using (PdfReader reader = new PdfReader(fileName))
{
var strategy = new SimpleTextExtractionStrategy();
for (int page = 1; page <= pdfReader.NumberOfPages; page++)
{
var currentPageText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);
if (currentPageText.Contains(searchText))
return true;
}
}
return false;
}
但是我在 LGPL/MPL 许可证(版本 3.0/4.0)下使用 itext,如果我在 AGPL 下免费制作我自己的软件,则较新的版本 5.0 是免费的。 class SimpleTextExtractionStrategy 在此版本 itext.Is 中未定义,是否有其他方法可以使用旧版本的 itext 执行此操作?
PDF小丑。 一个愚蠢的名字,但它是一个非常详细和灵活的 PDF 库。我以前用过。它在 LGPL 下是免费的。 http://pdfclown.org/about/#TheLicense
从 PDFClown 网站修改的示例(他们的示例是 java)
File file = new File(myFilePath);
// Define the text pattern to look for!
String textRegEx = "rabbit";
Pattern pattern = Pattern.compile(textRegEx, Pattern.CASE_INSENSITIVE);
// Instantiate the extractor!
TextExtractor textExtractor = new TextExtractor(true, true);
for(final Page page : file.getDocument().getPages())
{
// Extract the page text!
Map<Rectangle2D,List<ITextString>> textStrings = textExtractor.extract(page);
// Find the text pattern matches!
final Matcher matcher = pattern.matcher(TextExtractor.toString(textStrings));
}
针对 C# 进行了更新
File file = new File(myFilePath);
// Define the text pattern to look for!
var pattern = new Regex("rabbit", RegexOptions.IgnoreCase);
// Instantiate the extractor!
TextExtractor textExtractor = new TextExtractor(true, true);
foreach (var page in file.Document.Pages)
{
// Extract the page text!
var textStrings = textExtractor.Extract(page);
// Find the text pattern matches!
var matches = pattern.Matches(TextExtractor.ToString(textStrings));
}