简体   繁体   中英

Android reading pdf metadata - memory issue

I'm currently building an android application that displays a set of pdf files in a ListView. Instead of just displaying the filename I want to grab the Title from the metadata of the pdf and display that in the list, if the file doesnt have a Title set then just use the filename. I'm using iText atm, here is what I have:

File[] filteredFiles = root.listFiles(filter);
for (int i=0;i<filteredFiles.length;i++) {
    try {                   
        File f = filteredFiles[i];                  
        PdfReader reader = new PdfReader(f.getAbsolutePath());
        String title = reader.getInfo().get("Title");
        reader.close();

        //Do other stuff here...
    } catch (Exception e) {
        e.printStackTrace();
    }
}

This works fine, its gets the data I want, but its slowww. Also, sometimes I get memory crashes if the file is over 2MB. Is there a better way of doing this? Maybe a way of getting the metadata without having to actually open the pdf file?

Any help is much appreciated, Thanks.

You can try fast PDFParse library. It optimized for performance & small memory consumption.

File[] filteredFiles = root.listFiles(filter);
for (int i=0;i<filteredFiles.length;i++) {
    try {                   
        File f = filteredFiles[i];                  
        PDFDocument reader = new PDFDocument(f.getAbsolutePath());
        String title = reader.getDocumentInfo().getTitle();
        reader.close();

        //Do other stuff here...
    } catch (Exception e) {
        e.printStackTrace();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM