【问题标题】:Android- Get text from PDFAndroid - 从 PDF 中获取文本
【发布时间】:2015-06-28 02:16:13
【问题描述】:

我想从 SD 卡中的 PDF 文件中读取文本。如何从存储在 SD 卡中的 PDF 文件中获取文本?

我试过了:

public class MainActivity extends ActionBarActivity implements TextToSpeech.OnInitListener {

    private TextToSpeech tts;
    private String line = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tts = new TextToSpeech(getApplicationContext(), this);

        final TextView text1 = (TextView) findViewById(R.id.textView1);

        findViewById(R.id.button1).setOnClickListener(new OnClickListener() {

            private String[] arr;

            @Override
            public void onClick(View v) {
                File sdcard = Environment.getExternalStorageDirectory();

                // Get the text file

                File file = new File(sdcard, "test.pdf");

                // ob.pathh
                // Read text from file

                StringBuilder text = new StringBuilder();
                try {
                    BufferedReader br = new BufferedReader(new                            FileReader(file));

                    // int i=0;
                    List<String> lines = new ArrayList<String>();

                    while ((line = br.readLine()) != null) {
                        lines.add(line);
                        // arr[i]=line;
                        // i++;
                        text.append(line);
                        text.append('\n');
                    }
                    for (String string : lines) {
                        tts.speak(string, TextToSpeech.SUCCESS, null);
                    }
                    arr = lines.toArray(new String[lines.size()]);
                    System.out.println(arr.length);
                    text1.setText(text);

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

    }

    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            int result = tts.setLanguage(Locale.US);
            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {
                // speakOut();
            }

        } else {
            Log.e("TTS", "Initilization Failed!");
        }
    }

}

注意:如果文件是文本文件(test.txt)但不适用于pdf(test.pdf),则可以正常工作

但是这里的文本并不是从 PDF 中获取的,而是像字节码一样。我怎样才能做到这一点?

提前致谢。

【问题讨论】:

  • PDF 文件格式不是纯文本。您需要像 PDFBox 这样的解析器库来从文件中提取文本。
  • 安卓平台可以使用PDF box吗??
  • 您好@ShylendraMadda,希望您一切顺利!我也面临同样的问题。我无法从 pdf 中提取文本。请帮帮我..
  • @Namrata 你检查这个答案它的工作stackoverflow.com/a/42887336/2462531

标签: android pdf


【解决方案1】:

PDF 格式不是您的普通文本文件。您需要对 PDF 进行更多研究,这是您将获得的最佳答案 How to read pdf in my android application?

【讨论】:

  • 我终于可以显示那个 PDF 但我怎样才能从那个 PDF 中获取文本?
  • @ShylendraMadda u gt 来自 pdf 的数据?
【解决方案2】:

我已经有了 iText 的解决方案。

分级,

compile 'com.itextpdf:itextg:5.5.10'

Java,

  try {
            String parsedText="";
            PdfReader reader = new PdfReader(yourPdfPath);
            int n = reader.getNumberOfPages();
            for (int i = 0; i <n ; i++) {
                parsedText   = parsedText+PdfTextExtractor.getTextFromPage(reader, i+1).trim()+"\n"; //Extracting the content from the different pages
            }
            System.out.println(parsedText);
            reader.close();
        } catch (Exception e) {
            System.out.println(e);
        }

【讨论】:

  • 这里的路径格式是什么?我使用了/storage/emulated/0/Download/abc.pdf,但它给出了错误
  • 您遇到了什么错误?是否添加了存储权限?
  • 是的,我得到了解决方案,有权限错误
  • 这是免费的吗?还是需要付费?
  • @Sahil 使用文件选择器分配文件位置。
猜你喜欢
  • 2012-12-02
  • 1970-01-01
  • 1970-01-01
  • 2021-06-26
  • 2012-03-23
  • 2021-01-05
  • 2016-12-28
  • 2011-07-28
  • 2015-05-19
相关资源
最近更新 更多