0

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

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)
               {

               }
           }
       });

The above code is what i tried to do.

Mcbaloo
  • 157
  • 5
  • 18
  • You would 1) Create a TextToSpeech object, tts, 2) parse all the txt in the pdf file from beginning to end into one long string, and 3) tts.speak(string). If the was something more specific you wanted to do, then maybe edit the question with more precise requirements. – Nerdy Bunz Aug 21 '18 at 02:12
  • I have tried to parse all the text in the PDF to one long string. I try to do that using the IText PDFReader library but it's not working hence tts is unable to read out the text – Mcbaloo Aug 21 '18 at 06:37
  • You could use Log and logcat to determine whether yourre getting an exception, and if so, what it is. If you're not getting an exception, then check whether you are actually getting any text inside the parseText String by logging it. If you are getting the proper text, then the problem is with the textToSpeech (doubtful). – Nerdy Bunz Aug 22 '18 at 02:15
  • Actually I'm not getting any text from the parsedText.. I think that's where the problem is. I have tried a normal text and the tts works fine. It's extracting text from the PDF that's a bit of an issue – Mcbaloo Aug 22 '18 at 10:05
  • I don't really have experience with URIs or the PDFReader class... but if you apply the same method of using Log.i(...) in various places in your code to verify that things are happening the way you expect, then you'll find the problem. For exmaple, you can Log the result given by getRealPath() to make sure it is correct... or you could log inside the for loop to make sure it is even being executed at all. – Nerdy Bunz Aug 22 '18 at 10:29

1 Answers1

0

Try this:

btnSpeak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String parsedText = "We did not get any text from the PDF.  You will have to examine why.";
                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();
                    } 
                    catch (Exception ex) 
                    {
                    Log.i("XXX", "There was an exception: " + ex);    
                    }
                textToSpeech.speak(parsedText, TextToSpeech.QUEUE_FLUSH, null);
            }
        });
Nerdy Bunz
  • 6,040
  • 10
  • 41
  • 100