引用using iTextSharp.text.pdf;

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;

}