1

我想使用 iText 从互联网上的 pdf 文件中提取文本到 java 中的字符串。这是我的代码:

package me.moop.mytwitter;

import java.io.IOException;
import java.text.ParseException;

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.app.ProgressDialog;

public class MainActivity extends Activity {

    Button mBtnCheck;
    EditText mEtxtGroup;
    ProgressDialog mProgressDialog;
    TextView mTxtv1;

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

        mBtnCheck = (Button) findViewById(R.id.btnCheck);
        mEtxtGroup = (EditText) findViewById(R.id.etxtGroup);
        mTxtv1 = (TextView) findViewById(R.id.textView1);

    }

      public void checkScheduleChange(View view){
        if (view == mBtnCheck){
            String group;
            group = mEtxtGroup.getText().toString();
            if (group.length() > 0){
                mProgressDialog = new ProgressDialog(this);
                mProgressDialog.setMessage("Bezig met checken voor roosterwijzigingen...");
                mProgressDialog.show();
                new CheckScheduleChangeTask().execute();
            }
            else{
                Toast.makeText(this, "Voer een klas in", Toast.LENGTH_LONG).show();
            }
        }
    }

    private class CheckScheduleChangeTask extends AsyncTask<Void, Void, Void> {

        String content;

        @Override
        protected Void doInBackground(Void... args) {

            PdfReader reader = null;
            try {
                reader = new PdfReader("http://www.augustinianum.eu/roosterwijzigingen/14062012.pdf");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                content = PdfTextExtractor.getTextFromPage(reader, 1);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void arg) {
            mProgressDialog.dismiss();

                String group = mEtxtGroup.getText().toString();
                if (content.contains(group)){
                    Toast.makeText(MainActivity.this, "U hebt een roosterwijziging", Toast.LENGTH_LONG).show();
                }   
                mTxtv1.setText(content);
        }
    }
}

对于赶时间的人,这里是 iText 部分:

private class CheckScheduleChangeTask extends AsyncTask<Void, Void, Void> {

    String content;

