[英]Search particular word in PDF using iTextSharp
I have a PDF file in my System drive.我的系统驱动器中有一个 PDF 文件。 I want to write a program in C# using iTextSharp to search for a particular word in that PDF.
我想使用 iTextSharp 在 C# 中编写一个程序来搜索该 PDF 中的特定单词。
Say I want to search "StackOverFlow": If the PDF contains the Word "StackOverFlow", it should return true, else it should return false.假设我要搜索“StackOverFlow”:如果 PDF 包含单词“StackOverFlow”,它应该返回 true,否则它应该返回 false。
What I have tried till now is:到目前为止,我尝试过的是:
public string ReadPdfFile(string fileName)
{
StringBuilder text = new StringBuilder();
if (File.Exists(fileName))
{
PdfReader pdfReader = new PdfReader(fileName);
for (int page = 1; page <= pdfReader.NumberOfPages; page++)
{
ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
string currentText = "2154/MUM/2012 A";// PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);
currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));
text.Append(currentText);
}
pdfReader.Close();
}
return text.ToString();
}
The following method works fine.以下方法工作正常。 It gives the list of pages in which the text is found.
它给出了在其中找到文本的页面列表。
public List<int> ReadPdfFile(string fileName, String searthText)
{
List<int> pages = new List<int>();
if (File.Exists(fileName))
{
PdfReader pdfReader = new PdfReader(fileName);
for (int page = 1; page <= pdfReader.NumberOfPages; page++)
{
ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
string currentPageText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);
if (currentPageText.Contains(searthText))
{
pages.Add(page);
}
}
pdfReader.Close();
}
return pages;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.