Use OCR Text reader to save the text - java

I followed the OCR text reader guide on Codelabs (https://codelabs.developers.google.com/codelabs/mobile-vision-ocr/#0).
Now, I would like to save, a single portion of the text that I am scanning.
I tried with reducing width, and height of the preview; but it doesn't work, the APK crashes (at least on the only device I have to test it).
I am completely new to Java, and Android development, but my Internship mentor said to do this; completely alone, with zero help (as no one in the company knows about development).
So, the app opens, it recognizes text. Now, I would like to to know if there is a way to take that text, and save it (XML or TXT file).
I tried to look in the code, and see if at some point, the text read is saved in a variable or something; but it looks like a live preview, done trough the Google's dependencies (or a similar process).
I am not sure, but this might be off topic, as it is similar to an open question, but I am giving details on what I have done so far, and what I have tried.
Thanks.

The detected text is displayed in the OcrGraphic.draw(Canvas) method. There, it is returned as a TextBlock. You can call textBlock.getComponents() to get the lines and textBlock.getComponents() again to get each individual word (as a Text object).
Then you can convert it to a string and write the text to a file if you would like.

Related

Best way to extract text from PDF in java

I want to make a program that is able to read PDF files and parse it's contents.
Thus I need to extract the text using some kind of library. I found 3 ways to do so.
OCR libraries (like Tesseract)
ScanPdf libraries (like iText)
Converters from PDF to text.
I fail to understand the big differences between them since all of them will produce in the end a text file from the PDF. So which is the best way to go about this?
PDF is a complex format. If you open a PDF and you're staring at a bunch of text, that doesn't really tell you much. It could be that you're staring at an image file someone decided to wrap into a PDF file. This is 99%+ certain what you have if someone scanned a document and told their scanner to 'scan to PDF', and 100% certain what you got if you have a PNG or JPG and 'save as PDF', or try to 'print to PDF' such a thing.
There is no text in the PDF then. There are pixels.
To turn pixels into text, that's where OCR libraries come in. That's what they do. That is all they do. It's an AI bonanza and error prone. No guarantees.
However, PDF is more complex than that, it isn't like PNG/JPG: It's more like HTML. You can put actual text in there.
This has different issues, though. You can place text blobs (i.e. a 'rectangle', with coordinates, and then the text that is supposed to go inside). Again a lot like HTML: You can do something like:
<p class="foo">
World!
</p>
<p class="bar">
Hello,
</p>
and then create CSS so that the foo is rendered after the bar block (can be as simple as .foo, .bar { display: block; } .foo {float: right}).
Turning that HTML into "World! Hello," is not all that tricky. Realizing that during a render, you end up seeing "Hello, World!", and thus writing code that returns "Hello, World!", that's way more complicated.
The same problem applies to PDF. For simple PDFs, extracting the raw text inside is not too difficult, but be aware that for even mildly complex PDFs, the text can arrive in a jumbled mess.
iText is trying to give you enough power, at least, to provide the latter: To give you a full hierarchical breakdown. It returns 'here is a text box, here is its positioning, and here is the text inside. and now here is another text box, etc'. It does not return a big string.
In other words: The answer depends a lot on what PDFs you have / what PDFs you expect to be able to read, and how complex they are. If they are scans, you need an OCR library. If they are simple, a basic pdf2text converter will do fine. If you want to attempt to take into account fancily positioned PDFs with forms inside and 'popups' that can be opened and closed, oof. Probably all these tools are insufficient and you're signing up to many personweeks worth of effort.
There definitely IS text embedded PDFs, it is NOT just pixels.
It depends on if the PDF is a "true" PDF (ie you can highlight the text and copy and paste it elsewhere) or if the PDF is a scanned image.
With scanned images, you'll have to use an OCR API. All of the major cloud providers have OCR APIs (ie Amazon Textract, Google Document AI, Microsoft Form Recognizer, etc). If it's a true PDF, then I've found the pdf.js library (https://mozilla.github.io/pdf.js/) quite helpful in doing a direct text extraction.
Just know that doing this only gets you the text that is literally on the page, and there's quite a bit of work still to do to get key/value data fields programmatically across many documents.
This is something that my startup is working on (www.sensible.so/) too if you're interested in something more powerful!

How to modify an existing (flat) pdf as a form in Java?

Let me start off with saying that I've seen some questions in this regard but nothing actually specifically answering my issue.
Let's say I have a template version of a PDF received by someone else. It's a classic PDF that looks like a form but only one part is editable, the other parts are not because they are being filled in with data from an imported excel sheet. (Sadly I can't show an example)
I've been able to do most in iText 7 but I can't figure out for the life of me if I can change a flat text field into an editable form field, like on image 1.
Image 2 shows checkboxes that I want to make editable as well but I can only change their value within java and even then it only shows a cross, not a checkmark (even if I change the checktype).
It's a bit hard to explain but tl:dr is that I just want to know whether I can edit an existing flat PDF in java or if I have to do so in adobe itself.
Thanks

MS Word line breaks in Java

I'm building a grammatical text interpreter with a JavaFX GUI where you can just paste your text into a TextArea. This creates something of a problem though, since line breaks from MS Word are somehow formatted in a special way (both paragraphs (ENTER) and line breaks (SHIFT+ENTER).
If I copy the text to a plain .txt file and open it in a browser (tried Chrome, Safari and Firefox), I can copy it again from there without any problem. They somehow fix the issue.
Pasting the text into a new e-mail in Apple's mail and then copying it from there, fixes it, even pasting it into a JEditorPane (java swing), fixes it.
Notepad, TextEdit, Notes and Pages does NOT solve it.
But I need the ability to copy the text directly from a variety of sources and asking people to copy it to somewhere else is just not an option.
I searched for a solution before this post, and the general solution I could find was to replace "\r" with "\r\n", but this doesn't seem to help, as the line break is already lost, as soon as it has been pasted into the TextArea.
A different solution would of course be to simply use a JEditorPane instead of the TextArea, but when I create an AnchorPane and put a JEditorPane into it as a swingComponent, I get an area the size of the initial text, and editing that text on runtime doesn't change the size of that area (which is NOT the area of the contentPane).
Now, I know that I present different solution ideas here, and that I probably should just ask for how to get one specific thing to work, but I'm really getting a headache over this, so weather it's a way to get the contentPane to work properly with the layout, finding a way to make the TextArea keep these linebreaks or even something different is fine. I've just spent too much time trying to solve it now :(

How to add a non-printable image in xsl / apache-fop

I'm trying to generate an xsl to be printed in a pre-printed sheet which works fine.
Now i want to give the user a better previsualization (in the pdf screen version) adding a background image which emulates the "pre-printed" stuf on the sheet to give the user a "context" of what is he printing.
The question is: Is there any way I can set a background image in xsl (using apache fop) visible only in pdf but not in the printed version of it?
Thank you all for reading or givin any advice.
Although as the comments state, you can't have content in the PDF that does not come out in a physical printed copy, here is one possible work around for you. Depending on how your users are ultimately going to be using FOP for PDF rendering and how your a driving the work flow, it's possible to pass a parameter into an xslt file before the transofrmation phase is run, so potentially, you could do a dual rendering of the same PDF, one that is presented to the user where the background image is enabled, and one that gets printed, you could just set a variable similar to how they do in this Example, and call it something like $isPreview, and just use a simple if or choose statement to check for 'Y' or 'N'.
Since you are sending to a printer, you may even want to take advantage of FOP's ability to generate to Postscript rather than PDF, I've used this feature quite extensively for print documents using FOP while also producing a PDF copy for electronic delivery via email or hosted services, and I've yet to find any discrepancy between the PDF rendering and what is printed after sending a rendered postscript file, so it should work well for you as well.
As I said, this is not truly a solution to your problem as you've presented it, but as a work around, it could get you the desired results if your clever about how you implement it.
I don;t think the statement that it is not possible is true, I am just not sure how to create such a PDF with FOP. Certainly you can add an image field. One would use a button field and place the image in the button. Then you would set the properties of that button to not print (printable false).
PDF support images in fields: https://answers.acrobatusers.com/adding-image-field-form-q41825.aspx
RenderX supports PDF Form fields but I do not see where they support an image inside the button, only text: http://www.renderx.com/reference.html#PDF%20Forms. But they do support setting a field to "printable".

Managing text in Android application like in a eBook

I am developing an application which looks like an eBook reader, but its not exactly an eBook reader.
I have a huge text which is divided into various chapters. Now i want to present that text as a book, user should get a feel of reading an eBook which will have various features like GoTo, Search, Table of Contents and most importantly page curl transition between pages.
Now the problem is how to divide the whole content into number of pages. How can I know that, the number of characters that are going to fit into the screen( depending on the screen size and font size). I am totally confused on where to start and how to proceed.
Ultimately I am planning to develop an eBook Reader which will read only one book that is the text which I give.
Please let me know to achieve this, where to put page breaks and where to put the text( in database or resource file).
I'm trying to use Android TextSwitcher UI Component.
It automatically breaks the text according to the screen size. You can add some click listeners to traverse between pages..

Categories

Resources