1 /* PDFPage interface */
2 
3 JNIEXPORT jobject JNICALL
FUN(PDFPage_createAnnotation)4 FUN(PDFPage_createAnnotation)(JNIEnv *env, jobject self, jint subtype)
5 {
6 	fz_context *ctx = get_context(env);
7 	pdf_page *page = from_PDFPage(env, self);
8 	pdf_annot *annot = NULL;
9 
10 	if (!ctx || !page) return NULL;
11 
12 	fz_try(ctx)
13 		annot = pdf_create_annot(ctx, page, subtype);
14 	fz_catch(ctx)
15 		jni_rethrow(env, ctx);
16 
17 	return to_PDFAnnotation_safe_own(ctx, env, annot);
18 }
19 
20 JNIEXPORT void JNICALL
FUN(PDFPage_deleteAnnotation)21 FUN(PDFPage_deleteAnnotation)(JNIEnv *env, jobject self, jobject jannot)
22 {
23 	fz_context *ctx = get_context(env);
24 	pdf_page *page = from_PDFPage(env, self);
25 	pdf_annot *annot = from_PDFAnnotation(env, jannot);
26 
27 	if (!ctx || !page) return;
28 
29 	fz_try(ctx)
30 		pdf_delete_annot(ctx, page, annot);
31 	fz_catch(ctx)
32 		jni_rethrow_void(env, ctx);
33 }
34 
35 JNIEXPORT jboolean JNICALL
FUN(PDFPage_update)36 FUN(PDFPage_update)(JNIEnv *env, jobject self)
37 {
38 	fz_context *ctx = get_context(env);
39 	pdf_page *page = from_PDFPage(env, self);
40 	jboolean changed = JNI_FALSE;
41 
42 	if (!ctx || !page) return JNI_FALSE;
43 
44 	fz_try(ctx)
45 		changed = pdf_update_page(ctx, page);
46 	fz_catch(ctx)
47 		jni_rethrow(env, ctx);
48 
49 	return changed;
50 }
51 
52 JNIEXPORT jboolean JNICALL
FUN(PDFPage_applyRedactions)53 FUN(PDFPage_applyRedactions)(JNIEnv *env, jobject self, jboolean blackBoxes, jint imageMethod)
54 {
55 	fz_context *ctx = get_context(env);
56 	pdf_page *page = from_PDFPage(env, self);
57 	jboolean redacted = JNI_FALSE;
58 	pdf_redact_options opts = { blackBoxes, imageMethod };
59 
60 	if (!ctx || !page) return JNI_FALSE;
61 
62 	fz_try(ctx)
63 		redacted = pdf_redact_page(ctx, page->doc, page, &opts);
64 	fz_catch(ctx)
65 		jni_rethrow(env, ctx);
66 
67 	return redacted;
68 }
69 
70 JNIEXPORT jobject JNICALL
FUN(PDFPage_getAnnotations)71 FUN(PDFPage_getAnnotations)(JNIEnv *env, jobject self)
72 {
73 	fz_context *ctx = get_context(env);
74 	pdf_page *page = from_PDFPage(env, self);
75 	pdf_annot *annot = NULL;
76 	pdf_annot *annots = NULL;
77 	jobject jannots = NULL;
78 	int count;
79 	int i;
80 
81 	if (!ctx || !page) return NULL;
82 
83 	/* count the annotations */
84 	fz_try(ctx)
85 	{
86 		annots = pdf_first_annot(ctx, page);
87 
88 		annot = annots;
89 		for (count = 0; annot; count++)
90 			annot = pdf_next_annot(ctx, annot);
91 	}
92 	fz_catch(ctx)
93 		jni_rethrow(env, ctx);
94 
95 	/* no annotations, return NULL instead of empty array */
96 	if (count == 0)
97 		return NULL;
98 
99 	/* now run through actually creating the annotation objects */
100 	jannots = (*env)->NewObjectArray(env, count, cls_PDFAnnotation, NULL);
101 	if (!jannots || (*env)->ExceptionCheck(env)) jni_throw_null(env, "cannot wrap page annotations in object array");
102 
103 	annot = annots;
104 	for (i = 0; annot && i < count; i++)
105 	{
106 		jobject jannot = to_PDFAnnotation_safe(ctx, env, annot);
107 		if (!jannot) return NULL;
108 
109 		(*env)->SetObjectArrayElement(env, jannots, i, jannot);
110 		if ((*env)->ExceptionCheck(env)) return NULL;
111 
112 		(*env)->DeleteLocalRef(env, jannot);
113 
114 		fz_try(ctx)
115 			annot = pdf_next_annot(ctx, annot);
116 		fz_catch(ctx)
117 			jni_rethrow(env, ctx);
118 	}
119 
120 	return jannots;
121 }
122 
123 JNIEXPORT jobjectArray JNICALL
FUN(PDFPage_getWidgetsNative)124 FUN(PDFPage_getWidgetsNative)(JNIEnv *env, jobject self)
125 {
126 	fz_context *ctx = get_context(env);
127 	pdf_page *page = from_PDFPage(env, self);
128 	pdf_widget *widget = NULL;
129 	pdf_widget *widgets = NULL;
130 	jobjectArray jwidgets = NULL;
131 	int count;
132 	int i;
133 
134 	if (!ctx || !page) return NULL;
135 
136 	/* count the widgets */
137 	fz_try(ctx)
138 	{
139 		widgets = pdf_first_widget(ctx, page);
140 
141 		widget = widgets;
142 		for (count = 0; widget; count++)
143 			widget = pdf_next_widget(ctx, widget);
144 	}
145 	fz_catch(ctx)
146 		jni_rethrow(env, ctx);
147 
148 	/* no widgegts, return NULL instead of empty array */
149 	if (count == 0)
150 		return NULL;
151 
152 	/* now run through actually creating the widget objects */
153 	jwidgets = (*env)->NewObjectArray(env, count, cls_PDFWidget, NULL);
154 	if (!jwidgets || (*env)->ExceptionCheck(env)) jni_throw_null(env, "cannot wrap page widgets in object array");
155 
156 	widget = widgets;
157 	for (i = 0; widget && i < count; i++)
158 	{
159 		jobject jwidget = NULL;
160 
161 		if (widget)
162 		{
163 			jwidget = to_PDFWidget_safe(ctx, env, widget);
164 			if (!jwidget) return NULL;
165 		}
166 
167 		(*env)->SetObjectArrayElement(env, jwidgets, i, jwidget);
168 		if ((*env)->ExceptionCheck(env)) return NULL;
169 
170 		(*env)->DeleteLocalRef(env, jwidget);
171 
172 		fz_try(ctx)
173 			widget = pdf_next_widget(ctx, widget);
174 		fz_catch(ctx)
175 			jni_rethrow(env, ctx);
176 	}
177 
178 	return jwidgets;
179 }
180