Old question, but here is an example.
Note: c# 7.0+ is required to use IS new local variable assignment.
Note: This example uses PDFSharp installed from Package Manager.
"Install-Package PdfSharp -Version 1.50.5147"
Note: For my requirements, I only needed to search the first page of my PDFs, update if
needed.
using (PdfDocument inputDocument = PdfReader.Open(filePath, PdfDocumentOpenMode.Import))
{
if (searchPDFPage(ContentReader.ReadContent(inputDocument.Pages[0]), searchText))
{
// match found.
}
}
This code looks for a cString that starts with a pound sign, the OP would need to use a Contains string function.
private bool searchPDFPage(CObject cObject, string searchText)
{
if (cObject is COperator cOperator)
{
if (cOperator.OpCode.Name == OpCodeName.Tj.ToString() ||
cOperator.OpCode.Name == OpCodeName.TJ.ToString())
{
foreach (var cOperand in cOperator.Operands)
{
if (searchPDFPage(cOperand, searchText))
{
return true;
}
}
}
}
else if (cObject is CSequence cSequence)
{
foreach (var element in cSequence)
{
if (searchPDFPage(element, searchText))
{
return true;
}
}
}
else if (cObject is CString cString)
{
if (cString.Value.StartsWith("#"))
{
if (cString.Value.Substring(2) == searchText)
{
return true;
}
}
}
return false;
}
Credit: This example was modified based on this answer:
C# Extract text from PDF using PdfSharp