I have a problem using iTextSharp when reading data from PDF File. What I want to achieve is to read only specific part of PDF page (I want to only retrieve Address Information, which is located at constant position). I have seen using iTextSharp for reading all pages such as following:

StringBuilder text = new StringBuilder();
if (File.Exists(fileName)) {
    PdfReader pdfReader = new PdfReader(fileName);
    for (int page = 1; page <= pdfReader.NumberOfPages; page++) {
        ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
        string currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);
        currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(
            Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));
return text.ToString();

But how can I only restrict it to a specific location?

Posted on StackOverflow on Jun 12, 2014 by Robert J.

You are using a SimpleTextExtractionStrategy instead of a LocationTextExtractionStrategy. If rect is a rectangle based on the coordinates of your address, in iText 7 you need:

public String extractTextFromDoc() throws IOException { StringBuilder builder = new StringBuilder(); PdfDocument pdfDoc = new PdfDocument(new PdfReader(new ByteArrayInputStream(createSmallDoc()))); Rectangle rect = new Rectangle(100, 100, 200, 200); TextRegionEventFilter regionFilter = new TextRegionEventFilter(rect); for (int page = 1; page <= pdfDoc.getNumberOfPages(); page++) { ITextExtractionStrategy strategy = new FilteredTextEventListener(new LocationTextExtractionStrategy(), regionFilter); String str = PdfTextExtractor.getTextFromPage(pdfDoc.getPage(page), strategy) + "\n\n"; builder.append(str); } return builder.toString(); }
public string ExtractTextFromDoc() { PdfDocument pdf = new PdfDocument(new PdfReader(new MemoryStream(CreateSmallDoc()))); Rectangle rect = new Rectangle(100, 100, 200, 200); TextRegionEventFilter regionFilter = new TextRegionEventFilter(rect); StringBuilder builder = new StringBuilder(); for (int page = 1; page <= pdf.GetNumberOfPages(); page++) { ITextExtractionStrategy strat = new FilteredTextEventListener(new LocationTextExtractionStrategy(), regionFilter); string str = PdfTextExtractor.GetTextFromPage(pdf.GetPage(page), strat) + "\n\n"; builder.Append(str); } return builder.ToString(); } }

Now you'll get all the text snippets that intersect with the rect (so part of the text may be outside rect, iText doesn't cut text snippets in pieces).

Note that you can get the MediaBox of a page using:

PageSize mediabox = pdfDoc.getPage(pagenum).getPageSize();

The coordinate of the lower-left corner is x = mediabox.getLeft() and y = mediabox.getBottom() the coordinate of the upper-right corner is x = mediabox.getRight() and y = mediabox.getTop().

The values of x increase from left to right; the values of y increase from bottom to top. The unit of the measurement system in PDF is called "user unit". By default one user unit coincides with one point (this can change, but you won't find many PDFs with a different UserUnit value). In normal circumstances, 72 user units = 1 inch.

Click this link if you want to see how to answer this question in iText 5.