0

I tried many ways but it's not working for me. I have a pdf file in the asset folder.I want to extract pdf file text and display it on my app. But i am getting error file not found an exception. I feel my asset file path is not correct. I am using iText for achieve this. I tried below code.

  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;

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

  public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    String sAssets = "file:///android_asset/" + "bill.pdf";
    try {
        String parsedText="";

        PdfReader reader = new PdfReader(sAssets);
        int n = reader.getNumberOfPages();
        for (int i = 0; i <n ; i++) {
            parsedText   = parsedText+ PdfTextExtractor.getTextFromPage(reader, i+1).trim()+"\n"; //Extracting the content from the different pages
        }
        System.out.println(parsedText);
        reader.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}
}
James Z
  • 11,838
  • 10
  • 25
  • 41
learner
  • 291
  • 9
  • 21
  • 1
    itext pdf can extract text from pdf and then you can display the same on android. But your question is too broad for any one to answer. – Raghunandan Jul 18 '19 at 04:05
  • Pls check edited question – learner Jul 18 '19 at 04:29
  • 1
    The solution already exists on Java. [The link might have an answer to your question.](https://stackoverflow.com/questions/18098400/how-to-get-raw-text-from-pdf-file-using-java) – Eshwer Aaditya Jul 18 '19 at 04:46
  • 1
    I have used PDFbox years ago, done same task, please try that, if any problem occurs, share your code I'll fix that. – shahzain ali Jul 18 '19 at 05:26
  • I am able to extract pdf text. Now i want to display only specific text.Is that possible? Now its showing in unorder and bulk text.I want to display customer name and bill number only – learner Jul 18 '19 at 05:51
  • Then create a new question or edit your question to include what you really want. Your question included "But i am getting error file not found an exception" and now it's different? This isn't a forum, questions are expected to be about a specific topic and not evolve into something different. The comments are meant for clarifications of the original question. – Tilman Hausherr Jul 18 '19 at 18:09

0 Answers0