How to extract text from pdf document using MuPDF in Android - android

I am trying to extract text from a PDF file using MuPDF library in Android platform.
Is it possible to extract text within a rectangle specified by coordinates (left, top, right, bottom)?
Note: I didn't compile the library from source. I am using compiled libraries which is distributed in https://github.com/libreliodev/android.

yeah sure
here is the way you can do.
1.GeneratedText activity
public class GeneratedText extends Activity {
private Button close;
private Button clear;
private TextView tv;
private String data;
String text = "";
Intent i;
Context mContext;
// MuPDFPageView pdfview = new MuPDFPageView(mContext, null, null);
private EditText edit;
private Button undo;
public static GeneratedText screen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_generated_text);
close = (Button)findViewById(R.id.close);
clear = (Button)findViewById(R.id.clear);
tv = (TextView)findViewById(R.id.text1);
edit = (EditText)findViewById(R.id.edit);
undo = (Button)findViewById(R.id.undo);
undo.setEnabled(false);
i = getIntent();
data = i.getStringExtra("data");
tv.setText(data);
String mypattern = "Name and address of the Employee \n";
Pattern p = Pattern.compile(mypattern,Pattern.DOTALL);
if(data.matches(mypattern))
{
System.out.println("Start Printing name");
}
else
//do nothing
edit.setText(data);
System.out.println("hello user "+"/n"+"user1"+ "\n"+ "user2");
SharedPreferences pref = getSharedPreferences("key", 0);
SharedPreferences.Editor editor = pref.edit();
editor.putString("text", data);
editor.commit();
clear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setText("");
edit.setText("");
undo.setEnabled(true);
}
});
close.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
undo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String value = "";
SharedPreferences pref = getSharedPreferences("key", 0);
value = pref.getString("text", value);
edit.setText(value);
tv.setText(value);
undo.setEnabled(false);
}
});
}
}
1. now in mupdfactivity write this
public void Showtext( )
{
destroyAlertWaiter();
core.stopAlerts();
MuPDFPageView pdfview = new MuPDFPageView(MuPDFActivity.this, core, null);
String data = "";
pdfview.setFocusable(true);
data = pdfview.getSelectedText();
Intent i = new Intent(getApplicationContext(),GeneratedText.class);
i.putExtra("data",data);
startActivity(i);
}
call Showtext in OnAcceptButtonClick
and you will get your text.

Yes it is possible to extract text from PDF document with the help of MuPDF library. There is method called text() in mupdf.c which is defined in MuPDFCore.java which returns the text of the page. You need to call that method by page wise.
Steps:
1. gotopage(pagenumber)
2. text()

Related

How do I update my static context value saved from shared preferences?

