C#iTextSharp字串是否在pdf中
阿新 • • 發佈:2019-02-19
引用using iTextSharp.text.pdf;
/// </summary>
/// <param name="fileName">PDF檔案路徑</param>
/// <param name="key">字串</param>
/// <returns></returns>
public static bool IsExistKeys(string fileName, string key)
{
bool exist = false;
//開啟一個pdf檔案,並建立一個讀取該檔案的PdfReader
PdfReader reader = new PdfReader(fileName);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
ITextExtractionStrategy strategy;
for (int i = 1; i <= reader.NumberOfPages; i++)
{
strategy = parser.ProcessContent<SimpleTextExtractionStrategy>(i, new SimpleTextExtractionStrategy());
exist = strategy.GetResultantText().IndexOf(key)!= -1;
if (exist)
{
break;
}
}
return exist;
}
using iTextSharp.text.pdf.parser;
/// <summary>
/// 判斷字串是否在PDF中/// </summary>
/// <param name="fileName">PDF檔案路徑</param>
/// <param name="key">字串</param>
/// <returns></returns>
public static bool IsExistKeys(string fileName, string key)
{
bool exist = false;
//開啟一個pdf檔案,並建立一個讀取該檔案的PdfReader
PdfReader reader = new PdfReader(fileName);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
ITextExtractionStrategy strategy;
for (int i = 1; i <= reader.NumberOfPages; i++)
{
strategy = parser.ProcessContent<SimpleTextExtractionStrategy>(i, new SimpleTextExtractionStrategy());
exist = strategy.GetResultantText().IndexOf(key)!= -1;
if (exist)
{
break;
}
}
return exist;
}