0

I want to extract text from pdf file but I referred this and this links but no use.I achieved in getting text from text file using following code.

public class MainActivity extends ActionBarActivity implements TextToSpeech.OnInitListener {

    private TextToSpeech tts;
    private String line = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tts = new TextToSpeech(getApplicationContext(), this);

        final TextView text1 = (TextView) findViewById(R.id.textView1);

        findViewById(R.id.button1).setOnClickListener(new OnClickListener() {

            private String[] arr;

            @Override
            public void onClick(View v) {
                File sdcard = Environment.getExternalStorageDirectory();

                // Get the text file

                File file = new File(sdcard, "test.txt");

                // ob.pathh
                // Read text from file

                StringBuilder text = new StringBuilder();
                try {
                    BufferedReader br = new BufferedReader(new                            FileReader(file));

                    // int i=0;
                    List<String> lines = new ArrayList<String>();

                    while ((line = br.readLine()) != null) {
                        lines.add(line);
                        // arr[i]=line;
                        // i++;
                        text.append(line);
                        text.append('\n');
                    }
                    for (String string : lines) {
                        tts.speak(string, TextToSpeech.SUCCESS, null);
                    }
                    arr = lines.toArray(new String[lines.size()]);
                    System.out.println(arr.length);
                    text1.setText(text);

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

    }

    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            int result = tts.setLanguage(Locale.US);
            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {
                // speakOut();
            }

        } else {
            Log.e("TTS", "Initilization Failed!");
        }
    }

}

How it's possible to convert that pdf file to text file in android?

Community
  • 1
  • 1
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
  • Did you try this link: http://www.aspose.com/docs/display/pdfandroid/Extract+Text+from+PDF+Document – keshav kowshik Apr 24 '15 at 05:38
  • You try to read text from the PDF as if it were a text file. It is not. It is a binary file in which text is usually drawn using a non-standard encoding and the drawing operations usually also are compressed. In essence you will need a PDF library decompressing, decoding, and re-assembling the text. – mkl Apr 24 '15 at 07:57
  • If I used pdfbox lib Getting this error: "java.lang.NoClassDefFoundError: org.pdfbox.pdmodel.PDDocument" – Shailendra Madda Apr 24 '15 at 08:09
  • PDFBox references Java Runtime classes not present on android systems. If you want to use PDFBox there, you might have to patch it to remove those dependencies (and some functionality with them). – mkl Apr 24 '15 at 10:00

0 Answers0