PDFbox没有在android上提取区域

时间:2015-06-07 05:22:57

标签: java android pdfbox

我试图仅提取PDF文档中突出显示的文本。它可以在PC上运行,但是当我在Android上使用它时它会失败。 PDFBox并不直接在android上运行,所以我使用的是Birdbrain2 / PdfBox-Android for Android。

以下是适用的PC代码

import java.awt.geom.Rectangle2D;
import java.io.File;
import java.util.List;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
import org.apache.pdfbox.util.PDFTextStripperByArea;

public class ExtractHighlights {
    public static void main(String args[]) {        
        System.out.println(extractHighlights("sample.pdf"));
    }

    public static String extractHighlights(String fileName){
        String extractedText = "";
        try {
            PDDocument pddDocument = PDDocument.load(new File(fileName));
            List allPages = pddDocument.getDocumentCatalog().getAllPages();         
            for (int i = 0; i < allPages.size(); i++) {             
                PDPage page = (PDPage) allPages.get(i);
                List<PDAnnotation> la = page.getAnnotations();
                if (la.size() < 1) {
                    continue;
                }

                for (PDAnnotation pda : la) {
                    PDFTextStripperByArea stripper = new PDFTextStripperByArea();
                    stripper.setSortByPosition(true);

                    PDRectangle rect = pda.getRectangle();
                    float x = rect.getLowerLeftX();
                    float y = rect.getUpperRightY();
                    float width = rect.getWidth();
                    float height = rect.getHeight();
                    int rotation = page.findRotation();
                    if (rotation == 0) {
                        PDRectangle pageSize = page.findMediaBox();
                        y = pageSize.getHeight() - y;
                    }

                    Rectangle2D.Float awtRect = new Rectangle2D.Float(x, y,
                            width, height);
                    stripper.addRegion("0", awtRect);
                    stripper.extractRegions(page);  
                    String highlight = stripper.getTextForRegion("0").trim();
                    if(highlight.length() == 0) continue;
                    extractedText += highlight.substring(0,highlight.length()-2)+" ";
                }               
            }
            pddDocument.close();
            //System.out.println(extractedText);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return extractedText;
    }
}

这是不起作用的android代码

@Override
        protected String doInBackground(String... strings) {
            String extractedText = "";

            try {
                Log.i("ExtractHighlights","Started");
                PDDocument pddDocument = PDDocument.load(new File(strings[0]));
                PDPageTree allPages = pddDocument.getDocumentCatalog().getPages();
                int totalPages = allPages.getCount();
                int pageNumber = 0;
                for (PDPage page:allPages) {
                    publishProgress(pageNumber++,totalPages);
                    Log.i("ExtractHighlights", "Reading page");
                    List<PDAnnotation> la = page.getAnnotations();
                    if (la.size() < 1) {
                        continue;
                    }

                    for (PDAnnotation pda : la) {
                        Log.i("ExtractHighlights","Annotation found");
                        PDFTextStripperByArea stripper = new PDFTextStripperByArea();
                        stripper.setSortByPosition(true);

                        Log.i("ExtractHighlights","Getting rectangle");
                        PDRectangle rect = pda.getRectangle();
                        float x = rect.getLowerLeftX();
                        float y = rect.getUpperRightY();
                        float width = rect.getWidth();
                        float height = rect.getHeight();

                        RectF region = new RectF(x,y,width,height);
                        stripper.addRegion("0",region);
                        Log.i("ExtractHighlights","Extracting regions");
                        stripper.extractRegions(page);
                        Log.i("ExtractHighlights","Getting text from region");
                        String highlight = stripper.getTextForRegion("0").trim();
                        Log.i("ExtractHighlights",highlight);
                        if(highlight.length() == 0) continue;
                        extractedText += highlight.substring(0,highlight.length()-2)+" ";
                    }
                    Log.i("ExtractHighlights","Page done");
                }
                pddDocument.close();
                Log.i("ExtractHighlights","Document closed");
            } catch (Exception ex) {
                ex.printStackTrace();
            }

            return extractedText;
        }

此外,在Android和Android上需要很长时间才会假定程序已崩溃。

我可以尝试将整个PDF转换为文本,但之后我如何知道哪些文字突出显示?

1 个答案:

答案 0 :(得分:1)

正如@mkl指出的那样,这是一个已知的错误。 所以我使用了this家伙的PDFBox端口。它奏效了!