Hi all,
I'm using pdfium to implement a pdf annotator for android.
I used the following code in my JNI layer to implement highlight annotation feature. Even though it increases the total annotation count, Seems like annotation do not get rendered into the pdf as I couldn't get it displat in the bitmap image.
Similarly, I implemented the sticky note annotation and it works and renders fine.
Can someone please help me figure out whether this is a fault in my JNI implementation or fault in Android version of pdfium library?
Any tip will be helpful for me.
Thank you
JNI_FUNC(jint, PdfiumCore, nativeAddHighlightAnnotation)(
JNI_ARGS, jlong docPtr,
int page_index,
jstring text_,
jintArray color_, jintArray bound_, jfloatArray quadPoints_, jstring apstring) {
FPDF_PAGE page;
DocumentFile *doc =
reinterpret_cast<DocumentFile *>(docPtr);
jlong pagePtr = loadPageInternal(env, doc, page_index);
if (pagePtr == -1) {
return -1;
}
else {
page =
reinterpret_cast<FPDF_PAGE>(pagePtr);
}
// Get the bound array
jint *bounds = env->GetIntArrayElements(bound_,
NULL);
int boundsLen = (
int) (env->GetArrayLength(bound_));
if (boundsLen != 4) {
return -1;
}
// Set the annotation rectangle.
FS_RECTF rect;
rect.left = bounds[0];
rect.top = bounds[1];
rect.right = bounds[2];
rect.bottom = bounds[3];
// Get the text color
unsigned int R, G, B, A;
jint *colors = env->GetIntArrayElements(color_,
NULL);
int colorsLen = (
int) (env->GetArrayLength(color_));
if (colorsLen == 4) {
R = colors[0];
G = colors[1];
B = colors[2];
A = colors[3];
}
else {
R = 51u;
G = 102u;
B = 153u;
A = 204u;
}
// Add a highlight annotation to the page.
FPDF_ANNOTATION annot = FPDFPage_CreateAnnot(page,
FPDF_ANNOT_HIGHLIGHT);
// set the rectangle of the annotation
bool a = FPDFAnnot_SetRect(annot, &rect);
env->ReleaseIntArrayElements(bound_, bounds, 0);
// Set the color of the annotation.
bool b = FPDFAnnot_SetColor(annot,
FPDFANNOT_COLORTYPE_Color, R, G, B, A);
env->ReleaseIntArrayElements(color_, colors, 0);
unsigned short *kapstring = convertWideString(env, apstring);
FPDFAnnot_SetAP(annot,
FPDF_ANNOT_APPEARANCEMODE_NORMAL,
NULL);
FPDFAnnot_SetAP(annot,
FPDF_ANNOT_APPEARANCEMODE_NORMAL, kapstring);
// Set the content of the annotation.
unsigned short *kContents = convertWideString(env, text_);
bool c = FPDFAnnot_SetStringValue(annot, kContentsKey, kContents);
jfloat *quadPointsF = env->GetFloatArrayElements(quadPoints_,
NULL);
int quadPointLen = (
int) (env->GetArrayLength(quadPoints_));
bool d;
for (
int i = 0; i < quadPointLen / 8; i++)
{
FS_QUADPOINTSF *quadpoints =
new FS_QUADPOINTSF(quadPointsF[8 * i], quadPointsF[8 * i + 1], quadPointsF[8 * i + 2], quadPointsF[8 * i + 3], quadPointsF[8 * i + 4], quadPointsF[8 * i + 5], quadPointsF[8 * i + 6], quadPointsF[8 * i + 7]);
d = FPDFAnnot_AppendAttachmentPoints(annot, quadpoints);
}
int index = FPDFPage_GetAnnotIndex(page, annot);
FPDFPage_CloseAnnot(annot);
// close page
closePageInternal(pagePtr);
return (jint)index;
}
////////////////////////////////////////////loadPageInternal//////////////////////////////////
static jlong loadPageInternal(JNIEnv *env, DocumentFile *doc, int pageIndex) {
try {
if (doc == NULL) throw "Get page document null";
FPDF_DOCUMENT pdfDoc = doc->pdfDocument;
if (pdfDoc != NULL) {
FPDF_PAGE page = FPDF_LoadPage(pdfDoc, pageIndex);
if (page == NULL) {
throw "Loaded page is null";
}
return reinterpret_cast<jlong>(page);
} else {
throw "Get page pdf document null";
}
} catch (const char *msg) {
LOGE("%s", msg);
jniThrowException(env, "java/lang/IllegalStateException",
"cannot load page");
return -1;
}
}
////////////////////////////////////////////
closePageInternal //////////////////////////////////
static void closePageInternal(jlong pagePtr) {
FPDF_ClosePage(reinterpret_cast<FPDF_PAGE>(pagePtr));
}