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文件。所以当我点击播放按钮,它开始用android TextToSpeech
功能大声朗读当前的pdf或(任何扩展名)文件。任何有pdf阅读能力的教程都会对我有所帮助。谢谢
答案 0 :(得分:0)
好的,来自PDFBox。取自here
的示例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中提取文本。