static void SearchAndMerge(string srcSplitPdfPath, string dstFinalPdfPath)
{
// Regex search strings
var searchText = @"((NH)[A-Z0-9]{5})";
var reg = new Regex(searchText);
foreach (var file in Directory.EnumerateFiles(srcSplitPdfPath, "*.pdf"))
{
using (var pdfReader = new PdfReader(file))
{
for (var page = 1; page <= pdfReader.NumberOfPages; page++)
{
//Get the text from the page
var currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, new SimpleTextExtractionStrategy());
//currentText.IndexOf("", StringComparison.InvariantCultureIgnoreCase);
currentText.Contains("");
//Match our pattern against the extracted text
//var matches = reg.Matches(currentText);
var match = reg.Match(currentText);
//This is the file path that we want to target
var destFile = Path.Combine(dstFinalPdfPath, match.ToString() + ".pdf");
//If the file doesn't already exist then just copy the file and move on
if (!File.Exists(destFile))
{
System.IO.File.Copy(file, destFile);
continue;
}
using (var ms = new MemoryStream())
{
//Use a wrapper helper provided by iText
var cc = new PdfConcatenate(ms);
//Open for writing
cc.Open();
//Import the existing file
using (var subReader = new PdfReader(destFile))
{
cc.AddPages(subReader);
}
using (var subReader = new PdfReader(file))
{
cc.AddPages(subReader);
}
//Close for writing
cc.Close();
//Erase our exisiting file
File.Delete(destFile);
//Write our new file
File.WriteAllBytes(destFile, ms.ToArray());
}
}
}
}
}