Skip to content

Here's the C code that I use to get the text. #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

src/main/libs
src/main/obj

.idea
*.iml
19 changes: 18 additions & 1 deletion src/main/java/com/shockwave/pdfium/PdfiumCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.content.Context;
import android.util.Log;
import android.view.Surface;
import android.graphics.RectF;
import android.graphics.PointF;

import java.io.FileDescriptor;
import java.lang.reflect.Field;
Expand All @@ -29,12 +31,27 @@ private native void nativeRenderPage(long pagePtr, Surface surface, int dpi,
int startX, int startY,
int drawSizeHor, int drawSizeVer);

// Text Module
public static native int textLoadPage(long page);
public static native int textFindStart(int textPage, String findWhat, long flags, int startIndex);
public static native int textFindNext(int handle);
public static native int textFindPrev(int handle);
public static native int textGetSchResultIndex(int handle);
public static native int textGetSchCount(int handle);
public static native String textGetText(int textPage, int start, int count);
public static native RectF textGetRect(int textPage, int index);
public static native int textCountRects(int textPage, int start, int count);
public static native int textCountChars(int textPage);
public static native void textFindClose(int handle);
public static native void textClosePage(int textPage);

private static final Class FD_CLASS = FileDescriptor.class;
private static final String FD_FIELD_NAME = "descriptor";
private static Field mFdField = null;

private int mCurrentDpi;


