How to extract text from PDF file programmatically android studio

Xml file

<RelativeLayout
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical"
   
tools:context=".MainActivity">

    <
ScrollView
       
android:layout_width="match_parent"
       
android:layout_height="match_parent">

       
<!--text view for displaying our extracted text-->
       
<TextView
           
android:id="@+id/id_txtPDF"
            
android:layout_width="match_parent"
           
android:layout_height="match_parent" />

    </
ScrollView>

    <
LinearLayout
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:orientation="vertical"
       
android:layout_alignParentBottom="true">

        <
Button
           
android:id="@+id/id_btnExtract"
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:layout_centerHorizontal="true"
           
android:layout_marginBottom="20dp"
           
android:text="Extract Text from PDF"
           
android:textAllCaps="false" />


    </
LinearLayout>


</RelativeLayout>

 

Java Class

public class MainActivity extends AppCompatActivity {
   
private Button btn_extractPDF;     private TextView txt_extract;     String str_extract_Txt = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   
txt_extract = findViewById(R.id.id_txtPDF);     btn_extractPDF = findViewById(R.id.id_btnExtract);
btn_extractPDF.setOnClickListener(new View.OnClickListener() {
       
@Override         public void onClick(View v) {                     extractPDFFile();
        }
    });
}
private void extractPDFFile() {
       
try {
        

           
PdfReader reader = new PdfReader("res/raw/mubashir_hussain_cover_letter.pdf");             int n = reader.getNumberOfPages();
              for (int i = 0; i < n; i++) {
                str_extract_Txt = str_extract_Txt + PdfTextExtractor.getTextFromPage(reader, i + 1).trim() + "\n";

           
}

           
txt_extract.setText(str_extract_Txt);

           
reader.close();
        }
catch (Exception e) {

           
txt_extract.setText("Error found is : \n" + e);
        }
    }
}

 

Post a Comment

Previous Post Next Post