    @Override
    protected Void doInBackground(Void... args) {

        PdfReader reader = null;
        try {
            reader = new PdfReader("http://www.augustinianum.eu/roosterwijzigingen/14062012.pdf");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            content = PdfTextExtractor.getTextFromPage(reader, 1);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void arg) {
    // more code
    }

每当我在我的模拟器中启动它时,我都会在调用类后接近:CheckScheduleChangeTask。这是日志:

07-27 09:15:08.354: D/ddm-heap(222): Got feature list request
07-27 09:15:12.994: E/dalvikvm(222): Could not find class 'com.itextpdf.text.pdf.PdfReader', referenced from method me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground
07-27 09:15:13.004: W/dalvikvm(222): VFY: unable to resolve new-instance 410 (Lcom/itextpdf/text/pdf/PdfReader;) in Lme/moop/mytwitter/MainActivity$CheckScheduleChangeTask;
07-27 09:15:13.004: D/dalvikvm(222): VFY: replacing opcode 0x22 at 0x0002
07-27 09:15:13.004: D/dalvikvm(222): Making a copy of Lme/moop/mytwitter/MainActivity$CheckScheduleChangeTask;.doInBackground code (137 bytes)
07-27 09:15:13.004: E/dalvikvm(222): Could not find class 'com.itextpdf.text.pdf.parser.PdfReaderContentParser', referenced from method me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground
07-27 09:15:13.004: W/dalvikvm(222): VFY: unable to resolve new-instance 411 (Lcom/itextpdf/text/pdf/parser/PdfReaderContentParser;) in Lme/moop/mytwitter/MainActivity$CheckScheduleChangeTask;
07-27 09:15:13.004: D/dalvikvm(222): VFY: replacing opcode 0x22 at 0x000a
07-27 09:15:13.045: W/dalvikvm(222): threadid=15: thread exiting with uncaught exception (group=0x4001b188)
07-27 09:15:13.045: E/AndroidRuntime(222): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
07-27 09:15:13.135: E/AndroidRuntime(222): java.lang.RuntimeException: An error occured while executing doInBackground()
07-27 09:15:13.135: E/AndroidRuntime(222):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
07-27 09:15:13.135: E/AndroidRuntime(222):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-27 09:15:13.135: E/AndroidRuntime(222):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-27 09:15:13.135: E/AndroidRuntime(222):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-27 09:15:13.135: E/AndroidRuntime(222):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-27 09:15:13.135: E/AndroidRuntime(222):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
07-27 09:15:13.135: E/AndroidRuntime(222):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
07-27 09:15:13.135: E/AndroidRuntime(222):  at java.lang.Thread.run(Thread.java:1096)
07-27 09:15:13.135: E/AndroidRuntime(222): Caused by: java.lang.NoClassDefFoundError: com.itextpdf.text.pdf.PdfReader
07-27 09:15:13.135: E/AndroidRuntime(222):  at me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground(MainActivity.java:84)
07-27 09:15:13.135: E/AndroidRuntime(222):  at me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground(MainActivity.java:1)
07-27 09:15:13.135: E/AndroidRuntime(222):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-27 09:15:13.135: E/AndroidRuntime(222):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-27 09:15:13.135: E/AndroidRuntime(222):  ... 4 more
07-27 09:15:13.164: I/dalvikvm(222): threadid=7: reacting to signal 3
07-27 09:15:13.394: I/dalvikvm(222): Wrote stack trace to '/data/anr/traces.txt'
07-27 09:15:16.894: I/Process(222): Sending signal. PID: 222 SIG: 9
07-27 09:17:59.094: E/dalvikvm(252): Could not find class 'com.itextpdf.text.pdf.PdfReader', referenced from method me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground
07-27 09:17:59.094: W/dalvikvm(252): VFY: unable to resolve new-instance 410 (Lcom/itextpdf/text/pdf/PdfReader;) in Lme/moop/mytwitter/MainActivity$CheckScheduleChangeTask;
07-27 09:17:59.094: D/dalvikvm(252): VFY: replacing opcode 0x22 at 0x0002
07-27 09:17:59.094: D/dalvikvm(252): Making a copy of Lme/moop/mytwitter/MainActivity$CheckScheduleChangeTask;.doInBackground code (137 bytes)
07-27 09:17:59.094: E/dalvikvm(252): Could not find class 'com.itextpdf.text.pdf.parser.PdfReaderContentParser', referenced from method me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground
07-27 09:17:59.094: W/dalvikvm(252): VFY: unable to resolve new-instance 411 (Lcom/itextpdf/text/pdf/parser/PdfReaderContentParser;) in Lme/moop/mytwitter/MainActivity$CheckScheduleChangeTask;
07-27 09:17:59.094: D/dalvikvm(252): VFY: replacing opcode 0x22 at 0x000a
07-27 09:17:59.124: W/dalvikvm(252): threadid=15: thread exiting with uncaught exception (group=0x4001b188)
07-27 09:17:59.124: E/AndroidRuntime(252): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
07-27 09:17:59.134: E/AndroidRuntime(252): java.lang.RuntimeException: An error occured while executing doInBackground()
07-27 09:17:59.134: E/AndroidRuntime(252):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
07-27 09:17:59.134: E/AndroidRuntime(252):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-27 09:17:59.134: E/AndroidRuntime(252):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-27 09:17:59.134: E/AndroidRuntime(252):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-27 09:17:59.134: E/AndroidRuntime(252):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-27 09:17:59.134: E/AndroidRuntime(252):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
07-27 09:17:59.134: E/AndroidRuntime(252):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
07-27 09:17:59.134: E/AndroidRuntime(252):  at java.lang.Thread.run(Thread.java:1096)
07-27 09:17:59.134: E/AndroidRuntime(252): Caused by: java.lang.NoClassDefFoundError: com.itextpdf.text.pdf.PdfReader
07-27 09:17:59.134: E/AndroidRuntime(252):  at me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground(MainActivity.java:70)
07-27 09:17:59.134: E/AndroidRuntime(252):  at me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground(MainActivity.java:1)
07-27 09:17:59.134: E/AndroidRuntime(252):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-27 09:17:59.134: E/AndroidRuntime(252):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-27 09:17:59.134: E/AndroidRuntime(252):  ... 4 more
07-27 09:17:59.521: I/dalvikvm(252): threadid=7: reacting to signal 3
07-27 09:17:59.794: I/dalvikvm(252): Wrote stack trace to '/data/anr/traces.txt'
07-27 09:18:02.743: I/Process(252): Sending signal. PID: 252 SIG: 9
07-27 09:26:26.713: E/dalvikvm(280): Could not find class 'com.itextpdf.text.pdf.PdfReader', referenced from method me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground
07-27 09:26:26.723: W/dalvikvm(280): VFY: unable to resolve new-instance 410 (Lcom/itextpdf/text/pdf/PdfReader;) in Lme/moop/mytwitter/MainActivity$CheckScheduleChangeTask;
07-27 09:26:26.723: D/dalvikvm(280): VFY: replacing opcode 0x22 at 0x0001
07-27 09:26:26.723: D/dalvikvm(280): Making a copy of Lme/moop/mytwitter/MainActivity$CheckScheduleChangeTask;.doInBackground code (93 bytes)
07-27 09:26:26.723: I/dalvikvm(280): Could not find method com.itextpdf.text.pdf.parser.PdfTextExtractor.getTextFromPage, referenced from method me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground
07-27 09:26:26.723: W/dalvikvm(280): VFY: unable to resolve static method 2969: Lcom/itextpdf/text/pdf/parser/PdfTextExtractor;.getTextFromPage (Lcom/itextpdf/text/pdf/PdfReader;I)Ljava/lang/String;
07-27 09:26:26.723: D/dalvikvm(280): VFY: replacing opcode 0x71 at 0x000a
07-27 09:26:26.754: W/dalvikvm(280): threadid=15: thread exiting with uncaught exception (group=0x4001b188)
07-27 09:26:26.754: E/AndroidRuntime(280): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
07-27 09:26:26.763: E/AndroidRuntime(280): java.lang.RuntimeException: An error occured while executing doInBackground()
07-27 09:26:26.763: E/AndroidRuntime(280):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
07-27 09:26:26.763: E/AndroidRuntime(280):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-27 09:26:26.763: E/AndroidRuntime(280):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-27 09:26:26.763: E/AndroidRuntime(280):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-27 09:26:26.763: E/AndroidRuntime(280):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-27 09:26:26.763: E/AndroidRuntime(280):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
07-27 09:26:26.763: E/AndroidRuntime(280):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
07-27 09:26:26.763: E/AndroidRuntime(280):  at java.lang.Thread.run(Thread.java:1096)
07-27 09:26:26.763: E/AndroidRuntime(280): Caused by: java.lang.NoClassDefFoundError: com.itextpdf.text.pdf.PdfReader
07-27 09:26:26.763: E/AndroidRuntime(280):  at me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground(MainActivity.java:68)
07-27 09:26:26.763: E/AndroidRuntime(280):  at me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground(MainActivity.java:1)
07-27 09:26:26.763: E/AndroidRuntime(280):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-27 09:26:26.763: E/AndroidRuntime(280):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-27 09:26:26.763: E/AndroidRuntime(280):  ... 4 more
07-27 09:26:27.063: I/dalvikvm(280): threadid=7: reacting to signal 3
07-27 09:26:27.404: I/dalvikvm(280): Wrote stack trace to '/data/anr/traces.txt'
07-27 09:46:50.573: E/dalvikvm(308): Could not find class 'com.itextpdf.text.pdf.PdfReader', referenced from method me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground
07-27 09:46:50.573: W/dalvikvm(308): VFY: unable to resolve new-instance 410 (Lcom/itextpdf/text/pdf/PdfReader;) in Lme/moop/mytwitter/MainActivity$CheckScheduleChangeTask;
07-27 09:46:50.573: D/dalvikvm(308): VFY: replacing opcode 0x22 at 0x0001
07-27 09:46:50.573: D/dalvikvm(308): Making a copy of Lme/moop/mytwitter/MainActivity$CheckScheduleChangeTask;.doInBackground code (97 bytes)
07-27 09:46:50.573: I/dalvikvm(308): Could not find method com.itextpdf.text.pdf.parser.PdfTextExtractor.getTextFromPage, referenced from method me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground
07-27 09:46:50.573: W/dalvikvm(308): VFY: unable to resolve static method 2969: Lcom/itextpdf/text/pdf/parser/PdfTextExtractor;.getTextFromPage (Lcom/itextpdf/text/pdf/PdfReader;I)Ljava/lang/String;
07-27 09:46:50.583: D/dalvikvm(308): VFY: replacing opcode 0x71 at 0x000a
07-27 09:46:50.624: W/dalvikvm(308): threadid=15: thread exiting with uncaught exception (group=0x4001b188)
07-27 09:46:50.624: E/AndroidRuntime(308): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
07-27 09:46:50.634: E/AndroidRuntime(308): java.lang.RuntimeException: An error occured while executing doInBackground()
07-27 09:46:50.634: E/AndroidRuntime(308):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
07-27 09:46:50.634: E/AndroidRuntime(308):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-27 09:46:50.634: E/AndroidRuntime(308):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-27 09:46:50.634: E/AndroidRuntime(308):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-27 09:46:50.634: E/AndroidRuntime(308):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-27 09:46:50.634: E/AndroidRuntime(308):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
07-27 09:46:50.634: E/AndroidRuntime(308):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
07-27 09:46:50.634: E/AndroidRuntime(308):  at java.lang.Thread.run(Thread.java:1096)
07-27 09:46:50.634: E/AndroidRuntime(308): Caused by: java.lang.NoClassDefFoundError: com.itextpdf.text.pdf.PdfReader
07-27 09:46:50.634: E/AndroidRuntime(308):  at me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground(MainActivity.java:68)
07-27 09:46:50.634: E/AndroidRuntime(308):  at me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground(MainActivity.java:1)
07-27 09:46:50.634: E/AndroidRuntime(308):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-27 09:46:50.634: E/AndroidRuntime(308):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-27 09:46:50.634: E/AndroidRuntime(308):  ... 4 more
07-27 09:46:51.105: I/dalvikvm(308): threadid=7: reacting to signal 3
07-27 09:46:51.423: I/dalvikvm(308): Wrote stack trace to '/data/anr/traces.txt'
07-27 09:46:54.444: I/Process(308): Sending signal. PID: 308 SIG: 9

进口好像有问题。有谁知道如何解决这一问题?

谢谢你!

4

3 回答 3

1

它使用 iText 完成,非常简单。但是,如果有人对此有疑问,请问我。

顺便说一句,上述问题的解决方案在这里: https ://stackoverflow.com/a/10046725/1289716

于 2012-08-02T18:48:41.877 回答
0

您可以使用这个从文本创建 pdf 的简单示例 :)

使用网站上提供的库 droidtext0.2jar ..。

已编辑

你添加了你的库来构建路径?如果没有,则添加它。

于 2012-07-27T10:10:03.700 回答
-1

您可能还需要导入这些:

import com.lowagie.text.BadElementException; 
import com.lowagie.text.Document; 
import com.lowagie.text.DocumentException; 
import com.lowagie.text.Image; 
import com.lowagie.text.pdf.BaseFont; 
import com.lowagie.text.pdf.PdfContentByte; 
import com.lowagie.text.pdf.PdfWriter; 
于 2012-07-27T10:12:59.850 回答