用于在android中处理pdf的PDFBox

时间:2012-03-14 04:55:56

标签: android pdfbox

我正在尝试将pdfbox lib用于我的Android应用程序,但我正在

 java.lang.NoClassDefFoundError: org.pdfbox.pdmodel.PDDocument 

这个错误。我正在开发商业应用程序我不能使用像itext这样的其他Lib。所以我的问题是我们可以在android中使用PDfBox。

这是我的代码: -

PDFParser parser = null;
    String parsedText = null;
    PDFTextStripper pdfStripper;
    PDDocument pdDoc = null;
    COSDocument cosDoc = null;
    PDDocumentInformation pdDocInfo;
    try {
          f =new  File(Environment.getExternalStorageDirectory()+File.separator+"Download"+File.separator+"Services.pdf");

         if(f.exists()){
             System.out.println("---------exists-----------");
         }else{
             System.out.println("------NOT----exists----------");
         }
         parser = new PDFParser(new FileInputStream(f));
    } catch (Exception e) {
        System.out.println("Unable to open PDF Parser.");
        System.out.println("-----------------------error|"+e.toString());
    }

    try {
        parser.parse();
        cosDoc = parser.getDocument();
        pdfStripper = new PDFTextStripper();
        pdDoc = new PDDocument(cosDoc);//here i'm getting exception

        //pdDoc = PDDocument.load(f, false);

        parsedText = pdfStripper.getText(pdDoc);
    } catch (Exception e) {
        System.out.println("-----------------------error|"+e.toString());
        System.out.println("An exception occured in parsing the PDF Document.");
        e.printStackTrace();
        try {
            if (cosDoc != null) cosDoc.close();
            if (pdDoc != null) pdDoc.close();
        } catch (Exception e1) {
            e.printStackTrace();
        }

    }
    System.out.println("Done.");
    System.out.println("-----------------------parsedText|"+parsedText);

使用PDFBox 0.7.3 jar

3 个答案:

答案 0 :(得分:2)

似乎PDFBox依赖于Android设备上没有的awt和swing类。

因此,您无法在Android上使用PDFBox。

答案 1 :(得分:0)

当JVM无法加载类时,抛出NoClassDefFoundError。
As the javadoc says
您是否在编译期间将pdfbox库包含在类路径中?

答案 2 :(得分:0)

如果您只需要在Android中从PDF文档中提取文本,请使用此https://github.com/RatheeshRavindran/PDFBoxLight 我最近将PDFBox移植到Android,但请注意,这仍然是Beta版。