pdfpage.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // Copyright (C) 2004-2025 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. /* PDFPage interface */
  23. JNIEXPORT jobject JNICALL
  24. FUN(PDFPage_createAnnotation)(JNIEnv *env, jobject self, jint type)
  25. {
  26. fz_context *ctx = get_context(env);
  27. pdf_page *page = from_PDFPage(env, self);
  28. pdf_annot *annot = NULL;
  29. if (!ctx || !page) return NULL;
  30. fz_try(ctx)
  31. annot = pdf_create_annot(ctx, page, type);
  32. fz_catch(ctx)
  33. jni_rethrow(env, ctx);
  34. return to_PDFAnnotation_safe_own(ctx, env, annot);
  35. }
  36. JNIEXPORT void JNICALL
  37. FUN(PDFPage_deleteAnnotation)(JNIEnv *env, jobject self, jobject jannot)
  38. {
  39. fz_context *ctx = get_context(env);
  40. pdf_page *page = from_PDFPage(env, self);
  41. pdf_annot *annot = from_PDFAnnotation(env, jannot);
  42. if (!ctx || !page) return;
  43. fz_try(ctx)
  44. pdf_delete_annot(ctx, page, annot);
  45. fz_catch(ctx)
  46. jni_rethrow_void(env, ctx);
  47. }
  48. JNIEXPORT jboolean JNICALL
  49. FUN(PDFPage_update)(JNIEnv *env, jobject self)
  50. {
  51. fz_context *ctx = get_context(env);
  52. pdf_page *page = from_PDFPage(env, self);
  53. jboolean changed = JNI_FALSE;
  54. if (!ctx || !page) return JNI_FALSE;
  55. fz_try(ctx)
  56. changed = pdf_update_page(ctx, page);
  57. fz_catch(ctx)
  58. jni_rethrow(env, ctx);
  59. return changed;
  60. }
  61. JNIEXPORT jboolean JNICALL
  62. FUN(PDFPage_applyRedactions)(JNIEnv *env, jobject self, jboolean blackBoxes, jint imageMethod, jint lineArt, jint text)
  63. {
  64. fz_context *ctx = get_context(env);
  65. pdf_page *page = from_PDFPage(env, self);
  66. jboolean redacted = JNI_FALSE;
  67. pdf_redact_options opts = { blackBoxes, imageMethod, lineArt, text };
  68. if (!ctx || !page) return JNI_FALSE;
  69. fz_try(ctx)
  70. redacted = pdf_redact_page(ctx, page->doc, page, &opts);
  71. fz_catch(ctx)
  72. jni_rethrow(env, ctx);
  73. return redacted;
  74. }
  75. JNIEXPORT jobject JNICALL
  76. FUN(PDFPage_getAnnotations)(JNIEnv *env, jobject self)
  77. {
  78. fz_context *ctx = get_context(env);
  79. pdf_page *page = from_PDFPage(env, self);
  80. pdf_annot *annot = NULL;
  81. pdf_annot *annots = NULL;
  82. jobject jannots = NULL;
  83. int count;
  84. int i;
  85. if (!ctx || !page) return NULL;
  86. /* count the annotations */
  87. fz_try(ctx)
  88. {
  89. annots = pdf_first_annot(ctx, page);
  90. annot = annots;
  91. for (count = 0; annot; count++)
  92. annot = pdf_next_annot(ctx, annot);
  93. }
  94. fz_catch(ctx)
  95. jni_rethrow(env, ctx);
  96. /* no annotations, return NULL instead of empty array */
  97. if (count == 0)
  98. return NULL;
  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. annot = annots;
  103. for (i = 0; annot && i < count; i++)
  104. {
  105. jobject jannot = to_PDFAnnotation_safe(ctx, env, annot);
  106. if (!jannot) return NULL;
  107. (*env)->SetObjectArrayElement(env, jannots, i, jannot);
  108. if ((*env)->ExceptionCheck(env)) return NULL;
  109. (*env)->DeleteLocalRef(env, jannot);
  110. fz_try(ctx)
  111. annot = pdf_next_annot(ctx, annot);
  112. fz_catch(ctx)
  113. jni_rethrow(env, ctx);
  114. }
  115. return jannots;
  116. }
  117. JNIEXPORT jobjectArray JNICALL
  118. FUN(PDFPage_getWidgets)(JNIEnv *env, jobject self)
  119. {
  120. fz_context *ctx = get_context(env);
  121. pdf_page *page = from_PDFPage(env, self);
  122. pdf_annot *widget = NULL;
  123. pdf_annot *widgets = NULL;
  124. jobjectArray jwidgets = NULL;
  125. int count;
  126. int i;
  127. if (!ctx || !page) return NULL;
  128. /* count the widgets */
  129. fz_try(ctx)
  130. {
  131. widgets = pdf_first_widget(ctx, page);
  132. widget = widgets;
  133. for (count = 0; widget; count++)
  134. widget = pdf_next_widget(ctx, widget);
  135. }
  136. fz_catch(ctx)
  137. jni_rethrow(env, ctx);
  138. /* no widgets, return NULL instead of empty array */
  139. if (count == 0)
  140. return NULL;
  141. /* now run through actually creating the widget objects */
  142. jwidgets = (*env)->NewObjectArray(env, count, cls_PDFWidget, NULL);
  143. if (!jwidgets || (*env)->ExceptionCheck(env)) jni_throw_null(env, "cannot wrap page widgets in object array");
  144. widget = widgets;
  145. for (i = 0; widget && i < count; i++)
  146. {
  147. jobject jwidget = NULL;
  148. if (widget)
  149. {
  150. jwidget = to_PDFWidget_safe(ctx, env, widget);
  151. if (!jwidget) return NULL;
  152. }
  153. (*env)->SetObjectArrayElement(env, jwidgets, i, jwidget);
  154. if ((*env)->ExceptionCheck(env)) return NULL;
  155. (*env)->DeleteLocalRef(env, jwidget);
  156. fz_try(ctx)
  157. widget = pdf_next_widget(ctx, widget);
  158. fz_catch(ctx)
  159. jni_rethrow(env, ctx);
  160. }
  161. return jwidgets;
  162. }
  163. JNIEXPORT jobject JNICALL
  164. FUN(PDFPage_createSignature)(JNIEnv *env, jobject self)
  165. {
  166. fz_context *ctx = get_context(env);
  167. pdf_page *page = from_PDFPage(env, self);
  168. pdf_annot *widget = NULL;
  169. char name[80];
  170. if (!ctx || !page)
  171. return NULL;
  172. fz_try(ctx)
  173. {
  174. pdf_create_field_name(ctx, page->doc, "Signature", name, sizeof name);
  175. widget = pdf_create_signature_widget(ctx, page, name);
  176. }
  177. fz_catch(ctx)
  178. {
  179. jni_rethrow(env, ctx);
  180. }
  181. return to_PDFWidget_safe_own(ctx, env, widget);
  182. }
  183. JNIEXPORT jobject JNICALL
  184. FUN(PDFPage_getTransform)(JNIEnv *env, jobject self)
  185. {
  186. fz_context *ctx = get_context(env);
  187. pdf_page *page = from_PDFPage(env, self);
  188. fz_matrix ctm;
  189. if (!ctx || !page)
  190. return NULL;
  191. fz_try(ctx)
  192. pdf_page_transform(ctx, page, NULL, &ctm);
  193. fz_catch(ctx)
  194. jni_rethrow(env, ctx);
  195. return to_Matrix_safe(ctx, env, ctm);
  196. }
  197. JNIEXPORT jobject JNICALL
  198. FUN(PDFPage_getObject)(JNIEnv *env, jobject self)
  199. {
  200. fz_context *ctx = get_context(env);
  201. pdf_page *page = from_PDFPage(env, self);
  202. if (!ctx || !page)
  203. return NULL;
  204. return to_PDFObject_safe_own(ctx, env, pdf_keep_obj(ctx, page->obj));
  205. }
  206. JNIEXPORT void JNICALL
  207. FUN(PDFPage_setPageBox)(JNIEnv *env, jobject self, jint box, jobject jrect)
  208. {
  209. fz_context *ctx = get_context(env);
  210. pdf_page *page = from_PDFPage(env, self);
  211. fz_rect rect = from_Rect(env, jrect);
  212. if (!ctx || !page)
  213. return;
  214. fz_try(ctx)
  215. pdf_set_page_box(ctx, page, box, rect);
  216. fz_catch(ctx)
  217. jni_rethrow_void(env, ctx);
  218. }
  219. JNIEXPORT jint JNICALL
  220. FUN(PDFPage_countAssociatedFiles)(JNIEnv *env, jobject self)
  221. {
  222. fz_context *ctx = get_context(env);
  223. pdf_page *page = from_PDFPage(env, self);
  224. int n;
  225. fz_try(ctx)
  226. n = pdf_count_page_associated_files(ctx, page);
  227. fz_catch(ctx)
  228. jni_rethrow(env, ctx);
  229. return n;
  230. }
  231. JNIEXPORT jobject JNICALL
  232. FUN(PDFPage_associatedFile)(JNIEnv *env, jobject self, jint idx)
  233. {
  234. fz_context *ctx = get_context(env);
  235. pdf_page *page = from_PDFPage(env, self);
  236. pdf_obj *af;
  237. fz_try(ctx)
  238. af = pdf_page_associated_file(ctx, page, idx);
  239. fz_catch(ctx)
  240. jni_rethrow(env, ctx);
  241. return to_PDFObject_safe_own(ctx, env, af);
  242. }
  243. JNIEXPORT void JNICALL
  244. FUN(PDFPage_process)(JNIEnv *env, jobject self, jobject jproc)
  245. {
  246. fz_context *ctx = get_context(env);
  247. pdf_page *page = from_PDFPage(env, self);
  248. pdf_processor *proc = make_pdf_processor(env, ctx, jproc);
  249. pdf_obj *resources;
  250. pdf_obj *contents;
  251. if (!ctx || !page) return;
  252. if (!proc) jni_throw_arg_void(env, "processor must not be null");
  253. fz_try(ctx)
  254. {
  255. resources = pdf_page_resources(ctx, page);
  256. contents = pdf_page_contents(ctx, page);
  257. pdf_process_contents(ctx, proc, page->doc, resources, contents, NULL, NULL);
  258. pdf_close_processor(ctx, proc);
  259. }
  260. fz_always(ctx)
  261. {
  262. pdf_drop_processor(ctx, proc);
  263. }
  264. fz_catch(ctx)
  265. jni_rethrow_void(env, ctx);
  266. }
  267. JNIEXPORT jobject JNICALL
  268. FUN(PDFPage_toPixmap)(JNIEnv *env, jobject self, jobject jctm, jobject jcs, jboolean alpha, jboolean showExtra, jstring jusage, jint box)
  269. {
  270. fz_context *ctx = get_context(env);
  271. pdf_page *page = from_PDFPage(env, self);
  272. fz_colorspace *cs = from_ColorSpace(env, jcs);
  273. fz_matrix ctm = from_Matrix(env, jctm);
  274. fz_pixmap *pixmap = NULL;
  275. const char *usage = NULL;
  276. if (!ctx || !page) return NULL;
  277. if (jusage)
  278. {
  279. usage = (*env)->GetStringUTFChars(env, jusage, NULL);
  280. if (!usage) return NULL;
  281. }
  282. fz_try(ctx)
  283. {
  284. if (showExtra)
  285. pixmap = pdf_new_pixmap_from_page_with_usage(ctx, page, ctm, cs, alpha, usage, box);
  286. else
  287. pixmap = pdf_new_pixmap_from_page_contents_with_usage(ctx, page, ctm, cs, alpha, usage, box);
  288. }
  289. fz_always(ctx)
  290. if (usage)
  291. (*env)->ReleaseStringUTFChars(env, jusage, usage);
  292. fz_catch(ctx)
  293. jni_rethrow(env, ctx);
  294. return to_Pixmap_safe_own(ctx, env, pixmap);
  295. }