How to extract data from pdf file in matlab?

I am in search of such algorithm that will extract data from pdf file.For example in the pdf file a sentence is present i.e: Account# 29 . I want to extract 29 from pdf file.If it is possible by fopen() function ,please share it with me.I have tried pdftotext but doesn't succeed. Now if it is possible to extract data from pdf with the help of fopen(), it will be better.I also tried fopen() but leads to failure.Please share you experience with me..Thanks.

6 Comments

José-Luis
José-Luis on 19 Sep 2014
Edited: José-Luis on 19 Sep 2014
fopen() will not automagically read pdf data.
As a philosophical aside, pdf's are not meant to be edited. They are intended to be read only. I am afraid whatever method you can come up with will be something of a kludge.
Technically, it should be possible to make sense of a pdf file with fread. In practice, you would need to interpret the pdf format, and that is a tall order since a pdf is not really text but an image file. That doesn't stop tools like pdftotext to attempt it, but you'll probably get mixed results, depending on what's in your file.
thanks for comment: If you have any idea of modify pdf file: linK: http://stackoverflow.com/questions/18246650/modifying-pdf-file-with-matlab-by-fopen
Share it with me..I tried this one..But it save output with out any changes.If you can fix it please help me..
No, I would be very surprised if you could do it like that. Opening a binary file, which is what fopen() would get you, is a long way from actually transforming that file into text.
There is no easy way to extract text from pdfs.
Have you tried modify pdf file? Have you tried the link which i have mentioned in the previous comment?I am stuck in extracting data from pdf.i was using external converter but now i want it to be done inside matlab.Please solve my problem..thanks
Yes, I have seen it and it doesn't work. In principle, it might work for trivial purposes like changing the font type, but I have no idea what kind of data you are trying to extract.
Writing a robust algorithm is a tall order.
Sir,Just give a clue that how it is possible: let suppose Pdf file contain:
Account# 345
i want to capture 345 from it..for example i can use regexp() to extract numbers only...Please help me...I have spend lots of my time on it..but doesn't succeed..Almost i have wasted a month for it....Thanks

Sign in to comment.

 Accepted Answer

Jan
Jan on 21 Sep 2014
Assume you have a PDF file, which is displayed containing the string "Account# 345". Now different details impede the extraction of this string:
  • The contents can be compressed and/or encrypted, such that the string cannot be found in clear text inside the file.
  • Even without encryption or compression, the text need not be stored continously, but in a valid PDF each character can be stored with its paper position, such that the order does not matter.
In consequence searching a string in a PDF is not reliable. Therefore some OCR software is applied frequently to add an additional layer containing the contents as searchable strings. But as long as you do not specify any details of your PDF we cannot guess if they contain such strings.
Please notice, that your problem is not well defined and suggesting solutions is still based on guessing, although you've posted several corresponding questions in this forum. Finally the main problem is, that somebody decided to store data in PDF files, which is not sufficient for the later extraction of strings. Creating a large and complicatd workaround afterwards is an inefficient way. It would be more stable and faster to obtain the data in a more suitable format as a text file.

5 Comments

@Jan Simon: Thanks For Answer: I have attached my PDF and also encircle what i want to capture.The attached pdf is a scanned version.I also have Original PDF (means not scanned).Sir, Please help me.I am really stuck at this step.How can we use OCR in Matlab.. If there is any possibility to read this pdf and convert data into cells in matlab, It will be outstanding.You are my last Hope.You know that i have posted this question several times but no positive response from any other except you.So Please Help me...Waiting for you Response...Bundle of Thanks...
@Azizullah: I am sorry that you feel the responses have been negative. Let me just say that I think you have not grasped neither the magnitude nor the complexity of what you are asking.
Let me try to impress the main difficulties of what you are asking:
  1. pdfs are not text files. They can be images. The individual characters might not be stored contiguously. In your case, you have a scan, you might no find any strings there.
  2. OCR is not a problem that can be solved easily in this forum. There might be some attempts in the File Exchange. Bear in mind that there are entire companies that hire small armies of programmers to do OCR and, to my knowledge, there are no 100% fool-proof solutions.
The easiest solution, by far, is to get the original file from which the pdf was generated, as Jan suggests. Alternatively, if you have even a few hundred documents, it will be faster to manually type that in than try to come up with a robust algorithm.
I can only think of one scenario where you could extract the text. That would be if the text you are interested is in a consistent position. This is what I would do:
  1. Extract portions of your pdf as an image.
  2. Use the ocr function from the symbolic image toolbox to transform that image into a string.
That will work only if the text you are interested in is always in the same position.
@Jose-luis: Sorry but in my mind positive response will that when problem solved.. and thank for comment. Sir, If you can give me some time from your precious time and type code that how i will use ocr in matlab..and convert data into cells. My main aim is:
1) Read original pdf in matlab and convert data into cells.
2) Read Scanned pdf in matlab and convert data into cells. I you can help me and do the above two step further there is no problem to me to extract data from cells.
Up till now i am using external pdf to excel converter but now i want that to do that conversion inside matlab. I have also attached original pdf..please help me.. Please help me..I shall be very thankful to you for your this help from the core of my heart for the rest of my life....Thanks
A possible workaround is to convert the PDF to an Excel file, and then import that XLS file to Matlab. This is a relatively good solution for PDF's that contain tables of data.
If you take a look first at the Excel file, you might find ideas how to access the data you're interested in.
We will get a matrix if we use imread for a picture. How do we get the matrix of the PDF?

Sign in to comment.

Categories

Asked:

on 19 Sep 2014

Commented:

on 30 Jun 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!