4

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 usage of iTextSharp when 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)));
                text.Append(currentText);
            }
            pdfReader.Close();
        }
        return text.ToString();

But how can I only restrict it to a specific location? I am open to use anything, even OCR technique as it might happen in the future that some files will be images(but not neccessary at this time). This project is only for me, so no commercial use.

Thanks!

Robert J.
  • 2,631
  • 8
  • 32
  • 59

2 Answers2

10

You are using a SimpleTextExtractionStrategy instead of a LocationTextExtractionStrategy. Please read the official documentation and the accompanying examples (Java / C#). If rect is a rectangle based on the coordinates of your address, you need:

RenderFilter[] filter = {new RegionTextRenderFilter(rect)};
ITextExtractionStrategy strategy;
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= reader.NumberOfPages; i++) {
    strategy = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), filter);
    sb.AppendLine(PdfTextExtractor.GetTextFromPage(reader, i, strategy));
}

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:

Rectangle mediabox = reader.GetPageSize(pagenum);

The coordinate of the lower-left corner is x = mediabox.Left and y = mediabox.Bottom; the coordinate of the upper-right corner is x = mediabox.Right and y = mediabox.Top.

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.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thank you, that's what I was looking for! One question though; how cna I set the region? I though it is measured in pixels, but when I input (what I believed was) correct values I get no result, as string is located in a different position. – Robert J. Jun 12 '14 at 14:36
  • @BrunoLowagie `RegionTextRenderFilter` and `FilteredTextRenderListener` no longer appear to be a part of the .net assemblies. – chriszumberge Sep 24 '19 at 14:18
0

One of the things you can do is have the address fields be PDF Form fields, you can then extract the information by accessing the form field directly.

aydjay
  • 858
  • 11
  • 25
  • This would be a useful thing, if I would be the one who generated the documents. I did not create layout, nor I can change it unfortunately, therefore I cannot go with this option. – Robert J. Jun 12 '14 at 13:15
  • Cool, just thought I would bring it to your attention. – aydjay Jun 12 '14 at 13:47