6

I have to extract text from a pdf doc within a specific rectangular region. The work-flow is as following. First of all pdf is converted to an jpg image. Then user draws selection rectangle on top of the picture. Then I somehow need to extract all text from pdf doc within that selection region. Any suggestions what freeware pdf libs accessible from C# to use?

Singleton
  • 3,701
  • 3
  • 24
  • 37
mmierins
  • 3,674
  • 4
  • 21
  • 25
  • https://stackoverflow.com/q/20606467/1271037 – dovid Aug 13 '17 at 10:31
  • Possible duplicate of [Get text occurrences contained in a specified area with iTextSharp](https://stackoverflow.com/questions/20606467/get-text-occurrences-contained-in-a-specified-area-with-itextsharp) – bfontaine Aug 21 '17 at 10:11

4 Answers4

7

this code will perfectly extract pdf data on the basis of rectangular coordinates using itextsharp

    List<string> linestringlist = new List<string>();
    PdfReader reader = new PdfReader(pdfFilename);
    iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(coordinate1, coordinate2, coordinate3, coordinate4);
    RenderFilter[] renderFilter = new RenderFilter[1];
    renderFilter[0] = new RegionTextRenderFilter(rect);
    ITextExtractionStrategy textExtractionStrategy = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), renderFilter);
    string text = PdfTextExtractor.GetTextFromPage(reader, 1, textExtractionStrategy);
shailendra
  • 165
  • 2
  • 3
  • 8
4

I agree, OCR is not the approach to use here. You need a PDF library that can extract the text along with the bounding box coordinates.

QuickPDF is a commercial library (www.quickpdf.com) that can extract the required information for a very reasonable price of $249. http://www.quickpdflibrary.com/help/quickpdf/DAExtractPageText.php is the function you are looking for. This will extract the text for the whole page and then you would need to use simple Point and/or Rectangle functions to limit the text to your selected rectangle.

I don't believe iText has this capability based on my research.

You should also read How to extract text from a PDF?

Community
  • 1
  • 1
Andrew Cash
  • 2,321
  • 1
  • 17
  • 11
1

I would suggest you once you have rasterized the PDF into a JPEG image to use text recognition (OCR) to extract the text within the selected region. Here's an article about an OCR library for .NET. As far as extracting text from PDF is concerned here's an article illustrating how this be achieved more or less reliably. The problem will be how to recognize the text within the selected rectangle by the user.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • this is not an option. i need 100% accuracy. ocr is not 100% accurate. besides a single pdf document may contain text in multiple languages. as far as I know there are no free ocr libs that support ocr'ing more than one language at a time. – mmierins Nov 28 '10 at 17:21
  • @davidgale, I highly doubt a free solution exists for this kind of problems. You may take a look at the second option then about extracting text from PDF and see if it works reliably enough in your case. Still you will need to figure out about the selection region which IMHO won't be an easy task and I am not aware of any freeware library capable of doing this. – Darin Dimitrov Nov 28 '10 at 17:21
1

(disclaimer - I work for Atalasoft on its PDF products) Atalasoft's PdfReader will do this. It's not freeware, but it works quite well. The code looks like this:

using (PdfTextDocument doc = new PdfTextDocument(pathToFile)) {
    PdfTextPage page = doc.GetPage(pageNumber);
    string text = page.GetTextInBox(yourSelection);
}
plinth
  • 48,267
  • 11
  • 78
  • 120