Click here to Skip to main content
16,022,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to implement a text to speech in my application. First, I have implemented PDFview that allows me to pick PDF files from my device and load it. What I want to achieve is that after loading this PDF to the PDFview, I want to read the text out. How do I achieve this?

using same Uri passed into my PDFView libabry to load the pdf so i can view it


What I have tried:

public String getRealPathFromURI(Context context, Uri contentURI)
    {
        String result;

        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        if(cursor == null)
        {
            result = contentURI.getPath();
        } else {

            cursor.moveToFirst();
            int idx = cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);
            result = cursor.getString(idx);
            cursor.close();
        }
        return result;
}








 btnSpeak.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
                    String parsedText = "";
               try
               {

                    PdfReader pdfReader = new  PdfReader(getRealPathFromURI(getContext(), uri));
                   int n = pdfReader.getNumberOfPages();
                  for (int i = 0; i < n; i++) {
                       parsedText = parsedText + PdfTextExtractor.getTextFromPage(pdfReader, i + 1).trim() + "\n";
                   }
                   pdfReader.close();


                   textToSpeech.speak(parsedText, TextToSpeech.QUEUE_FLUSH, null);
               }
               catch (Exception ex)
               {

               }
           }
       });
Posted
Updated 20-Aug-18 23:23pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900