如何在Android中将PDF文件转换为文本文件?

时间:2013-01-28 07:24:44

标签: android

在我的应用程序中,我想将pdf文件转换为文本文件,任何人都可以帮助它 从SD卡读取pdf文件并将其转换为文本文件并存储在SD卡上。

2 个答案:

答案 0 :(得分:2)

图书馆和sdk的集合

这可能对您有所帮助

http://www.qoppa.com/android/pdfsdk/

http://code.google.com/p/droidreader/

答案 1 :(得分:1)

从SD卡读取pdf文件:

Please check your device is any pdf reader application available, I think isn't any..

只需使用此代码,

    private void viewPdf(Uri file) {
            Intent intent;
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(file, "application/pdf");
            try {
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                // No application to view, ask to download one
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("No Application Found");
                builder.setMessage("Download one from Android Market?");
                builder.setPositiveButton("Yes, Please",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent marketIntent = new Intent(Intent.ACTION_VIEW);
                                marketIntent
                                        .setData(Uri
                                                .parse("market://details?id=com.adobe.reader"));
                                startActivity(marketIntent);
                            }
                        });
                builder.setNegativeButton("No, Thanks", null);
                builder.create().show();
            }
        }

If any pdf reader application not available then this code is download pdf reader from android market, But be sure your device has pre-installed **android-market** application. So I think try this on android device, rather then emulator.

将pdf文件转换为文本文件

可以将pdf转换为文本文件。如果你的pdf文件是图像,你想使用OCR 工具。

还有一套用于pdf的解析库和sdk。你可以看到这个 https://stackoverflow.com/questions/4665957/pdf-parsing-library-for-android

在SD卡上存储文件

 btnWriteSDFile.setOnClickListener(new OnClickListener() 
    {
    public void onClick(View v)
    {       

        try {
            File myFile = new File("/sdcard/mysdfile.txt");
            myFile.createNewFile();
            FileOutputStream fOut = new FileOutputStream(myFile);
            OutputStreamWriter myOutWriter =new OutputStreamWriter(fOut);
            myOutWriter.append(txtData.getText());
            myOutWriter.close();
            fOut.close();
            Toast.makeText(v.getContext(),"Done writing SD 'mysdfile.txt'", Toast.LENGTH_SHORT).show();
            txtData.setText("");
        } 
        catch (Exception e) 
        {
            Toast.makeText(v.getContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
        }
    }


    }); 




btnReadSDFile.setOnClickListener(new OnClickListener()
        {

        public void onClick(View v) 
        {

        try {

            File myFile = new File("/sdcard/mysdfile.txt");
            FileInputStream fIn = new FileInputStream(myFile);
            BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
            String aDataRow = "";
            String aBuffer = "";
            while ((aDataRow = myReader.readLine()) != null) 
            {
                aBuffer += aDataRow ;
            }
            txtData.setText(aBuffer);
            myReader.close();
            Toast.makeText(v.getContext(),"Done reading SD 'mysdfile.txt'",Toast.LENGTH_SHORT).show();
        } 
        catch (Exception e)
        {
            Toast.makeText(v.getContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
        }
        }
        }); 

此外,还要在Android.Manifest中写下此权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />