|
| 1 | +package io.hexawallet.hexa; |
| 2 | +import android.content.ContentResolver; |
| 3 | +import android.database.Cursor; |
| 4 | +import android.net.Uri; |
| 5 | +import android.os.Build; |
| 6 | +import android.provider.DocumentsContract; |
| 7 | +import android.provider.MediaStore; |
| 8 | +import android.util.Log; |
| 9 | +import android.view.WindowManager; |
| 10 | + |
| 11 | +import androidx.annotation.NonNull; |
| 12 | + |
| 13 | +import com.facebook.react.bridge.Promise; |
| 14 | +import com.facebook.react.bridge.ReactApplicationContext; |
| 15 | +import com.facebook.react.bridge.ReactContextBaseJavaModule; |
| 16 | +import com.facebook.react.bridge.ReactMethod; |
| 17 | +import com.facebook.react.bridge.WritableArray; |
| 18 | +import com.facebook.react.bridge.WritableNativeArray; |
| 19 | +import com.lowagie.text.pdf.PdfReader; |
| 20 | +import com.lowagie.text.pdf.RandomAccessFileOrArray; |
| 21 | +import com.lowagie.text.pdf.parser.PdfTextExtractor; |
| 22 | + |
| 23 | +import java.io.BufferedInputStream; |
| 24 | +import java.io.FileInputStream; |
| 25 | + |
| 26 | +import java.io.File; |
| 27 | +import java.io.FileInputStream; |
| 28 | +import java.io.IOException; |
| 29 | +import java.io.InputStream; |
| 30 | +import java.util.ArrayList; |
| 31 | + |
| 32 | +public class PDFModule extends ReactContextBaseJavaModule { |
| 33 | + |
| 34 | + public static ReactApplicationContext context; |
| 35 | + public PDFModule(ReactApplicationContext reactContext) { |
| 36 | + super(reactContext); |
| 37 | + context=reactContext; |
| 38 | + |
| 39 | + } |
| 40 | + |
| 41 | + @NonNull |
| 42 | + @Override |
| 43 | + public String getName() { |
| 44 | + return "PDFModule"; |
| 45 | + } |
| 46 | + |
| 47 | + public static InputStream fileToInputStream(File file) throws IOException { |
| 48 | + FileInputStream fileInputStream = new FileInputStream(file); |
| 49 | + return fileInputStream; |
| 50 | + } |
| 51 | + |
| 52 | + public static String getAbsolutePathFromContentUri(Uri contentUri) { |
| 53 | + String absolutePath = null; |
| 54 | + |
| 55 | + String[] projection = {MediaStore.Images.Media.DATA}; |
| 56 | + ContentResolver contentResolver = context.getContentResolver(); |
| 57 | + |
| 58 | + Cursor cursor = contentResolver.query(contentUri, projection, null, null, null); |
| 59 | + if (cursor != null) { |
| 60 | + int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); |
| 61 | + cursor.moveToFirst(); |
| 62 | + absolutePath = cursor.getString(columnIndex); |
| 63 | + cursor.close(); |
| 64 | + } |
| 65 | + |
| 66 | + return absolutePath; |
| 67 | + } |
| 68 | + |
| 69 | + public static String getPath(final Uri uri) { |
| 70 | + if ("file".equalsIgnoreCase(uri.getScheme())) { |
| 71 | + return uri.getPath(); |
| 72 | + } |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + @ReactMethod |
| 77 | + public void pdfToText(String path, Promise resolve) { |
| 78 | + try{ |
| 79 | + PdfReader pdfReader = new PdfReader(path); |
| 80 | + StringBuilder extractedText = new StringBuilder(); |
| 81 | + PdfTextExtractor textExtractor = new PdfTextExtractor(pdfReader); |
| 82 | + WritableArray pages = new WritableNativeArray(); |
| 83 | + for (int pageIndex = 1; pageIndex <= pdfReader.getNumberOfPages(); pageIndex++) { |
| 84 | + pages.pushString(textExtractor.getTextFromPage(pageIndex)); |
| 85 | + } |
| 86 | + pdfReader.close(); |
| 87 | + resolve.resolve(pages); |
| 88 | + }catch (Exception e) { |
| 89 | + Log.d("ExtractedText error", e.toString()); |
| 90 | + resolve.resolve(e.toString()); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments