title: Google Android Text-to-Speech is not working with PDFBox on Android
body: <p>I use PDFBox to extract text from a document and display it, I also want the text extracted available to Text-to-Speech so it can dictate it but it does not work. I tried different methods from different forums on Stackoverflow and other websites. Unfortunately all of those doesn't work neither. It's been long since this problem is still unresolved. I appreciate y'all! My code is written in Java.</p>
  <pre><code>import android.app.Activity;
  import android.content.Intent;
  import android.content.pm.PackageManager;
  import android.net.Uri;
  import android.os.Bundle;
  import android.speech.tts.TextToSpeech;
  import android.text.method.ScrollingMovementMethod;
  import android.util.Log;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import androidx.appcompat.app.AppCompatActivity;
  
  import com.tom_roush.pdfbox.android.PDFBoxResourceLoader;
  import com.tom_roush.pdfbox.pdmodel.PDDocument;
  import com.tom_roush.pdfbox.text.PDFTextStripper;
  
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.io.InputStream;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Locale;
  
  public class ReadPDF extends AppCompatActivity {
  
      TextToSpeech tts;
      TextView outputTextView;
      private static final int READ_REQUEST_CODE = 42;
      Uri fileUri;
      Intent intentOpenFile;
  
      String googleTtsPackage = &quot;com.google.android.tts.service.GoogleTTSService&quot;;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.content_main);
          outputTextView = findViewById(R.id.output_text);
          outputTextView.setMovementMethod(new ScrollingMovementMethod());
  
          intentOpenFile = new Intent(Intent.ACTION_OPEN_DOCUMENT);
          intentOpenFile.setType(&quot;application/pdf&quot;);
          startActivityForResult(intentOpenFile, READ_REQUEST_CODE);
  
          tts = new TextToSpeech(this, status -&gt; {
              tts.setLanguage(Locale.forLanguageTag(&quot;fil-PH&quot;));
              tts.setSpeechRate(0.8F);
              tts.setPitch(0.8F);
              Locale[] locales = Locale.getAvailableLocales();
              List&lt;Locale&gt; localeList = new ArrayList&lt;&gt;();
              for (Locale locale : locales) {
                  int res = tts.isLanguageAvailable(locale);
                  if (res == TextToSpeech.LANG_COUNTRY_AVAILABLE) {
                      localeList.add(locale);
                  } else {
                      tts.setLanguage(Locale.US);
                  }
              }
              if(status != TextToSpeech.ERROR){
                  if(!isPackageInstalled(getPackageManager(), googleTtsPackage))
                      confirmDialog();
                  else tts.setEngineByPackageName(googleTtsPackage);
              }
          });
  
          PDFBoxResourceLoader.init(getApplicationContext());
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent resultData) {
          super.onActivityResult(requestCode, resultCode, resultData);
          if (requestCode == READ_REQUEST_CODE &amp;&amp; resultCode == Activity.RESULT_OK) {
              if (resultData != null) {
                  fileUri = resultData.getData();
                  readPDFFile(fileUri);
                  Toast.makeText(this, &quot;Loaded successfully&quot;, Toast.LENGTH_SHORT).show();
              }
          }
      }
  
      public void readPDFFile(Uri uri) {
          PDDocument document = null;
          String parsedText = null;
          try {
              InputStream inputStream = this.getContentResolver().openInputStream(uri);
              document = PDDocument.load(inputStream);
              PDFTextStripper pdfStripper = new PDFTextStripper();
              pdfStripper.setStartPage(1);
              pdfStripper.setEndPage(20);
              parsedText = pdfStripper.getText(document);
              outputTextView.setText(parsedText);
              tts.speak(parsedText, TextToSpeech.QUEUE_ADD, null, null);
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
          @Override
      public void onPause(){
          super.onPause();
          tts.stop();
      }
  
      @Override
      public void onDestroy(){
          super.onDestroy();
          tts.shutdown();
      }
  
      private void confirmDialog(){
          Intent installVoice = new Intent(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
          startActivity(installVoice);
      }
  
      public static boolean isPackageInstalled(PackageManager pm, String packageName) {
          try {
              pm.getPackageInfo(packageName, 0);
          } catch (PackageManager.NameNotFoundException e) {
              return false;
          }
          return true;
      }
  }```
  
  
  </code></pre>
  
  <p>XML Activity is this..</p>
  <pre><code>&lt;TextView
      android:id=&quot;@+id/output_text&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:layout_alignParentTop=&quot;true&quot;
      android:layout_alignParentBottom=&quot;true&quot;
      android:scrollbars=&quot;vertical&quot;
      android:text=&quot;&quot;
      android:textColor=&quot;#000000&quot;
      android:visibility=&quot;visible&quot;
      app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
      app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
      app:layout_constraintRight_toRightOf=&quot;parent&quot;
      app:layout_constraintTop_toTopOf=&quot;parent&quot;
      tools:ignore=&quot;SpeakableTextPresentCheck&quot;
      tools:visibility=&quot;visible&quot; /&gt;
  </code></pre>
  <p>I did try</p>
  <pre><code>public void readPDFFile(Uri uri) { 
  PDDocument document = null;
  String parsedText = null;
  try { 
  InputStream inputStream = this.getContentResolver().openInputStream(uri);
  document = PDDocument.load(inputStream);
  PDFTextStripper pdfStripper = new PDFTextStripper();
  pdfStripper.setStartPage(1);
  pdfStripper.setEndPage(20);
  parsedText = pdfStripper.getText(document);
  outputTextView.setText(parsedText);
  tts.speak(parsedText, TextToSpeech.QUEUE_ADD, null, null);
  } catch (IOException e) { e.printStackTrace(); } }
  </code></pre>
  <pre><code>and I expected it to give me Text-to-Speech output.
  </code></pre>