0

Android 提供了一个名为 TextToSpeech 的功能,它能够使用此代码大声朗读文本,我可以在 android 中大声朗读任何句子。

TextToSpeech tts= new TextToSpeech(this, this);
int result = tts.setLanguage(Locale.US);
tts.speak(**text**, TextToSpeech.QUEUE_FLUSH, null);

这里的text变量存储要听到的句子,但我的问题是我不想要一个变量,我想要的是将整个 pdf 或 .doc 文件放在那里。所以当我点击播放按钮时它开始阅读当前的 pdf 或(任何扩展名)文件,带有 androidTextToSpeech功能。任何具有 pdf 阅读能力的教程都会对我有很大帮助。谢谢

4

1 回答 1

0

好的,来自 PDFBox。取自此处的示例

import java.io.*;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.util.*;

public class PDFTest {

 public static void main(String[] args){
 PDDocument pd;
 BufferedWriter wr;
 try {
         File input = new File("C:\\Invoice.pdf");  // The PDF file from where you would like to extract
         File output = new File("C:\\SampleText.txt"); // The text file where you are going to store the extracted data
         pd = PDDocument.load(input);
         System.out.println(pd.getNumberOfPages());
         System.out.println(pd.isEncrypted());
         pd.save("CopyOfInvoice.pdf"); // Creates a copy called "CopyOfInvoice.pdf"
         PDFTextStripper stripper = new PDFTextStripper();
         stripper.setStartPage(3); //Start extracting from page 3
         stripper.setEndPage(5); //Extract till page 5
         wr = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output)));
         stripper.writeText(pd, wr);
         if (pd != null) {
             pd.close();
         }
        // I use close() to flush the stream.
        wr.close();
 } catch (Exception e){
         e.printStackTrace();
        }
     }
}

PDFStripper 是这里的关键。从 PDF 中提取文本。

于 2014-04-08T19:53:30.930 回答