Hello,
is it possible to search text only in pdf header/footer not in the whole content? My final goal is to split large document into several pdf documents based on search results found only in header/footer.
Regards
static void Main(string[] args)
{
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("test.pdf");
PdfPageBase page = pdf.Pages[0];
float headerHeight = 105;
float footerHeight = 150;
bool result = SearchText(page, headerHeight, footerHeight, "searchText");
}
public static bool SearchText(PdfPageBase page, float headerHeight, float footerHeight, string searchText)
{
PdfDocument temp = new PdfDocument();
PdfPageBase newPage = temp.Pages.Add(new SizeF(page.Size.Width, page.Size.Height), new PdfMargins(0));
//Draw template
newPage.Canvas.DrawTemplate(page.CreateTemplate(), new PointF(0, 0));
//Draw a white rectangle to cover the content
RectangleF rect = new RectangleF(0, headerHeight, page.Size.Width, page.Size.Height - headerHeight - footerHeight);
newPage.Canvas.DrawRectangle(PdfBrushes.White, rect);
PdfTextFind[] result = newPage.FindText(searchText, TextFindParameter.IgnoreCase).Finds;
return result != null;
} static void Main(string[] args)
{
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("PDF master.pdf");
PdfPageBase page = pdf.Pages[0];
float headerHeight = 50;
float footerHeight = 50;
bool result = SearchText(page, headerHeight, footerHeight, "content");
}
public static bool SearchText(PdfPageBase page, float headerHeight, float footerHeight, string searchText)
{
//Extract headerText
RectangleF rect = new RectangleF(0, 0, page.Size.Width, headerHeight);
string headerText = page.ExtractText(rect);
//Extract footerText
rect = new RectangleF(0, page.Size.Height - footerHeight, page.Size.Width, footerHeight);
string footerText = page.ExtractText(rect);
if (headerText.Contains(searchText))
{
return true;
}
if (footerText.Contains(searchText))
{
return true;
}
return false;
}