Currently, I use this code to extract text from a Rectangle (area).
public static class ReaderExtensions
{
public static string ExtractText(this PdfPage page, Rectangle rect)
{
var filter = new IEventFilter[1];
filter[0] = new TextRegionEventFilter(rect);
var filteredTextEventListener = new FilteredTextEventListener(new LocationTextExtractionStrategy(), filter);
var str = PdfTextExtractor.GetTextFromPage(page, filteredTextEventListener);
return str;
}
}
It works, but I don't know if it's the best way to do it.
Also, I wonder if the GetTextFromPage could be improved by the iText team to increase its performance, since I'm processing hundreds of pages in big PDFs and it usually takes more than 10 minutes to do it using my current configuration.
EDIT:
From the comments: It seems that iText can extract the text of multiple rectangles on the same page in one pass, something that can improve the performance (batched operations tend to be more efficient), but how?
MORE DETAILS!
My goal is to extract data from a PDF with multiple pages. Each page has the same layout: a table with rows and columns.
Currently, I'm using the method above to extract the text of each rectangle. But, as you see, the extraction isn't batched. It's only a rectangle at a time. How could I extract all the rectangles of a page in a single pass?
LocationTextExtractionStrategy. Otherwise you do. Etc. pp. - mklGetResultantText(TextChunkFilter)overload; using that one needed to parse the page but once and then could retrieve the text from arbitrary parts of the page from this strategy. This option seems to have been dropped in the port to iText 7. I think one can add that feature again but that requires a bit more time than I estimated. I'll look into that later. - mkl