public PdfiumCore(Context ctx){
mCurrentDpi = ctx.getResources().getDisplayMetrics().densityDpi;
}
Expand Down Expand Up @@ -115,7 +132,7 @@ public void renderPage(PdfDocument doc, Surface surface, int pageIndex,
try{
//nativeRenderPage(doc.mNativePagesPtr.get(pageIndex), surface, mCurrentDpi);
nativeRenderPage(doc.mNativePagesPtr.get(pageIndex), surface, mCurrentDpi,
startX, startY, drawSizeX, drawSizeY);
startX, startY, drawSizeX, drawSizeY);
}catch(NullPointerException e){
Log.e(TAG, "mContext may be null");
e.printStackTrace();
Expand Down
135 changes: 135 additions & 0 deletions src/main/jni/src/mainJNILib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ extern "C" {

#include <android/native_window.h>
#include <android/native_window_jni.h>
#include <android/bitmap.h>
#include <utils/Mutex.h>
using namespace android;


#include <fpdfview.h>
#include <fpdfdoc.h>
#include <fpdftext.h>


static Mutex sLibraryLock;
Expand Down Expand Up @@ -255,4 +259,135 @@ JNI_FUNC(void, PdfiumCore, nativeRenderPage)(JNI_ARGS, jlong pagePtr, jobject ob
ANativeWindow_release(nativeWindow);
}


// Text Module API

JNI_FUNC(jint*, PdfiumCore, textLoadPage)(JNI_ARGS, jlong pagePtr){
FPDF_PAGE page = reinterpret_cast<FPDF_PAGE>(pagePtr);
return (jint*)FPDFText_LoadPage(page);
}

JNI_FUNC(void, PdfiumCore, textClosePage)(JNI_ARGS, jint textpage){
FPDF_TEXTPAGE pTextPage = reinterpret_cast<FPDF_TEXTPAGE>(textpage);
FPDFText_ClosePage(pTextPage);
}

JNI_FUNC(jint*, PdfiumCore, textFindStart)(JNI_ARGS, jint textpage, jstring findwhat, jlong flag, jint startindex){

int length = env->GetStringLength(findwhat);
const FPDF_WCHAR* wcFind = env->GetStringChars(findwhat, 0);

FPDF_TEXTPAGE pTextPage = reinterpret_cast<FPDF_TEXTPAGE>(textpage);
FPDF_SCHHANDLE searchHandle = NULL;
LOGI("wcFind is %x %x %x %x",wcFind[0],wcFind[1],wcFind[2],wcFind[3]);

searchHandle = FPDFText_FindStart(pTextPage,(FPDF_WCHAR*)wcFind, flag, startindex);

if(searchHandle == NULL){
LOGE("FPDFTextFindStart: FPDFTextFindStart did not return success");
}

return (jint*)searchHandle;
}

JNI_FUNC(jint, PdfiumCore, textFindNext)(JNI_ARGS, jint searchHandle){

FPDF_SCHHANDLE pSearchHandle = reinterpret_cast<FPDF_SCHHANDLE>(searchHandle);
FPDF_BOOL isMatch = 0;
isMatch = FPDFText_FindNext(pSearchHandle);
LOGD("FPDFText_FindNext Match is %x",isMatch);
return isMatch;
}

// TODO: incomplete
JNI_FUNC(jstring, PdfiumCore, textGetText)(JNI_ARGS, jint textpage, jint nStart, jint nCount){

FPDF_DWORD bufflen = 0;

FPDF_TEXTPAGE pTextPage = reinterpret_cast<FPDF_TEXTPAGE>(textpage);

//TODO: How to fix this ????
FPDF_WCHAR* pBuff = new FPDF_WCHAR[bufflen+1];
pBuff[bufflen] = 0;

int ret = FPDFText_GetText(pTextPage, nStart, nCount, pBuff);

if(ret == 0){
LOGE("FPDFTextGetText: FPDFTextGetText did not return success");
}

return env->NewString(pBuff, bufflen);
}

JNI_FUNC(jint, PdfiumCore, textCountChars)(JNI_ARGS, jint textPage){

FPDF_TEXTPAGE pTextPage = reinterpret_cast<FPDF_TEXTPAGE>(textPage);
int count = 0;
count = FPDFText_CountChars(pTextPage);
return count;
}

JNI_FUNC(jint, PdfiumCore, textCountRects)(JNI_ARGS, jint textPage, jint start, jint count){

FPDF_TEXTPAGE pTextPage = reinterpret_cast<FPDF_TEXTPAGE>(textPage);
int rectCount = 0;
rectCount = FPDFText_CountRects(pTextPage, start, count);
return rectCount;
}


JNI_FUNC(jobject, PdfiumCore, textGetRect)(JNI_ARGS, jint textpage, jint index){

jclass cls_r;
double rectLeft, rectTop, rectRight, rectBottom;
FPDF_TEXTPAGE pTextPage = reinterpret_cast<FPDF_TEXTPAGE>(textpage);

FPDFText_GetRect(pTextPage, index, &rectLeft, &rectTop, &rectRight, &rectBottom);

// get android RectF
cls_r = env->FindClass((const char*)"android/graphics/RectF");
if (cls_r == NULL){
return NULL;
}

jobject obj = env->AllocObject(cls_r);
jfieldID left = env->GetFieldID( cls_r, (const char*)"left", "F");
jfieldID right = env->GetFieldID(cls_r, (const char*)"right", "F");
jfieldID top = env->GetFieldID(cls_r, (const char*)"top", "F");
jfieldID bottom = env->GetFieldID( cls_r, (const char*)"bottom", "F");

env->SetFloatField( obj, left, rectLeft);
env->SetFloatField( obj, right, rectRight);
env->SetFloatField( obj, top, rectTop);
env->SetFloatField( obj, bottom, rectBottom);
return obj;
}

JNI_FUNC(jint, PdfiumCore, textGetSchResultIndex)(JNI_ARGS, jint searchHandle){
FPDF_SCHHANDLE pSearchHandle = reinterpret_cast<FPDF_SCHHANDLE>(searchHandle);
int index = -1;
index = FPDFText_GetSchResultIndex(pSearchHandle);
if(index == -1){
LOGE("FPDFTextGetSchResultIndex: FPDFTextGetSchResultIndex did not return success");
}
return index;
}

JNI_FUNC(jint, PdfiumCore, textGetSchCount)(JNI_ARGS, jint searchHandle){
FPDF_SCHHANDLE pSearchHandle = reinterpret_cast<FPDF_SCHHANDLE>(searchHandle);
int count = -1;
count = FPDFText_GetSchCount(pSearchHandle);
if(count == -1){
LOGE("FPDFTextGetSchCount: FPDFTextGetSchCount did not return success");
}
return count;
}

JNI_FUNC(void, PdfiumCore, textFindClose)(JNI_ARGS, jint searchHandle){
FPDF_SCHHANDLE pSearchHandle = reinterpret_cast<FPDF_SCHHANDLE>(searchHandle);
FPDFText_FindClose(pSearchHandle);
}



}//extern C