简体   繁体   中英

How can i parse PDF file in android?

I am developing android application. This application allows the user to highlight words in PDF file. Then these words must be extracted, so how can I parse the PDF file to get extracted words from the file without any library? Can any one help me?

You have to code it yourself if you don't want to use a libary. There isn't any build-in classes in Android SDK for PDF manipulation.

However you have to use a Libary and have a look at this question:

PDF Library to rendering the PDF files in Android

You can parse PDF easily on android easily using the iText library as like I've done it on my project by parsing a pdf file from assets and displaying it in the android listview.

iTextG Link: https://developers.itextpdf.com/itextg-android

assetManager = getAssets();

try{
    InputStream inputStream = assetManager.open("sample.pdf");
    String parsedText = "";
    PdfReader reader = new PdfReader(inputStream);
    int n = reader.getNumberOfPages();

    for (int i = 0; i < n; i++)
        parsedText = parsedText + PdfTextExtractor.getTextFromPage(reader, i + 1).trim() + "\n";

    String[] data = parsedText.split("\n");

    for(String d : data){
        patients.add(d);
    }

    reader.close();

}catch (FileNotFoundException e){
    Toast.makeText(this, "File Not Found", Toast.LENGTH_SHORT).show();
}catch (IOException e){
    Toast.makeText(this, "IO Error reading input stream", Toast.LENGTH_SHORT).show();
}

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