I am planning to implement an easter egg to change my API through an alertdialog, but currently after I change my url endpoint and submit it it saves it in shared preferences, but next time i save it, it does save it in shared preferences ,but doesn't reflect in the app. How do I make it such that every single time i change it ,it applies to my apiendpoint as expected:
Alertdialog which triggers on 5 taps:
myimage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
long time= System.currentTimeMillis();
//if it is the first time, or if it has been more than 3 seconds since the first tap ( so it is like a new try), we reset everything
if (startMillis==0 || (time-startMillis> 3000) ) {
startMillis=time;
count=1;
}
//it is not the first, and it has been less than 3 seconds since the first
else{ // time-startMillis< 3000
count++;
}
if (count==5) {
final AlertDialog dialogBuilder = new AlertDialog.Builder(LoginActivity.this).create();
LayoutInflater inflater = LoginActivity.this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.custom_dialog, null);
final EditText editText = (EditText) dialogView.findViewById(R.id.edt_comment);
Button submitButton = (Button) dialogView.findViewById(R.id.buttonSubmit);
Button cancelButton = (Button) dialogView.findViewById(R.id.buttonCancel);
Button resetButton = (Button) dialogView.findViewById(R.id.buttonReset);
editText.setTextColor(getResources().getColor(R.color.black,null));
if(SharedPreferencesHelper.getSomeStringValue(getApplicationContext()) != null) {
editText.setText( SharedPreferencesHelper.getSomeStringValue(getApplicationContext()));
}
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialogBuilder.dismiss();
}
});
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//save the endpoint value in shared preferences
SharedPreferencesHelper.setSomeStringValue(getApplicationContext(),editText.getText().toString());
dialogBuilder.dismiss();
}
});
resetButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferencesHelper.setSomeStringValue(getApplicationContext(),BuildConfig.BASE_URL);
dialogBuilder.dismiss();
}
});
dialogBuilder.setView(dialogView);
dialogBuilder.setCanceledOnTouchOutside(false);
dialogBuilder.show();
}
}
});
Here's my sharedpreferenceshelper:
public class SharedPreferencesHelper {
private static final String APP_SETTINGS = "APP_SETTINGS";
// properties
private static final String SOME_STRING_VALUE = "SOME_STRING_VALUE";
// other properties...
private SharedPreferencesHelper() {}
private static SharedPreferences getSharedPreferences(Context context) {
return context.getSharedPreferences(APP_SETTINGS, Context.MODE_PRIVATE);
}
public static String getSomeStringValue(Context context) {
return getSharedPreferences(context).getString(SOME_STRING_VALUE , null);
}
public static void setSomeStringValue(Context context, String newValue) {
final SharedPreferences.Editor editor = getSharedPreferences(context).edit();
editor.putString(SOME_STRING_VALUE , newValue);
editor.apply();
}
}
Finally, here's my Apiendpoint class:
final class ApiEndPoint {
private static String NEW_URL = SharedPreferencesHelper.getSomeStringValue(MyApp.getAppContext());
static final String ENDPOINT_SERVER_LOGIN = NEW_URL
+ "/service-myverification-link/v1/link/verify";
private ApiEndPoint() {
// This class is not publicly instantiable
}
}
For the code above, I was wondering how do i make the NEW_URL link more dynamic, seems like its sticking to the value saved first time and does not change after. I need to keep this link static due to :
#Override
public Single<LoginResponse> doServerLoginApiCall(LoginRequest request) {
return Rx2AndroidNetworking.post(ApiEndPoint.ENDPOINT_SERVER_LOGIN)
.doNotCacheResponse()
.addBodyParameter(request)
.build()
.getObjectSingle(LoginResponse.class);
}
Any idea, how I can fix this so I can update the link in NEW_URL every single time that it sticks to the new value instead of the value stored the first time?
You will need to use the onSharedPreferenceChangeListener
SharedPreferencesHelper.getSharedPreferences(context).registerOnSharedPreferenceChangeListener(
new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if(key.equals("SOME_STRING_VALUE")){
NEW_URL = prefs.getString("SOME_STRING_VALUE" , null);
}
}
});
Why don't you send the Url as a parameter every time:
#Override
public Single<LoginResponse> doServerLoginApiCall(LoginRequest request, String url) {
return Rx2AndroidNetworking.post(url)
.doNotCacheResponse()
.addBodyParameter(request)
.build()
.getObjectSingle(LoginResponse.class);
}
Method call:
doServerLoginApiCall(request, SharedPreferencesHelper.getSomeStringValue(getApplicationContext()) + ENDPOINT);

EditText to phone memory?

I have found a very good solution to my problem on another post (Save entered text in editText via button)
however when I implement this code, my application crashes. Any advice would be appreciated, the error I receive is that the "String or" in the method makeTag() is not used. Please have a look
private Button savenotebutton1;
private SharedPreferences savednotes;
private EditText editText1;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.x1);
savenotebutton1 = (Button) findViewById(R.id.savenotebutton1);
editText1 = (EditText) findViewById(R.id.noteEditText1);
savednotes = getSharedPreferences("notes",MODE_PRIVATE);
editText1.setText(savednotes.getString("tag", "Default Value")); //add this line
savenotebutton1.setOnClickListener(saveButtonListener);
}
private void makeTag(String tag){
String or = savednotes.getString(tag, null);
SharedPreferences.Editor preferencesEditor = savednotes.edit();
preferencesEditor.putString("tag",tag); //change this line to this
preferencesEditor.commit();
}
public OnClickListener saveButtonListener = new OnClickListener(){
#Override
public void onClick(View v) {
if(editText1.getText().length()>0){
makeTag(editText1.getText().toString());
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(editText1.getWindowToken(),0);
}
}
};
}
You should replace this
String or = savednotes.getString(tag, null);
With
String or = savednotes.getString("tag", "Default Value")
Under your makeTag() function
Update: Error is about you are not register your activity into manifest.xml file.

How can I save ints to be retained even when the application is not running?

When I click on an option I want a check sign to appear to show it has been purchased. I am using SharedPreferences and have made all the links; however, when I click back or exit and then re-enter the application, the check is gone and any history of anything I did before is gone. How do I save the data so that it is kept even when the application is off or I'm at another page in the game. This is my code so far:
ImageView tick1;
int mYellCheck, mYellCheck1;
public static String sharedPrefs = "MyData";
SharedPreferences data;
SharedPreferences.Editor editor;
int mYellAlpha;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.upgrades);
update();
coins = Replay.totalCoins + Replay4.totalCoins + Replay5.totalCoins
+ Replay6.totalCoins;
data = getSharedPreferences(sharedPrefs, MODE_PRIVATE);
tick1.setAlpha(mYellCheck);
}
public void update() {
TextView coinScore = (TextView) findViewById(R.id.tvScore);
coinScore.setText("You have " + coins + " coins");
tick1 = (ImageView) findViewById(R.id.ivtick1);
ImageView moreYellow = (ImageView) findViewById(R.id.ivMoreYellow);
ImageView back = (ImageView) findViewById (R.id.ivBack);
back.setOnClickListener(this);
moreYellow.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.ivMoreYellow:
mYellAlpha = 255;
SharedPreferences.Editor editor = data.edit();
editor.putInt("MoreYellowUpgrade", mYellAlpha);
editor.commit();
data = getSharedPreferences(sharedPrefs, MODE_PRIVATE);
mYellCheck = data.getInt("MoreYellowUpgrade", 0);
tick1.setAlpha(mYellCheck);
break;
case R.id.ivBack:
Intent i = new Intent ("m.k.zaman.SpotActivity");
startActivity(i);
break;
}
}
EDIT
or Should i just use internal storage?
Try replacing your current onClick call to R.id.ivMoreYellow with this:
case R.id.ivMoreYellow:
mYellAlpha = 255;
mYellCheck = data.getInt("MoreYellowUpgrade", 0);
tick1.setAlpha(mYellCheck);
SharedPreferences.Editor editor = data.edit();
editor.putInt("MoreYellowUpgrade", mYellAlpha);
editor.commit();
break;
You were first overwriting all of the data in the SharedPreferences, and then calling to receive that data that you just overwrote, so I placed the getInt() call before the putInt(). Do you change mYellAplha to anything other than 255 though? If you don't you will never notice a change in SharedPreferences if you are always saving and getting 255.

Retrieve text from edittext to array string in android eclipse

I am making text to voice conversion app. I want to take text from user and change it to voice. when i made input text hard coded. my app works perfectly.
static final String[] texts = {"what's up dude"};
TextToSpeech tts;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button)findViewById(R.id.btexttovoice);
b.setOnClickListener(this);
tts = new TextToSpeech(textvoice.this,new TextToSpeech.OnInitListener(){
#Override
public void onInit(int status) {
// TODO Auto-generated method stub
if(status!= TextToSpeech.ERROR)
{
tts.setLanguage(Locale.US);
}
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
if(tts!= null)
{
tts.stop();
tts.shutdown();
}
super.onPause();
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Random r = new Random();
String random = texts[r.nextInt(3)];
tts.speak(random, TextToSpeech.QUEUE_FLUSH, null);
}
}
it works great but if i want to take input from user through Edit text
i make the following changes:
String[] texts ;
text = (EditText)findViewById(R.id.ttexttovoice);
texts = text.getText().toString();
Here i get error since texts is the array type. How can i get text from edit text to array type of string?
if i do this without array string
String texts ;
text = (EditText)findViewById(R.id.ttexttovoice);
texts = text.getText().toString();
i get no error but i didn't find the desired output. In fact no voice is received.
it seems a simple problem but i am stuck here. kindly help me. Thanks in advance.
String[] texts = new String[3];
text = (EditText)findViewById(R.id.ttexttovoice);
texts[0] = text.getText().toString();

Using a button and then showing dynamic text

I am writing a program that when the user enter a number text appears according to that number. My problem is that the button line has public void ... after this I am trying to use if statements and return methods, but because of the public void, the return method can not return anything. I tried to close the public void, but I am getting errors. Please help.
The code is as follows. I have included the different codes that I have tried like toast, etc..
ente#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.go);
button.setOnClickListener(mAddListener);
// tv = (TextView) findViewById(R.id.textView1);
}
private OnClickListener mAddListener = new OnClickListener()
{
public void onClick(View v) {
}}
;
//Toast.makeText(Num.this, "This Display", Toast.LENGTH_SHORT).show();
//toast.show();
//finish ();
// long id = 0;
// try
{
PleaseEnter=(EditText)findViewById(R.id.PleaseEnter);
{
}
if (PleaseEnter.equals("1"))
// tv.setText("This is the display 1.");
return "This display";
// Context context = getApplicationContext();
// CharSequence text ="this display";
// int duration =Toast.LENGTH_LONG;
// Toast toast =Toast.makeText(context, text, duration);
// toast.show();
else if (PleaseEnter.equals("2"))
return;
//tv.setText("Dispaly 2");
You can define your own method at Activity level, such as:
private void onTextEdited(String content) {
// deal with the String
}
In the onClick method of your OnClickListener, you can call it such as:
public void onClick(View v) {
EditText myEditText = (EditText) findViewById(R.id.PleaseEnter);
onTextEdited(myEditText.getText().toString());
}

Categories

Resources