pdfdocument.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041
  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. /* PDFDocument interface */
  23. static void free_event_cb_data(fz_context *ctx, void *data)
  24. {
  25. jobject jlistener = (jobject)data;
  26. jboolean detach = JNI_FALSE;
  27. JNIEnv *env;
  28. env = jni_attach_thread(&detach);
  29. if (env == NULL)
  30. fz_warn(ctx, "cannot attach to JVM in free_event_cb_data");
  31. else
  32. {
  33. (*env)->DeleteGlobalRef(env, jlistener);
  34. jni_detach_thread(detach);
  35. }
  36. }
  37. static void event_cb(fz_context *ctx, pdf_document *pdf, pdf_doc_event *event, void *data)
  38. {
  39. jobject jlistener = (jobject)data;
  40. jboolean detach = JNI_FALSE;
  41. JNIEnv *env;
  42. env = jni_attach_thread(&detach);
  43. if (env == NULL)
  44. fz_throw(ctx, FZ_ERROR_GENERIC, "cannot attach to JVM in event_cb");
  45. switch (event->type)
  46. {
  47. case PDF_DOCUMENT_EVENT_ALERT:
  48. {
  49. pdf_alert_event *alert;
  50. jstring jtitle = NULL;
  51. jstring jmessage = NULL;
  52. jstring jcheckboxmsg = NULL;
  53. jobject jalertresult;
  54. jobject jpdf;
  55. alert = pdf_access_alert_event(ctx, event);
  56. jpdf = to_PDFDocument_safe(ctx, env, pdf);
  57. if (!jpdf || (*env)->ExceptionCheck(env))
  58. fz_throw_java_and_detach_thread(ctx, env, detach);
  59. jtitle = (*env)->NewStringUTF(env, alert->title);
  60. if (!jtitle || (*env)->ExceptionCheck(env))
  61. fz_throw_java_and_detach_thread(ctx, env, detach);
  62. jmessage = (*env)->NewStringUTF(env, alert->message);
  63. if (!jmessage || (*env)->ExceptionCheck(env))
  64. fz_throw_java_and_detach_thread(ctx, env, detach);
  65. if (alert->has_check_box) {
  66. jcheckboxmsg = (*env)->NewStringUTF(env, alert->check_box_message);
  67. if (!jcheckboxmsg || (*env)->ExceptionCheck(env))
  68. fz_throw_java_and_detach_thread(ctx, env, detach);
  69. }
  70. jalertresult = (*env)->CallObjectMethod(env, jlistener, mid_PDFDocument_JsEventListener_onAlert, jpdf, jtitle, jmessage, alert->icon_type, alert->button_group_type, alert->has_check_box, jcheckboxmsg, alert->initially_checked);
  71. if ((*env)->ExceptionCheck(env))
  72. fz_throw_java_and_detach_thread(ctx, env, detach);
  73. if (jalertresult)
  74. {
  75. alert->button_pressed = (*env)->GetIntField(env, jalertresult, fid_AlertResult_buttonPressed);
  76. alert->finally_checked = (*env)->GetBooleanField(env, jalertresult, fid_AlertResult_checkboxChecked);
  77. }
  78. }
  79. break;
  80. default:
  81. fz_throw_and_detach_thread(ctx, detach, FZ_ERROR_GENERIC, "event not yet implemented");
  82. break;
  83. }
  84. jni_detach_thread(detach);
  85. }
  86. JNIEXPORT jlong JNICALL
  87. FUN(PDFDocument_newNative)(JNIEnv *env, jclass cls)
  88. {
  89. fz_context *ctx = get_context(env);
  90. pdf_document *doc = NULL;
  91. if (!ctx) return 0;
  92. fz_try(ctx)
  93. doc = pdf_create_document(ctx);
  94. fz_catch(ctx)
  95. jni_rethrow(env, ctx);
  96. return jlong_cast(doc);
  97. }
  98. JNIEXPORT void JNICALL
  99. FUN(PDFDocument_finalize)(JNIEnv *env, jobject self)
  100. {
  101. fz_context *ctx = get_context(env);
  102. pdf_document *pdf = from_PDFDocument_safe(env, self);
  103. if (!ctx || !pdf) return;
  104. FUN(Document_finalize)(env, self); /* Call super.finalize() */
  105. }
  106. JNIEXPORT jint JNICALL
  107. FUN(PDFDocument_countObjects)(JNIEnv *env, jobject self)
  108. {
  109. fz_context *ctx = get_context(env);
  110. pdf_document *pdf = from_PDFDocument(env, self);
  111. int count = 0;
  112. if (!ctx || !pdf) return 0;
  113. fz_try(ctx)
  114. count = pdf_xref_len(ctx, pdf);
  115. fz_catch(ctx)
  116. jni_rethrow(env, ctx);
  117. return count;
  118. }
  119. JNIEXPORT jobject JNICALL
  120. FUN(PDFDocument_newNull)(JNIEnv *env, jobject self)
  121. {
  122. fz_context *ctx = get_context(env);
  123. pdf_obj *obj = NULL;
  124. jobject jobj;
  125. if (!ctx) return NULL;
  126. obj = PDF_NULL;
  127. jobj = (*env)->NewObject(env, cls_PDFObject, mid_PDFObject_init, jlong_cast(obj));
  128. if (!jobj)
  129. pdf_drop_obj(ctx, obj);
  130. return jobj;
  131. }
  132. JNIEXPORT jobject JNICALL
  133. FUN(PDFDocument_newBoolean)(JNIEnv *env, jobject self, jboolean b)
  134. {
  135. fz_context *ctx = get_context(env);
  136. pdf_obj *obj = NULL;
  137. jobject jobj;
  138. if (!ctx) return NULL;
  139. obj = b ? PDF_TRUE : PDF_FALSE;
  140. jobj = (*env)->NewObject(env, cls_PDFObject, mid_PDFObject_init, jlong_cast(obj));
  141. if (!jobj)
  142. pdf_drop_obj(ctx, obj);
  143. return jobj;
  144. }
  145. JNIEXPORT jobject JNICALL
  146. FUN(PDFDocument_newInteger)(JNIEnv *env, jobject self, jint i)
  147. {
  148. fz_context *ctx = get_context(env);
  149. pdf_obj *obj = NULL;
  150. jobject jobj;
  151. if (!ctx) return NULL;
  152. fz_try(ctx)
  153. obj = pdf_new_int(ctx, i);
  154. fz_catch(ctx)
  155. jni_rethrow(env, ctx);
  156. jobj = (*env)->NewObject(env, cls_PDFObject, mid_PDFObject_init, jlong_cast(obj));
  157. if (!jobj)
  158. pdf_drop_obj(ctx, obj);
  159. return jobj;
  160. }
  161. JNIEXPORT jobject JNICALL
  162. FUN(PDFDocument_newReal)(JNIEnv *env, jobject self, jfloat f)
  163. {
  164. fz_context *ctx = get_context(env);
  165. pdf_obj *obj = NULL;
  166. jobject jobj;
  167. if (!ctx) return NULL;
  168. fz_try(ctx)
  169. obj = pdf_new_real(ctx, f);
  170. fz_catch(ctx)
  171. jni_rethrow(env, ctx);
  172. jobj = (*env)->NewObject(env, cls_PDFObject, mid_PDFObject_init, jlong_cast(obj));
  173. if (!jobj)
  174. pdf_drop_obj(ctx, obj);
  175. return jobj;
  176. }
  177. JNIEXPORT jobject JNICALL
  178. FUN(PDFDocument_newString)(JNIEnv *env, jobject self, jstring jstring)
  179. {
  180. fz_context *ctx = get_context(env);
  181. pdf_obj *obj = NULL;
  182. const char *s = NULL;
  183. jobject jobj;
  184. if (!ctx) return NULL;
  185. if (!jstring) jni_throw_arg(env, "string must not be null");
  186. s = (*env)->GetStringUTFChars(env, jstring, NULL);
  187. if (!s) return NULL;
  188. fz_try(ctx)
  189. obj = pdf_new_text_string(ctx, s);
  190. fz_always(ctx)
  191. (*env)->ReleaseStringUTFChars(env, jstring, s);
  192. fz_catch(ctx)
  193. jni_rethrow(env, ctx);
  194. jobj = (*env)->NewObject(env, cls_PDFObject, mid_PDFObject_init, jlong_cast(obj));
  195. if (!jobj)
  196. pdf_drop_obj(ctx, obj);
  197. return jobj;
  198. }
  199. JNIEXPORT jobject JNICALL
  200. FUN(PDFDocument_newByteString)(JNIEnv *env, jobject self, jobject jbs)
  201. {
  202. fz_context *ctx = get_context(env);
  203. pdf_obj *obj = NULL;
  204. jbyte *bs;
  205. size_t bslen;
  206. jobject jobj;
  207. if (!ctx) return NULL;
  208. if (!jbs) jni_throw_arg(env, "bs must not be null");
  209. bslen = (*env)->GetArrayLength(env, jbs);
  210. fz_try(ctx)
  211. bs = Memento_label(fz_malloc(ctx, bslen), "PDFDocument_newByteString");
  212. fz_catch(ctx)
  213. jni_rethrow(env, ctx);
  214. (*env)->GetByteArrayRegion(env, jbs, 0, bslen, bs);
  215. if ((*env)->ExceptionCheck(env))
  216. {
  217. fz_free(ctx, bs);
  218. return NULL;
  219. }
  220. fz_try(ctx)
  221. obj = pdf_new_string(ctx, (char *) bs, bslen);
  222. fz_always(ctx)
  223. fz_free(ctx, bs);
  224. fz_catch(ctx)
  225. jni_rethrow(env, ctx);
  226. jobj = (*env)->NewObject(env, cls_PDFObject, mid_PDFObject_init, jlong_cast(obj));
  227. if (!jobj)
  228. pdf_drop_obj(ctx, obj);
  229. return jobj;
  230. }
  231. JNIEXPORT jobject JNICALL
  232. FUN(PDFDocument_newName)(JNIEnv *env, jobject self, jstring jname)
  233. {
  234. fz_context *ctx = get_context(env);
  235. pdf_obj *obj = NULL;
  236. const char *name = NULL;
  237. jobject jobj;
  238. if (!ctx) return NULL;
  239. if (!jname) jni_throw_arg(env, "name must not be null");
  240. name = (*env)->GetStringUTFChars(env, jname, NULL);
  241. if (!name) return NULL;
  242. fz_try(ctx)
  243. obj = pdf_new_name(ctx, name);
  244. fz_always(ctx)
  245. (*env)->ReleaseStringUTFChars(env, jname, name);
  246. fz_catch(ctx)
  247. jni_rethrow(env, ctx);
  248. jobj = (*env)->NewObject(env, cls_PDFObject, mid_PDFObject_init, jlong_cast(obj));
  249. if (!jobj)
  250. pdf_drop_obj(ctx, obj);
  251. return jobj;
  252. }
  253. JNIEXPORT jobject JNICALL
  254. FUN(PDFDocument_newIndirect)(JNIEnv *env, jobject self, jint num, jint gen)
  255. {
  256. fz_context *ctx = get_context(env);
  257. pdf_document *pdf = from_PDFDocument(env, self);
  258. pdf_obj *obj = NULL;
  259. jobject jobj;
  260. if (!ctx || !pdf) return NULL;
  261. fz_try(ctx)
  262. obj = pdf_new_indirect(ctx, pdf, num, gen);
  263. fz_catch(ctx)
  264. jni_rethrow(env, ctx);
  265. jobj = (*env)->NewObject(env, cls_PDFObject, mid_PDFObject_init, jlong_cast(obj));
  266. if (!jobj)
  267. pdf_drop_obj(ctx, obj);
  268. return jobj;
  269. }
  270. JNIEXPORT jobject JNICALL
  271. FUN(PDFDocument_newArray)(JNIEnv *env, jobject self)
  272. {
  273. fz_context *ctx = get_context(env);
  274. pdf_document *pdf = from_PDFDocument(env, self);
  275. pdf_obj *obj = NULL;
  276. jobject jobj;
  277. if (!ctx || !pdf) return NULL;
  278. fz_try(ctx)
  279. obj = pdf_new_array(ctx, pdf, 0);
  280. fz_catch(ctx)
  281. jni_rethrow(env, ctx);
  282. jobj = (*env)->NewObject(env, cls_PDFObject, mid_PDFObject_init, jlong_cast(obj));
  283. if (!jobj)
  284. pdf_drop_obj(ctx, obj);
  285. return jobj;
  286. }
  287. JNIEXPORT jobject JNICALL
  288. FUN(PDFDocument_newDictionary)(JNIEnv *env, jobject self)
  289. {
  290. fz_context *ctx = get_context(env);
  291. pdf_document *pdf = from_PDFDocument(env, self);
  292. pdf_obj *obj = NULL;
  293. jobject jobj;
  294. if (!ctx || !pdf) return NULL;
  295. fz_try(ctx)
  296. obj = pdf_new_dict(ctx, pdf, 0);
  297. fz_catch(ctx)
  298. jni_rethrow(env, ctx);
  299. jobj = (*env)->NewObject(env, cls_PDFObject, mid_PDFObject_init, jlong_cast(obj));
  300. if (!jobj)
  301. pdf_drop_obj(ctx, obj);
  302. return jobj;
  303. }
  304. JNIEXPORT jobject JNICALL
  305. FUN(PDFDocument_findPage)(JNIEnv *env, jobject self, jint jat)
  306. {
  307. fz_context *ctx = get_context(env);
  308. pdf_document *pdf = from_PDFDocument(env, self);
  309. pdf_obj *obj = NULL;
  310. if (!ctx || !pdf) return NULL;
  311. fz_try(ctx)
  312. obj = pdf_lookup_page_obj(ctx, pdf, jat);
  313. fz_catch(ctx)
  314. jni_rethrow(env, ctx);
  315. return to_PDFObject_safe(ctx, env, obj);
  316. }
  317. JNIEXPORT jobject JNICALL
  318. FUN(PDFDocument_getTrailer)(JNIEnv *env, jobject self)
  319. {
  320. fz_context *ctx = get_context(env);
  321. pdf_document *pdf = from_PDFDocument(env, self);
  322. pdf_obj *obj = NULL;
  323. if (!ctx || !pdf) return NULL;
  324. fz_try(ctx)
  325. obj = pdf_trailer(ctx, pdf);
  326. fz_catch(ctx)
  327. jni_rethrow(env, ctx);
  328. return to_PDFObject_safe(ctx, env, obj);
  329. }
  330. JNIEXPORT jobject JNICALL
  331. FUN(PDFDocument_addObject)(JNIEnv *env, jobject self, jobject jobj)
  332. {
  333. fz_context *ctx = get_context(env);
  334. pdf_document *pdf = from_PDFDocument(env, self);
  335. pdf_obj *obj = from_PDFObject(env, jobj);
  336. if (!ctx || !pdf) return NULL;
  337. if (!jobj) jni_throw_arg(env, "object must not be null");
  338. fz_try(ctx)
  339. obj = pdf_add_object_drop(ctx, pdf, obj);
  340. fz_catch(ctx)
  341. jni_rethrow(env, ctx);
  342. return jobj;
  343. }
  344. JNIEXPORT jobject JNICALL
  345. FUN(PDFDocument_createObject)(JNIEnv *env, jobject self)
  346. {
  347. fz_context *ctx = get_context(env);
  348. pdf_document *pdf = from_PDFDocument(env, self);
  349. pdf_obj *ind = NULL;
  350. if (!ctx || !pdf) return NULL;
  351. fz_try(ctx)
  352. ind = pdf_new_indirect(ctx, pdf, pdf_create_object(ctx, pdf), 0);
  353. fz_catch(ctx)
  354. jni_rethrow(env, ctx);
  355. return to_PDFObject_safe_own(ctx, env, ind);
  356. }
  357. JNIEXPORT void JNICALL
  358. FUN(PDFDocument_deleteObject)(JNIEnv *env, jobject self, jint num)
  359. {
  360. fz_context *ctx = get_context(env);
  361. pdf_document *pdf = from_PDFDocument(env, self);
  362. if (!ctx || !pdf) return;
  363. fz_try(ctx)
  364. pdf_delete_object(ctx, pdf, num);
  365. fz_catch(ctx)
  366. jni_rethrow_void(env, ctx);
  367. }
  368. JNIEXPORT jobject JNICALL
  369. FUN(PDFDocument_newPDFGraftMap)(JNIEnv *env, jobject self)
  370. {
  371. fz_context *ctx = get_context(env);
  372. pdf_document *pdf = from_PDFDocument(env, self);
  373. pdf_graft_map *map = NULL;
  374. if (!ctx || !pdf) return NULL;
  375. fz_try(ctx)
  376. map = pdf_new_graft_map(ctx, pdf);
  377. fz_catch(ctx)
  378. jni_rethrow(env, ctx);
  379. return to_PDFGraftMap_safe_own(ctx, env, self, map);
  380. }
  381. JNIEXPORT jobject JNICALL
  382. FUN(PDFDocument_graftObject)(JNIEnv *env, jobject self, jobject jobj)
  383. {
  384. fz_context *ctx = get_context(env);
  385. pdf_obj *obj = from_PDFObject(env, jobj);
  386. pdf_document *dst = from_PDFDocument(env, self);
  387. if (!ctx || !dst) return NULL;
  388. if (!dst) jni_throw_arg(env, "dst must not be null");
  389. if (!obj) jni_throw_arg(env, "object must not be null");
  390. fz_try(ctx)
  391. obj = pdf_graft_object(ctx, dst, obj);
  392. fz_catch(ctx)
  393. jni_rethrow(env, ctx);
  394. return to_PDFObject_safe_own(ctx, env, obj);
  395. }
  396. JNIEXPORT jobject JNICALL
  397. FUN(PDFDocument_addStreamBuffer)(JNIEnv *env, jobject self, jobject jbuf, jobject jobj, jboolean compressed)
  398. {
  399. fz_context *ctx = get_context(env);
  400. pdf_document *pdf = from_PDFDocument(env, self);
  401. pdf_obj *obj = from_PDFObject(env, jobj);
  402. fz_buffer *buf = from_Buffer(env, jbuf);
  403. pdf_obj *ind = NULL;
  404. if (!ctx || !pdf) return NULL;
  405. if (!jbuf) jni_throw_arg(env, "buffer must not be null");
  406. fz_try(ctx)
  407. ind = pdf_add_stream(ctx, pdf, buf, obj, compressed);
  408. fz_catch(ctx)
  409. jni_rethrow(env, ctx);
  410. return to_PDFObject_safe_own(ctx, env, ind);
  411. }
  412. JNIEXPORT jobject JNICALL
  413. FUN(PDFDocument_addStreamString)(JNIEnv *env, jobject self, jstring jbuf, jobject jobj, jboolean compressed)
  414. {
  415. fz_context *ctx = get_context(env);
  416. pdf_document *pdf = from_PDFDocument(env, self);
  417. pdf_obj *obj = from_PDFObject(env, jobj);
  418. fz_buffer *buf = NULL;
  419. const char *sbuf = NULL;
  420. pdf_obj *ind = NULL;
  421. if (!ctx || !pdf) return NULL;
  422. if (!jbuf) jni_throw_arg(env, "buffer must not be null");
  423. sbuf = (*env)->GetStringUTFChars(env, jbuf, NULL);
  424. if (!sbuf) return NULL;
  425. fz_var(buf);
  426. fz_try(ctx)
  427. {
  428. buf = fz_new_buffer_from_copied_data(ctx, (const unsigned char *)sbuf, strlen(sbuf));
  429. ind = pdf_add_stream(ctx, pdf, buf, obj, compressed);
  430. }
  431. fz_always(ctx)
  432. {
  433. fz_drop_buffer(ctx, buf);
  434. (*env)->ReleaseStringUTFChars(env, jbuf, sbuf);
  435. }
  436. fz_catch(ctx)
  437. jni_rethrow(env, ctx);
  438. return to_PDFObject_safe_own(ctx, env, ind);
  439. }
  440. JNIEXPORT jobject JNICALL
  441. FUN(PDFDocument_addPageBuffer)(JNIEnv *env, jobject self, jobject jmediabox, jint rotate, jobject jresources, jobject jcontents)
  442. {
  443. fz_context *ctx = get_context(env);
  444. pdf_document *pdf = from_PDFDocument(env, self);
  445. fz_rect mediabox = from_Rect(env, jmediabox);
  446. pdf_obj *resources = from_PDFObject(env, jresources);
  447. fz_buffer *contents = from_Buffer(env, jcontents);
  448. pdf_obj *ind = NULL;
  449. if (!ctx || !pdf) return NULL;
  450. fz_try(ctx)
  451. ind = pdf_add_page(ctx, pdf, mediabox, rotate, resources, contents);
  452. fz_catch(ctx)
  453. jni_rethrow(env, ctx);
  454. return to_PDFObject_safe_own(ctx, env, ind);
  455. }
  456. JNIEXPORT jobject JNICALL
  457. FUN(PDFDocument_addPageString)(JNIEnv *env, jobject self, jobject jmediabox, jint rotate, jobject jresources, jstring jcontents)
  458. {
  459. fz_context *ctx = get_context(env);
  460. pdf_document *pdf = from_PDFDocument(env, self);
  461. fz_rect mediabox = from_Rect(env, jmediabox);
  462. pdf_obj *resources = from_PDFObject(env, jresources);
  463. const char *scontents = NULL;
  464. fz_buffer *contents = NULL;
  465. pdf_obj *ind = NULL;
  466. if (!ctx || !pdf) return NULL;
  467. scontents = (*env)->GetStringUTFChars(env, jcontents, NULL);
  468. if (!scontents) return NULL;
  469. fz_var(contents);
  470. fz_try(ctx)
  471. {
  472. contents = fz_new_buffer_from_copied_data(ctx, (const unsigned char *)scontents, strlen(scontents));
  473. ind = pdf_add_page(ctx, pdf, mediabox, rotate, resources, contents);
  474. }
  475. fz_always(ctx)
  476. {
  477. fz_drop_buffer(ctx, contents);
  478. (*env)->ReleaseStringUTFChars(env, jcontents, scontents);
  479. }
  480. fz_catch(ctx)
  481. jni_rethrow(env, ctx);
  482. return to_PDFObject_safe_own(ctx, env, ind);
  483. }
  484. JNIEXPORT void JNICALL
  485. FUN(PDFDocument_insertPage)(JNIEnv *env, jobject self, jint jat, jobject jpage)
  486. {
  487. fz_context *ctx = get_context(env);
  488. pdf_document *pdf = from_PDFDocument(env, self);
  489. int at = jat;
  490. pdf_obj *page = from_PDFObject(env, jpage);
  491. if (!ctx || !pdf) return;
  492. if (!page) jni_throw_arg_void(env, "page must not be null");
  493. fz_try(ctx)
  494. pdf_insert_page(ctx, pdf, at, page);
  495. fz_catch(ctx)
  496. jni_rethrow_void(env, ctx);
  497. }
  498. JNIEXPORT void JNICALL
  499. FUN(PDFDocument_deletePage)(JNIEnv *env, jobject self, jint jat)
  500. {
  501. fz_context *ctx = get_context(env);
  502. pdf_document *pdf = from_PDFDocument(env, self);
  503. int at = jat;
  504. if (!ctx || !pdf) return;
  505. fz_try(ctx)
  506. pdf_delete_page(ctx, pdf, at);
  507. fz_catch(ctx)
  508. jni_rethrow_void(env, ctx);
  509. }
  510. JNIEXPORT jobject JNICALL
  511. FUN(PDFDocument_addImage)(JNIEnv *env, jobject self, jobject jimage)
  512. {
  513. fz_context *ctx = get_context(env);
  514. pdf_document *pdf = from_PDFDocument(env, self);
  515. fz_image *image = from_Image(env, jimage);
  516. pdf_obj *ind = NULL;
  517. if (!ctx || !pdf) return NULL;
  518. if (!image) jni_throw_arg(env, "image must not be null");
  519. fz_try(ctx)
  520. ind = pdf_add_image(ctx, pdf, image);
  521. fz_catch(ctx)
  522. jni_rethrow(env, ctx);
  523. return to_PDFObject_safe_own(ctx, env, ind);
  524. }
  525. JNIEXPORT jobject JNICALL
  526. FUN(PDFDocument_addFont)(JNIEnv *env, jobject self, jobject jfont)
  527. {
  528. fz_context *ctx = get_context(env);
  529. pdf_document *pdf = from_PDFDocument(env, self);
  530. fz_font *font = from_Font(env, jfont);
  531. pdf_obj *ind = NULL;
  532. if (!ctx || !pdf) return NULL;
  533. if (!font) jni_throw_arg(env, "font must not be null");
  534. fz_try(ctx)
  535. ind = pdf_add_cid_font(ctx, pdf, font);
  536. fz_catch(ctx)
  537. jni_rethrow(env, ctx);
  538. return to_PDFObject_safe_own(ctx, env, ind);
  539. }
  540. JNIEXPORT jobject JNICALL
  541. FUN(PDFDocument_addCJKFont)(JNIEnv *env, jobject self, jobject jfont, jint ordering, jint wmode, jboolean serif)
  542. {
  543. fz_context *ctx = get_context(env);
  544. pdf_document *pdf = from_PDFDocument(env, self);
  545. fz_font *font = from_Font(env, jfont);
  546. pdf_obj *ind = NULL;
  547. if (!ctx || !pdf) return NULL;
  548. if (!font) jni_throw_arg(env, "font must not be null");
  549. fz_try(ctx)
  550. ind = pdf_add_cjk_font(ctx, pdf, font, ordering, wmode, serif);
  551. fz_catch(ctx)
  552. jni_rethrow(env, ctx);
  553. return to_PDFObject_safe_own(ctx, env, ind);
  554. }
  555. JNIEXPORT jobject JNICALL
  556. FUN(PDFDocument_addSimpleFont)(JNIEnv *env, jobject self, jobject jfont, jint encoding)
  557. {
  558. fz_context *ctx = get_context(env);
  559. pdf_document *pdf = from_PDFDocument(env, self);
  560. fz_font *font = from_Font(env, jfont);
  561. pdf_obj *ind = NULL;
  562. if (!ctx || !pdf) return NULL;
  563. if (!font) jni_throw_arg(env, "font must not be null");
  564. fz_try(ctx)
  565. ind = pdf_add_simple_font(ctx, pdf, font, encoding);
  566. fz_catch(ctx)
  567. jni_rethrow(env, ctx);
  568. return to_PDFObject_safe_own(ctx, env, ind);
  569. }
  570. JNIEXPORT jboolean JNICALL
  571. FUN(PDFDocument_hasUnsavedChanges)(JNIEnv *env, jobject self)
  572. {
  573. fz_context *ctx = get_context(env);
  574. pdf_document *pdf = from_PDFDocument(env, self);
  575. if (!ctx || !pdf) return JNI_FALSE;
  576. return pdf_has_unsaved_changes(ctx, pdf) ? JNI_TRUE : JNI_FALSE;
  577. }
  578. JNIEXPORT jboolean JNICALL
  579. FUN(PDFDocument_wasRepaired)(JNIEnv *env, jobject self)
  580. {
  581. fz_context *ctx = get_context(env);
  582. pdf_document *pdf = from_PDFDocument(env, self);
  583. if (!ctx || !pdf) return JNI_FALSE;
  584. return pdf_was_repaired(ctx, pdf) ? JNI_TRUE : JNI_FALSE;
  585. }
  586. JNIEXPORT jboolean JNICALL
  587. FUN(PDFDocument_canBeSavedIncrementally)(JNIEnv *env, jobject self)
  588. {
  589. fz_context *ctx = get_context(env);
  590. pdf_document *pdf = from_PDFDocument(env, self);
  591. if (!ctx || !pdf) return JNI_FALSE;
  592. return pdf_can_be_saved_incrementally(ctx, pdf) ? JNI_TRUE : JNI_FALSE;
  593. }
  594. static fz_stream *
  595. SeekableOutputStream_as_stream(fz_context *ctx, void *opaque)
  596. {
  597. SeekableStreamState *in_state = opaque;
  598. SeekableStreamState *state;
  599. fz_stream *stm;
  600. state = Memento_label(fz_malloc(ctx, sizeof(SeekableStreamState)), "SeekableStreamState_state");
  601. state->stream = in_state->stream;
  602. state->array = in_state->array;
  603. stm = fz_new_stream(ctx, state, SeekableInputStream_next, fz_free);
  604. stm->seek = SeekableInputStream_seek;
  605. return stm;
  606. }
  607. JNIEXPORT void JNICALL
  608. FUN(PDFDocument_nativeSaveWithStream)(JNIEnv *env, jobject self, jobject jstream, jstring joptions)
  609. {
  610. fz_context *ctx = get_context(env);
  611. pdf_document *pdf = from_PDFDocument(env, self);
  612. SeekableStreamState *state = NULL;
  613. jobject stream = NULL;
  614. jbyteArray array = NULL;
  615. fz_output *out = NULL;
  616. const char *options = NULL;
  617. pdf_write_options pwo;
  618. fz_var(state);
  619. fz_var(out);
  620. fz_var(stream);
  621. fz_var(array);
  622. if (!ctx || !pdf) return;
  623. if (!jstream) jni_throw_arg_void(env, "stream must not be null");
  624. if (joptions)
  625. {
  626. options = (*env)->GetStringUTFChars(env, joptions, NULL);
  627. if (!options) return;
  628. }
  629. stream = (*env)->NewGlobalRef(env, jstream);
  630. if (!stream)
  631. {
  632. if (options) (*env)->ReleaseStringUTFChars(env, joptions, options);
  633. return;
  634. }
  635. array = (*env)->NewByteArray(env, sizeof state->buffer);
  636. if ((*env)->ExceptionCheck(env))
  637. {
  638. if (options) (*env)->ReleaseStringUTFChars(env, joptions, options);
  639. (*env)->DeleteGlobalRef(env, stream);
  640. return;
  641. }
  642. if (!array)
  643. {
  644. if (options) (*env)->ReleaseStringUTFChars(env, joptions, options);
  645. (*env)->DeleteGlobalRef(env, stream);
  646. jni_throw_run_void(env, "cannot create byte array");
  647. }
  648. array = (*env)->NewGlobalRef(env, array);
  649. if (!array)
  650. {
  651. if (options) (*env)->ReleaseStringUTFChars(env, joptions, options);
  652. (*env)->DeleteGlobalRef(env, stream);
  653. jni_throw_run_void(env, "cannot create global reference");
  654. }
  655. fz_try(ctx)
  656. {
  657. if (jstream)
  658. {
  659. /* No exceptions can occur from here to stream owning state, so we must not free state. */
  660. state = Memento_label(fz_malloc(ctx, sizeof(SeekableStreamState)), "SeekableStreamState_state");
  661. state->stream = stream;
  662. state->array = array;
  663. /* Ownership transferred to state. */
  664. stream = NULL;
  665. array = NULL;
  666. /* Stream takes ownership of state. */
  667. out = fz_new_output(ctx, sizeof state->buffer, state, SeekableOutputStream_write, NULL, SeekableOutputStream_drop);
  668. out->seek = SeekableOutputStream_seek;
  669. out->tell = SeekableOutputStream_tell;
  670. out->truncate = SeekableOutputStream_truncate;
  671. out->as_stream = SeekableOutputStream_as_stream;
  672. /* these are now owned by 'out' */
  673. state = NULL;
  674. }
  675. pdf_parse_write_options(ctx, &pwo, options);
  676. pdf_write_document(ctx, pdf, out, &pwo);
  677. fz_close_output(ctx, out);
  678. }
  679. fz_always(ctx)
  680. {
  681. fz_drop_output(ctx, out);
  682. if (options)
  683. (*env)->ReleaseStringUTFChars(env, joptions, options);
  684. }
  685. fz_catch(ctx)
  686. {
  687. (*env)->DeleteGlobalRef(env, array);
  688. (*env)->DeleteGlobalRef(env, stream);
  689. jni_rethrow_void(env, ctx);
  690. }
  691. }
  692. JNIEXPORT void JNICALL
  693. FUN(PDFDocument_save)(JNIEnv *env, jobject self, jstring jfilename, jstring joptions)
  694. {
  695. fz_context *ctx = get_context(env);
  696. pdf_document *pdf = from_PDFDocument(env, self);
  697. const char *filename = NULL;
  698. const char *options = NULL;
  699. pdf_write_options pwo;
  700. if (!ctx || !pdf) return;
  701. if (!jfilename) jni_throw_arg_void(env, "filename must not be null");
  702. filename = (*env)->GetStringUTFChars(env, jfilename, NULL);
  703. if (!filename) return;
  704. if (joptions)
  705. {
  706. options = (*env)->GetStringUTFChars(env, joptions, NULL);
  707. if (!options)
  708. {
  709. (*env)->ReleaseStringUTFChars(env, jfilename, filename);
  710. return;
  711. }
  712. }
  713. fz_try(ctx)
  714. {
  715. pdf_parse_write_options(ctx, &pwo, options);
  716. pdf_save_document(ctx, pdf, filename, &pwo);
  717. }
  718. fz_always(ctx)
  719. {
  720. if (options)
  721. (*env)->ReleaseStringUTFChars(env, joptions, options);
  722. (*env)->ReleaseStringUTFChars(env, jfilename, filename);
  723. }
  724. fz_catch(ctx)
  725. jni_rethrow_void(env, ctx);
  726. }
  727. JNIEXPORT void JNICALL
  728. FUN(PDFDocument_enableJs)(JNIEnv *env, jobject self)
  729. {
  730. fz_context *ctx = get_context(env);
  731. pdf_document *pdf = from_PDFDocument_safe(env, self);
  732. if (!ctx || !pdf) return;
  733. fz_try(ctx)
  734. pdf_enable_js(ctx, pdf);
  735. fz_catch(ctx)
  736. jni_rethrow_void(env, ctx);
  737. }
  738. JNIEXPORT void JNICALL
  739. FUN(PDFDocument_disableJs)(JNIEnv *env, jobject self)
  740. {
  741. fz_context *ctx = get_context(env);
  742. pdf_document *pdf = from_PDFDocument_safe(env, self);
  743. if (!ctx || !pdf) return;
  744. fz_try(ctx)
  745. pdf_disable_js(ctx, pdf);
  746. fz_catch(ctx)
  747. jni_rethrow_void(env, ctx);
  748. }
  749. JNIEXPORT jboolean JNICALL
  750. FUN(PDFDocument_isJsSupported)(JNIEnv *env, jobject self)
  751. {
  752. fz_context *ctx = get_context(env);
  753. pdf_document *pdf = from_PDFDocument_safe(env, self);
  754. jboolean supported = JNI_FALSE;
  755. if (!ctx || !pdf) return JNI_FALSE;
  756. fz_try(ctx)
  757. supported = pdf_js_supported(ctx, pdf);
  758. fz_catch(ctx)
  759. jni_rethrow(env, ctx);
  760. return supported;
  761. }
  762. JNIEXPORT void JNICALL
  763. FUN(PDFDocument_setJsEventListener)(JNIEnv *env, jobject self, jobject jlistener)
  764. {
  765. fz_context *ctx = get_context(env);
  766. pdf_document *pdf = from_PDFDocument_safe(env, self);
  767. if (!ctx || !pdf) return;
  768. if (!jlistener) jni_throw_arg_void(env, "listener must not be null");
  769. jlistener = (*env)->NewGlobalRef(env, jlistener);
  770. if (!jlistener) jni_throw_arg_void(env, "unable to get reference to listener");
  771. fz_try(ctx)
  772. pdf_set_doc_event_callback(ctx, pdf, event_cb, free_event_cb_data, jlistener);
  773. fz_catch(ctx)
  774. jni_rethrow_void(env, ctx);
  775. }
  776. JNIEXPORT void JNICALL
  777. FUN(PDFDocument_calculate)(JNIEnv *env, jobject self)
  778. {
  779. fz_context *ctx = get_context(env);
  780. pdf_document *pdf = from_PDFDocument_safe(env, self);
  781. if (!ctx || !pdf) return;
  782. fz_try(ctx)
  783. if (pdf->recalculate)
  784. pdf_calculate_form(ctx, pdf);
  785. fz_catch(ctx)
  786. jni_rethrow_void(env, ctx);
  787. }
  788. JNIEXPORT jint JNICALL
  789. FUN(PDFDocument_countVersions)(JNIEnv *env, jobject self)
  790. {
  791. fz_context *ctx = get_context(env);
  792. pdf_document *pdf = from_PDFDocument_safe(env, self);
  793. int val = 0;
  794. if (!ctx || !pdf) return 0;
  795. fz_try(ctx)
  796. val = pdf_count_versions(ctx, pdf);
  797. fz_catch(ctx)
  798. jni_rethrow(env, ctx);
  799. return val;
  800. }
  801. JNIEXPORT jint JNICALL
  802. FUN(PDFDocument_countUnsavedVersions)(JNIEnv *env, jobject self)
  803. {
  804. fz_context *ctx = get_context(env);
  805. pdf_document *pdf = from_PDFDocument_safe(env, self);
  806. int val = 0;
  807. if (!ctx || !pdf) return 0;
  808. fz_try(ctx)
  809. val = pdf_count_unsaved_versions(ctx, pdf);
  810. fz_catch(ctx)
  811. jni_rethrow(env, ctx);
  812. return val;
  813. }
  814. JNIEXPORT jint JNICALL
  815. FUN(PDFDocument_validateChangeHistory)(JNIEnv *env, jobject self)
  816. {
  817. fz_context *ctx = get_context(env);
  818. pdf_document *pdf = from_PDFDocument_safe(env, self);
  819. int val = 0;
  820. if (!ctx || !pdf) return 0;
  821. fz_try(ctx)
  822. val = pdf_validate_change_history(ctx, pdf);
  823. fz_catch(ctx)
  824. jni_rethrow(env, ctx);
  825. return val;
  826. }
  827. JNIEXPORT jboolean JNICALL
  828. FUN(PDFDocument_wasPureXFA)(JNIEnv *env, jobject self)
  829. {
  830. fz_context *ctx = get_context(env);
  831. pdf_document *pdf = from_PDFDocument_safe(env, self);
  832. jboolean val = JNI_FALSE;
  833. if (!ctx || !pdf) return JNI_FALSE;
  834. fz_try(ctx)
  835. val = pdf_was_pure_xfa(ctx, pdf);
  836. fz_catch(ctx)
  837. jni_rethrow(env, ctx);
  838. return val;
  839. }
  840. JNIEXPORT jboolean JNICALL
  841. FUN(PDFDocument_wasLinearized)(JNIEnv *env, jobject self)
  842. {
  843. fz_context *ctx = get_context(env);
  844. pdf_document *pdf = from_PDFDocument_safe(env, self);
  845. jboolean val = JNI_FALSE;
  846. if (!ctx || !pdf) return JNI_FALSE;
  847. fz_try(ctx)
  848. val = pdf_doc_was_linearized(ctx, pdf);
  849. fz_catch(ctx)
  850. jni_rethrow(env, ctx);
  851. return val;
  852. }
  853. JNIEXPORT void JNICALL
  854. FUN(PDFDocument_graftPage)(JNIEnv *env, jobject self, jint pageTo, jobject jobj, jint pageFrom)
  855. {
  856. fz_context *ctx = get_context(env);
  857. pdf_document *src = from_PDFDocument(env, jobj);
  858. pdf_document *dst = from_PDFDocument(env, self);
  859. if (!ctx || !dst) return;
  860. if (!src) jni_throw_arg_void(env, "Source Document must not be null");
  861. fz_try(ctx)
  862. pdf_graft_page(ctx, dst, pageTo, src, pageFrom);
  863. fz_catch(ctx)
  864. jni_rethrow_void(env, ctx);
  865. }
  866. JNIEXPORT void JNICALL
  867. FUN(PDFDocument_enableJournal)(JNIEnv *env, jobject self)
  868. {
  869. fz_context *ctx = get_context(env);
  870. pdf_document *pdf = from_PDFDocument(env, self);
  871. if (!ctx || !pdf) return;
  872. pdf_enable_journal(ctx, pdf);
  873. }
  874. JNIEXPORT void JNICALL
  875. FUN(PDFDocument_saveJournalWithStream)(JNIEnv *env, jobject self, jobject jstream)
  876. {
  877. fz_context *ctx = get_context(env);
  878. pdf_document *pdf = from_PDFDocument(env, self);
  879. SeekableStreamState *state = NULL;
  880. jobject stream = NULL;
  881. jbyteArray array = NULL;
  882. fz_output *out = NULL;
  883. if (!ctx || !pdf)
  884. return;
  885. if (!jstream)
  886. jni_throw_arg_void(env, "stream must not be null");
  887. fz_var(state);
  888. fz_var(out);
  889. fz_var(stream);
  890. fz_var(array);
  891. stream = (*env)->NewGlobalRef(env, jstream);
  892. if (!stream)
  893. jni_throw_run_void(env, "invalid stream");
  894. array = (*env)->NewByteArray(env, sizeof state->buffer);
  895. if ((*env)->ExceptionCheck(env))
  896. {
  897. (*env)->DeleteGlobalRef(env, stream);
  898. return;
  899. }
  900. if (!array)
  901. {
  902. (*env)->DeleteGlobalRef(env, stream);
  903. jni_throw_run_void(env, "cannot create byte array");
  904. }
  905. array = (*env)->NewGlobalRef(env, array);
  906. if (!array)
  907. {
  908. (*env)->DeleteGlobalRef(env, stream);
  909. jni_throw_run_void(env, "cannot create global reference");
  910. }
  911. fz_try(ctx)
  912. {
  913. /* No exceptions can occur from here to stream owning state, so we must not free state. */
  914. state = Memento_label(fz_malloc(ctx, sizeof(SeekableStreamState)), "SeekableStreamState_state");
  915. state->stream = stream;
  916. state->array = array;
  917. /* Ownership transferred to state. */
  918. stream = NULL;
  919. array = NULL;
  920. /* Stream takes ownership of state. */
  921. out = fz_new_output(ctx, sizeof state->buffer, state, SeekableOutputStream_write, NULL, SeekableOutputStream_drop);
  922. out->seek = SeekableOutputStream_seek;
  923. out->tell = SeekableOutputStream_tell;
  924. out->truncate = SeekableOutputStream_truncate;
  925. out->as_stream = SeekableOutputStream_as_stream;
  926. /* these are now owned by 'out' */
  927. state = NULL;
  928. pdf_write_journal(ctx, pdf, out);
  929. fz_close_output(ctx, out);
  930. }
  931. fz_always(ctx)
  932. {
  933. fz_drop_output(ctx, out);
  934. }
  935. fz_catch(ctx)
  936. {
  937. (*env)->DeleteGlobalRef(env, array);
  938. (*env)->DeleteGlobalRef(env, stream);
  939. jni_rethrow_void(env, ctx);
  940. }
  941. }
  942. JNIEXPORT void JNICALL
  943. FUN(PDFDocument_loadJournalWithStream)(JNIEnv *env, jobject self, jobject jstream)
  944. {
  945. fz_context *ctx = get_context(env);
  946. pdf_document *pdf = from_PDFDocument(env, self);
  947. SeekableStreamState *state = NULL;
  948. jobject stream = NULL;
  949. jbyteArray array = NULL;
  950. fz_stream *stm = NULL;
  951. if (!ctx || !pdf)
  952. return;
  953. if (!jstream)
  954. jni_throw_arg_void(env, "stream must not be null");
  955. fz_var(state);
  956. fz_var(stm);
  957. fz_var(stream);
  958. fz_var(array);
  959. stream = (*env)->NewGlobalRef(env, jstream);
  960. if (!stream)
  961. jni_throw_run_void(env, "invalid stream");
  962. array = (*env)->NewByteArray(env, sizeof state->buffer);
  963. if ((*env)->ExceptionCheck(env))
  964. {
  965. (*env)->DeleteGlobalRef(env, stream);
  966. return;
  967. }
  968. if (!array)
  969. {
  970. (*env)->DeleteGlobalRef(env, stream);
  971. jni_throw_run_void(env, "cannot create byte array");
  972. }
  973. array = (*env)->NewGlobalRef(env, array);
  974. if (!array)
  975. {
  976. (*env)->DeleteGlobalRef(env, stream);
  977. jni_throw_run_void(env, "cannot create global reference");
  978. }
  979. fz_try(ctx)
  980. {
  981. /* No exceptions can occur from here to stream owning state, so we must not free state. */
  982. state = Memento_label(fz_malloc(ctx, sizeof(SeekableStreamState)), "SeekableStreamState_state");
  983. state->stream = stream;
  984. state->array = array;
  985. /* Ownership transferred to state. */
  986. stream = NULL;
  987. array = NULL;
  988. /* Stream takes ownership of accstate. */
  989. stm = fz_new_stream(ctx, state, SeekableInputStream_next, SeekableInputStream_drop);
  990. stm->seek = SeekableInputStream_seek;
  991. pdf_read_journal(ctx, pdf, stm);
  992. }
  993. fz_always(ctx)
  994. {
  995. fz_drop_stream(ctx, stm);
  996. }
  997. fz_catch(ctx)
  998. {
  999. (*env)->DeleteGlobalRef(env, array);
  1000. (*env)->DeleteGlobalRef(env, stream);
  1001. jni_rethrow_void(env, ctx);
  1002. }
  1003. }
  1004. JNIEXPORT void JNICALL
  1005. FUN(PDFDocument_saveJournal)(JNIEnv *env, jobject self, jstring jfilename)
  1006. {
  1007. fz_context *ctx = get_context(env);
  1008. pdf_document *pdf = from_PDFDocument(env, self);
  1009. const char *filename = NULL;
  1010. if (!ctx || !pdf)
  1011. return;
  1012. if (!jfilename)
  1013. jni_throw_arg_void(env, "filename must not be null");
  1014. filename = (*env)->GetStringUTFChars(env, jfilename, NULL);
  1015. if (!filename)
  1016. jni_throw_run_void(env, "cannot get characters in filename");
  1017. fz_try(ctx)
  1018. pdf_save_journal(ctx, pdf, filename);
  1019. fz_always(ctx)
  1020. (*env)->ReleaseStringUTFChars(env, jfilename, filename);
  1021. fz_catch(ctx)
  1022. jni_rethrow_void(env, ctx);
  1023. }
  1024. JNIEXPORT void JNICALL
  1025. FUN(PDFDocument_loadJournal)(JNIEnv *env, jobject self, jstring jfilename)
  1026. {
  1027. fz_context *ctx = get_context(env);
  1028. pdf_document *pdf = from_PDFDocument(env, self);
  1029. const char *filename = NULL;
  1030. if (!ctx || !pdf)
  1031. return;
  1032. if (!jfilename)
  1033. jni_throw_arg_void(env, "filename must not be null");
  1034. filename = (*env)->GetStringUTFChars(env, jfilename, NULL);
  1035. if (!filename)
  1036. jni_throw_run_void(env, "cannot get characters in filename");
  1037. fz_try(ctx)
  1038. pdf_load_journal(ctx, pdf, filename);
  1039. fz_always(ctx)
  1040. (*env)->ReleaseStringUTFChars(env, jfilename, filename);
  1041. fz_catch(ctx)
  1042. jni_rethrow_void(env, ctx);
  1043. }
  1044. JNIEXPORT jint JNICALL
  1045. FUN(PDFDocument_undoRedoPosition)(JNIEnv *env, jobject self)
  1046. {
  1047. fz_context *ctx = get_context(env);
  1048. pdf_document *pdf = from_PDFDocument(env, self);
  1049. int steps;
  1050. if (!ctx || !pdf) return 0;
  1051. return pdf_undoredo_state(ctx, pdf, &steps);
  1052. }
  1053. JNIEXPORT jint JNICALL
  1054. FUN(PDFDocument_undoRedoSteps)(JNIEnv *env, jobject self)
  1055. {
  1056. fz_context *ctx = get_context(env);
  1057. pdf_document *pdf = from_PDFDocument(env, self);
  1058. int steps;
  1059. if (!ctx || !pdf) return 0;
  1060. (void)pdf_undoredo_state(ctx, pdf, &steps);
  1061. return steps;
  1062. }
  1063. JNIEXPORT jstring JNICALL
  1064. FUN(PDFDocument_undoRedoStep)(JNIEnv *env, jobject self, jint n)
  1065. {
  1066. fz_context *ctx = get_context(env);
  1067. pdf_document *pdf = from_PDFDocument(env, self);
  1068. const char *step;
  1069. if (!ctx || !pdf) return NULL;
  1070. step = pdf_undoredo_step(ctx, pdf, n);
  1071. return (*env)->NewStringUTF(env, step);
  1072. }
  1073. JNIEXPORT jboolean JNICALL
  1074. FUN(PDFDocument_canUndo)(JNIEnv *env, jobject self)
  1075. {
  1076. fz_context *ctx = get_context(env);
  1077. pdf_document *pdf = from_PDFDocument(env, self);
  1078. jboolean can = JNI_FALSE;
  1079. if (!ctx || !pdf) return JNI_FALSE;
  1080. fz_try(ctx)
  1081. can = pdf_can_undo(ctx, pdf);
  1082. fz_catch(ctx)
  1083. jni_rethrow(env, ctx);
  1084. return can;
  1085. }
  1086. JNIEXPORT jboolean JNICALL
  1087. FUN(PDFDocument_canRedo)(JNIEnv *env, jobject self)
  1088. {
  1089. fz_context *ctx = get_context(env);
  1090. pdf_document *pdf = from_PDFDocument(env, self);
  1091. jboolean can = JNI_FALSE;
  1092. if (!ctx || !pdf) return JNI_FALSE;
  1093. fz_try(ctx)
  1094. can = pdf_can_redo(ctx, pdf);
  1095. fz_catch(ctx)
  1096. jni_rethrow(env, ctx);
  1097. return can;
  1098. }
  1099. JNIEXPORT void JNICALL
  1100. FUN(PDFDocument_undo)(JNIEnv *env, jobject self)
  1101. {
  1102. fz_context *ctx = get_context(env);
  1103. pdf_document *pdf = from_PDFDocument(env, self);
  1104. if (!ctx || !pdf) return;
  1105. fz_try(ctx)
  1106. pdf_undo(ctx, pdf);
  1107. fz_catch(ctx)
  1108. jni_rethrow_void(env, ctx);
  1109. }
  1110. JNIEXPORT void JNICALL
  1111. FUN(PDFDocument_redo)(JNIEnv *env, jobject self)
  1112. {
  1113. fz_context *ctx = get_context(env);
  1114. pdf_document *pdf = from_PDFDocument(env, self);
  1115. if (!ctx || !pdf) return;
  1116. fz_try(ctx)
  1117. pdf_redo(ctx, pdf);
  1118. fz_catch(ctx)
  1119. jni_rethrow_void(env, ctx);
  1120. }
  1121. JNIEXPORT void JNICALL
  1122. FUN(PDFDocument_beginOperation)(JNIEnv *env, jobject self, jstring joperation)
  1123. {
  1124. fz_context *ctx = get_context(env);
  1125. pdf_document *pdf = from_PDFDocument(env, self);
  1126. const char *operation = NULL;
  1127. if (!ctx || !pdf) return;
  1128. if (!joperation) jni_throw_arg_void(env, "operation must not be null");
  1129. operation = (*env)->GetStringUTFChars(env, joperation, NULL);
  1130. if (!operation) return;
  1131. fz_try(ctx)
  1132. pdf_begin_operation(ctx, pdf, operation);
  1133. fz_always(ctx)
  1134. (*env)->ReleaseStringUTFChars(env, joperation, operation);
  1135. fz_catch(ctx)
  1136. jni_rethrow_void(env, ctx);
  1137. }
  1138. JNIEXPORT void JNICALL
  1139. FUN(PDFDocument_beginImplicitOperation)(JNIEnv *env, jobject self)
  1140. {
  1141. fz_context *ctx = get_context(env);
  1142. pdf_document *pdf = from_PDFDocument(env, self);
  1143. if (!ctx || !pdf) return;
  1144. fz_try(ctx)
  1145. pdf_begin_implicit_operation(ctx, pdf);
  1146. fz_catch(ctx)
  1147. jni_rethrow_void(env, ctx);
  1148. }
  1149. JNIEXPORT void JNICALL
  1150. FUN(PDFDocument_endOperation)(JNIEnv *env, jobject self)
  1151. {
  1152. fz_context *ctx = get_context(env);
  1153. pdf_document *pdf = from_PDFDocument(env, self);
  1154. if (!ctx || !pdf) return;
  1155. fz_try(ctx)
  1156. pdf_end_operation(ctx, pdf);
  1157. fz_catch(ctx)
  1158. jni_rethrow_void(env, ctx);
  1159. }
  1160. JNIEXPORT void JNICALL
  1161. FUN(PDFDocument_abandonOperation)(JNIEnv *env, jobject self)
  1162. {
  1163. fz_context *ctx = get_context(env);
  1164. pdf_document *pdf = from_PDFDocument(env, self);
  1165. if (!ctx || !pdf) return;
  1166. fz_try(ctx)
  1167. pdf_abandon_operation(ctx, pdf);
  1168. fz_catch(ctx)
  1169. jni_rethrow_void(env, ctx);
  1170. }
  1171. JNIEXPORT jboolean JNICALL
  1172. FUN(PDFDocument_isRedacted)(JNIEnv *env, jobject self)
  1173. {
  1174. fz_context *ctx = get_context(env);
  1175. pdf_document *pdf = from_PDFDocument(env, self);
  1176. if (!ctx || !pdf) return JNI_FALSE;
  1177. return pdf->redacted;
  1178. }
  1179. JNIEXPORT jint JNICALL
  1180. FUN(PDFDocument_getLanguage)(JNIEnv *env, jobject self)
  1181. {
  1182. fz_context *ctx = get_context(env);
  1183. pdf_document *pdf = from_PDFDocument(env, self);
  1184. int lang;
  1185. if (!ctx || !pdf) return FZ_LANG_UNSET;
  1186. fz_try(ctx)
  1187. lang = pdf_document_language(ctx, pdf);
  1188. fz_catch(ctx)
  1189. jni_rethrow(env, ctx);
  1190. return lang;
  1191. }
  1192. JNIEXPORT void JNICALL
  1193. FUN(PDFDocument_setLanguage)(JNIEnv *env, jobject self, jint lang)
  1194. {
  1195. fz_context *ctx = get_context(env);
  1196. pdf_document *pdf = from_PDFDocument(env, self);
  1197. if (!ctx || !pdf) return;
  1198. fz_try(ctx)
  1199. pdf_set_document_language(ctx, pdf, lang);
  1200. fz_catch(ctx)
  1201. jni_rethrow_void(env, ctx);
  1202. }
  1203. JNIEXPORT jint JNICALL
  1204. FUN(PDFDocument_countSignatures)(JNIEnv *env, jobject self)
  1205. {
  1206. fz_context *ctx = get_context(env);
  1207. pdf_document *pdf = from_PDFDocument_safe(env, self);
  1208. jint val = -1;
  1209. if (!ctx || !pdf) return -1;
  1210. fz_try(ctx)
  1211. val = pdf_count_signatures(ctx, pdf);
  1212. fz_catch(ctx)
  1213. jni_rethrow(env, ctx);
  1214. return val;
  1215. }
  1216. JNIEXPORT jobject JNICALL
  1217. FUN(PDFDocument_addEmbeddedFile)(JNIEnv *env, jobject self, jstring jfilename, jstring jmimetype, jobject jcontents, jlong created, jlong modified, jboolean addChecksum)
  1218. {
  1219. fz_context *ctx = get_context(env);
  1220. pdf_document *pdf = from_PDFDocument_safe(env, self);
  1221. const char *filename = NULL;
  1222. const char *mimetype = NULL;
  1223. fz_buffer *contents = from_Buffer(env, jcontents);
  1224. pdf_obj *fs = NULL;
  1225. if (!ctx || !pdf) return NULL;
  1226. if (!jfilename) jni_throw_arg(env, "filename must not be null");
  1227. filename = (*env)->GetStringUTFChars(env, jfilename, NULL);
  1228. if (!filename) return NULL;
  1229. if (jmimetype)
  1230. {
  1231. mimetype = (*env)->GetStringUTFChars(env, jmimetype, NULL);
  1232. if (!mimetype)
  1233. {
  1234. (*env)->ReleaseStringUTFChars(env, jfilename, filename);
  1235. return NULL;
  1236. }
  1237. }
  1238. fz_try(ctx)
  1239. fs = pdf_add_embedded_file(ctx, pdf, filename, mimetype, contents, created, modified, addChecksum);
  1240. fz_always(ctx)
  1241. {
  1242. if (mimetype)
  1243. (*env)->ReleaseStringUTFChars(env, jmimetype, mimetype);
  1244. (*env)->ReleaseStringUTFChars(env, jfilename, filename);
  1245. }
  1246. fz_catch(ctx)
  1247. jni_rethrow(env, ctx);
  1248. return to_PDFObject_safe(ctx, env, fs);
  1249. }
  1250. JNIEXPORT jstring JNICALL
  1251. FUN(PDFDocument_getEmbeddedFileParams)(JNIEnv *env, jobject self, jobject jfs)
  1252. {
  1253. return FUN(PDFDocument_getFileSpecParams)(env, self, jfs);
  1254. }
  1255. JNIEXPORT jstring JNICALL
  1256. FUN(PDFDocument_getFileSpecParams)(JNIEnv *env, jobject self, jobject jfs)
  1257. {
  1258. fz_context *ctx = get_context(env);
  1259. pdf_obj *fs = from_PDFObject_safe(env, jfs);
  1260. pdf_embedded_file_params params;
  1261. jstring jfilename = NULL;
  1262. jstring jmimetype = NULL;
  1263. fz_try(ctx)
  1264. pdf_get_embedded_file_params(ctx, fs, &params);
  1265. fz_catch(ctx)
  1266. jni_rethrow(env, ctx);
  1267. jfilename = (*env)->NewStringUTF(env, params.filename);
  1268. if (!jfilename || (*env)->ExceptionCheck(env))
  1269. return NULL;
  1270. jmimetype = (*env)->NewStringUTF(env, params.mimetype);
  1271. if (!jmimetype || (*env)->ExceptionCheck(env))
  1272. return NULL;
  1273. return (*env)->NewObject(env, cls_PDFDocument_PDFEmbeddedFileParams, mid_PDFDocument_PDFEmbeddedFileParams_init,
  1274. jfilename, jmimetype, params.size, params.created * 1000, params.modified * 1000);
  1275. }
  1276. JNIEXPORT jobject JNICALL
  1277. FUN(PDFDocument_loadEmbeddedFileContents)(JNIEnv *env, jobject self, jobject jfs)
  1278. {
  1279. fz_context *ctx = get_context(env);
  1280. pdf_obj *fs = from_PDFObject_safe(env, jfs);
  1281. fz_buffer *contents = NULL;
  1282. fz_try(ctx)
  1283. contents = pdf_load_embedded_file_contents(ctx, fs);
  1284. fz_catch(ctx)
  1285. jni_rethrow(env, ctx);
  1286. return to_Buffer_safe(ctx, env, contents);
  1287. }
  1288. JNIEXPORT jboolean JNICALL
  1289. FUN(PDFDocument_verifyEmbeddedFileChecksum)(JNIEnv *env, jobject self, jobject jfs)
  1290. {
  1291. fz_context *ctx = get_context(env);
  1292. pdf_obj *fs = from_PDFObject_safe(env, jfs);
  1293. int valid = 0;
  1294. fz_try(ctx)
  1295. valid = pdf_verify_embedded_file_checksum(ctx, fs);
  1296. fz_catch(ctx)
  1297. jni_rethrow(env, ctx);
  1298. return valid ? JNI_TRUE : JNI_FALSE;
  1299. }
  1300. JNIEXPORT jboolean JNICALL
  1301. FUN(PDFDocument_isEmbeddedFile)(JNIEnv *env, jobject self, jobject jfs)
  1302. {
  1303. fz_context *ctx = get_context(env);
  1304. pdf_obj *fs = from_PDFObject_safe(env, jfs);
  1305. int embedded = 0;
  1306. fz_try(ctx)
  1307. embedded = pdf_is_embedded_file(ctx, fs);
  1308. fz_catch(ctx)
  1309. jni_rethrow(env, ctx);
  1310. return embedded ? JNI_TRUE : JNI_FALSE;
  1311. }
  1312. JNIEXPORT void JNICALL
  1313. FUN(PDFDocument_setPageLabels)(JNIEnv *env, jobject self, jint index, jint style, jstring jprefix, jint start)
  1314. {
  1315. fz_context *ctx = get_context(env);
  1316. pdf_document *pdf = from_PDFDocument_safe(env, self);
  1317. const char *prefix = NULL;
  1318. if (jprefix)
  1319. {
  1320. prefix = (*env)->GetStringUTFChars(env, jprefix, NULL);
  1321. if (!prefix) return;
  1322. }
  1323. fz_try(ctx)
  1324. {
  1325. pdf_set_page_labels(ctx, pdf, index, style, prefix, start);
  1326. }
  1327. fz_always(ctx)
  1328. {
  1329. if (jprefix)
  1330. (*env)->ReleaseStringUTFChars(env, jprefix, prefix);
  1331. }
  1332. fz_catch(ctx)
  1333. {
  1334. jni_rethrow_void(env, ctx);
  1335. }
  1336. }
  1337. JNIEXPORT void JNICALL
  1338. FUN(PDFDocument_deletePageLabels)(JNIEnv *env, jobject self, jint index)
  1339. {
  1340. fz_context *ctx = get_context(env);
  1341. pdf_document *pdf = from_PDFDocument_safe(env, self);
  1342. fz_try(ctx)
  1343. pdf_delete_page_labels(ctx, pdf, index);
  1344. fz_catch(ctx)
  1345. jni_rethrow_void(env, ctx);
  1346. }
  1347. JNIEXPORT jint JNICALL
  1348. FUN(PDFDocument_getVersion)(JNIEnv *env, jobject self)
  1349. {
  1350. fz_context *ctx = get_context(env);
  1351. pdf_document *pdf = from_PDFDocument(env, self);
  1352. int version = 0;
  1353. if (!ctx || !pdf) return 0;
  1354. fz_try(ctx)
  1355. version = pdf_version(ctx, pdf);
  1356. fz_catch(ctx)
  1357. jni_rethrow(env, ctx);
  1358. return version;
  1359. }
  1360. JNIEXPORT jstring JNICALL
  1361. FUN(PDFDocument_formatURIFromPathAndNamedDest)(JNIEnv *env, jclass cls, jstring jpath, jstring jname)
  1362. {
  1363. fz_context *ctx = get_context(env);
  1364. char *uri = NULL;
  1365. jobject juri;
  1366. const char *path = NULL;
  1367. const char *name = NULL;
  1368. if (jpath)
  1369. {
  1370. path = (*env)->GetStringUTFChars(env, jpath, NULL);
  1371. if (!path) return NULL;
  1372. }
  1373. if (jname)
  1374. {
  1375. name = (*env)->GetStringUTFChars(env, jname, NULL);
  1376. if (!name) return NULL;
  1377. }
  1378. fz_try(ctx)
  1379. uri = pdf_new_uri_from_path_and_named_dest(ctx, path, name);
  1380. fz_always(ctx)
  1381. {
  1382. if (jname)
  1383. (*env)->ReleaseStringUTFChars(env, jname, name);
  1384. if (jpath)
  1385. (*env)->ReleaseStringUTFChars(env, jpath, path);
  1386. }
  1387. fz_catch(ctx)
  1388. jni_rethrow(env, ctx);
  1389. juri = (*env)->NewStringUTF(env, uri);
  1390. fz_free(ctx, uri);
  1391. return juri;
  1392. }
  1393. JNIEXPORT jstring JNICALL
  1394. FUN(PDFDocument_formatURIFromPathAndExplicitDest)(JNIEnv *env, jclass cls, jstring jpath, jobject jdest)
  1395. {
  1396. fz_context *ctx = get_context(env);
  1397. fz_link_dest dest = from_LinkDestination(env, jdest);
  1398. char *uri = NULL;
  1399. jobject juri;
  1400. const char *path = NULL;
  1401. if (jpath)
  1402. {
  1403. path = (*env)->GetStringUTFChars(env, jpath, NULL);
  1404. if (!path) return NULL;
  1405. }
  1406. fz_try(ctx)
  1407. uri = pdf_new_uri_from_path_and_explicit_dest(ctx, path, dest);
  1408. fz_always(ctx)
  1409. {
  1410. if (jpath)
  1411. (*env)->ReleaseStringUTFChars(env, jpath, path);
  1412. }
  1413. fz_catch(ctx)
  1414. jni_rethrow(env, ctx);
  1415. juri = (*env)->NewStringUTF(env, uri);
  1416. fz_free(ctx, uri);
  1417. return juri;
  1418. }
  1419. JNIEXPORT jstring JNICALL
  1420. FUN(PDFDocument_appendNamedDestToURI)(JNIEnv *env, jclass cls, jstring jurl, jstring jname)
  1421. {
  1422. fz_context *ctx = get_context(env);
  1423. char *uri = NULL;
  1424. jobject juri;
  1425. const char *url = NULL;
  1426. const char *name = NULL;
  1427. if (jurl)
  1428. {
  1429. url = (*env)->GetStringUTFChars(env, jurl, NULL);
  1430. if (!url) return NULL;
  1431. }
  1432. if (jname)
  1433. {
  1434. name = (*env)->GetStringUTFChars(env, jname, NULL);
  1435. if (!name) return NULL;
  1436. }
  1437. fz_try(ctx)
  1438. uri = pdf_append_named_dest_to_uri(ctx, url, name);
  1439. fz_always(ctx)
  1440. {
  1441. if (jname)
  1442. (*env)->ReleaseStringUTFChars(env, jname, name);
  1443. if (jurl)
  1444. (*env)->ReleaseStringUTFChars(env, jurl, url);
  1445. }
  1446. fz_catch(ctx)
  1447. jni_rethrow(env, ctx);
  1448. juri = (*env)->NewStringUTF(env, uri);
  1449. fz_free(ctx, uri);
  1450. return juri;
  1451. }
  1452. JNIEXPORT jstring JNICALL
  1453. FUN(PDFDocument_appendExplicitDestToURI)(JNIEnv *env, jclass cls, jstring jurl, jobject jdest)
  1454. {
  1455. fz_context *ctx = get_context(env);
  1456. fz_link_dest dest = from_LinkDestination(env, jdest);
  1457. char *uri = NULL;
  1458. jobject juri;
  1459. const char *url = NULL;
  1460. if (jurl)
  1461. {
  1462. url = (*env)->GetStringUTFChars(env, jurl, NULL);
  1463. if (!url) return NULL;
  1464. }
  1465. fz_try(ctx)
  1466. uri = pdf_append_explicit_dest_to_uri(ctx, url, dest);
  1467. fz_always(ctx)
  1468. {
  1469. if (jurl)
  1470. (*env)->ReleaseStringUTFChars(env, jurl, url);
  1471. }
  1472. fz_catch(ctx)
  1473. jni_rethrow(env, ctx);
  1474. juri = (*env)->NewStringUTF(env, uri);
  1475. fz_free(ctx, uri);
  1476. return juri;
  1477. }
  1478. JNIEXPORT void JNICALL
  1479. FUN(PDFDocument_rearrangePages)(JNIEnv *env, jobject self, jobject jpages)
  1480. {
  1481. fz_context *ctx = get_context(env);
  1482. pdf_document *pdf = from_PDFDocument(env, self);
  1483. jsize len = 0;
  1484. int *pages = NULL;
  1485. if (!ctx || !pdf) return;
  1486. len = (*env)->GetArrayLength(env, jpages);
  1487. fz_try(ctx)
  1488. pages = fz_malloc_array(ctx, len, int);
  1489. fz_catch(ctx)
  1490. jni_rethrow_void(env, ctx);
  1491. (*env)->GetIntArrayRegion(env, jpages, 0, len, pages);
  1492. if ((*env)->ExceptionCheck(env))
  1493. {
  1494. fz_free(ctx, pages);
  1495. return;
  1496. }
  1497. fz_try(ctx)
  1498. pdf_rearrange_pages(ctx, pdf, len, pages, PDF_CLEAN_STRUCTURE_DROP);
  1499. fz_always(ctx)
  1500. fz_free(ctx, pages);
  1501. fz_catch(ctx)
  1502. jni_rethrow_void(env, ctx);
  1503. }
  1504. JNIEXPORT jint JNICALL
  1505. FUN(PDFDocument_countAssociatedFiles)(JNIEnv *env, jobject self)
  1506. {
  1507. fz_context *ctx = get_context(env);
  1508. pdf_document *doc = from_PDFDocument(env, self);
  1509. int n;
  1510. if (!ctx) return 0;
  1511. fz_try(ctx)
  1512. n = pdf_count_document_associated_files(ctx, doc);
  1513. fz_catch(ctx)
  1514. jni_rethrow(env, ctx);
  1515. return n;
  1516. }
  1517. JNIEXPORT jobject JNICALL
  1518. FUN(PDFDocument_associatedFile)(JNIEnv *env, jobject self, jint idx)
  1519. {
  1520. fz_context *ctx = get_context(env);
  1521. pdf_document *doc = from_PDFDocument(env, self);
  1522. pdf_obj *af;
  1523. if (!ctx) return NULL;
  1524. fz_try(ctx)
  1525. af = pdf_document_associated_file(ctx, doc, idx);
  1526. fz_catch(ctx)
  1527. jni_rethrow(env, ctx);
  1528. return to_PDFObject_safe_own(ctx, env, af);
  1529. }
  1530. JNIEXPORT jint JNICALL
  1531. FUN(PDFDocument_zugferdProfile)(JNIEnv *env, jobject self)
  1532. {
  1533. fz_context *ctx = get_context(env);
  1534. pdf_document *doc = from_PDFDocument(env, self);
  1535. enum pdf_zugferd_profile p;
  1536. float version;
  1537. if (!ctx) return 0;
  1538. fz_try(ctx)
  1539. p = pdf_zugferd_profile(ctx, doc, &version);
  1540. fz_catch(ctx)
  1541. jni_rethrow(env, ctx);
  1542. return (int)p;
  1543. }
  1544. JNIEXPORT jfloat JNICALL
  1545. FUN(PDFDocument_zugferdVersion)(JNIEnv *env, jobject self)
  1546. {
  1547. fz_context *ctx = get_context(env);
  1548. pdf_document *doc = from_PDFDocument(env, self);
  1549. float version;
  1550. if (!ctx) return 0;
  1551. fz_try(ctx)
  1552. (void) pdf_zugferd_profile(ctx, doc, &version);
  1553. fz_catch(ctx)
  1554. jni_rethrow(env, ctx);
  1555. return (jfloat)version;
  1556. }
  1557. JNIEXPORT jobject JNICALL
  1558. FUN(PDFDocument_zugferdXML)(JNIEnv *env, jobject self)
  1559. {
  1560. fz_context *ctx = get_context(env);
  1561. pdf_document *doc = from_PDFDocument(env, self);
  1562. fz_buffer *buf;
  1563. if (!ctx) return NULL;
  1564. fz_try(ctx)
  1565. buf = pdf_zugferd_xml(ctx, doc);
  1566. fz_catch(ctx)
  1567. jni_rethrow(env, ctx);
  1568. return to_Buffer_safe_own(ctx, env, buf);
  1569. }
  1570. JNIEXPORT jobject JNICALL
  1571. FUN(PDFDocument_loadImage)(JNIEnv *env, jobject self, jobject jobj)
  1572. {
  1573. fz_context *ctx = get_context(env);
  1574. pdf_document *doc = from_PDFDocument(env, self);
  1575. pdf_obj *obj = from_PDFObject(env, jobj);
  1576. fz_image *img;
  1577. if (!ctx) return NULL;
  1578. fz_try(ctx)
  1579. img = pdf_load_image(ctx, doc, obj);
  1580. fz_catch(ctx)
  1581. jni_rethrow(env, ctx);
  1582. return to_Image_safe_own(ctx, env, img);
  1583. }
  1584. JNIEXPORT jobject JNICALL
  1585. FUN(PDFDocument_lookupDest)(JNIEnv *env, jobject self, jobject jdest)
  1586. {
  1587. fz_context *ctx = get_context(env);
  1588. pdf_document *doc = from_PDFDocument(env, self);
  1589. pdf_obj *dest = from_PDFObject(env, jdest);
  1590. pdf_obj *obj = NULL;
  1591. if (!ctx) return NULL;
  1592. fz_try(ctx)
  1593. obj = pdf_lookup_dest(ctx, doc, dest);
  1594. fz_catch(ctx)
  1595. jni_rethrow(env, ctx);
  1596. return to_PDFObject_safe_own(ctx, env, obj);
  1597. }
  1598. JNIEXPORT jint JNICALL
  1599. FUN(PDFDocument_countLayers)(JNIEnv *env, jobject self)
  1600. {
  1601. fz_context *ctx = get_context(env);
  1602. pdf_document *doc = from_PDFDocument(env, self);
  1603. jint layers = 0;
  1604. fz_try(ctx)
  1605. layers = pdf_count_layers(ctx, doc);
  1606. fz_catch(ctx)
  1607. jni_rethrow(env, ctx);
  1608. return layers;
  1609. }
  1610. JNIEXPORT jboolean JNICALL
  1611. FUN(PDFDocument_isLayerVisible)(JNIEnv *env, jobject self, jint layer)
  1612. {
  1613. fz_context *ctx = get_context(env);
  1614. pdf_document *doc = from_PDFDocument(env, self);
  1615. jboolean visible = JNI_FALSE;
  1616. fz_try(ctx)
  1617. visible = pdf_layer_is_enabled(ctx, doc, layer);
  1618. fz_catch(ctx)
  1619. jni_rethrow(env, ctx);
  1620. return visible;
  1621. }
  1622. JNIEXPORT void JNICALL
  1623. FUN(PDFDocument_setLayerVisible)(JNIEnv *env, jobject self, jint layer, jboolean visible)
  1624. {
  1625. fz_context *ctx = get_context(env);
  1626. pdf_document *doc = from_PDFDocument(env, self);
  1627. fz_try(ctx)
  1628. pdf_enable_layer(ctx, doc, layer, visible);
  1629. fz_catch(ctx)
  1630. jni_rethrow_void(env, ctx);
  1631. }
  1632. JNIEXPORT jstring JNICALL
  1633. FUN(PDFDocument_getLayerName)(JNIEnv *env, jobject self, jint layer)
  1634. {
  1635. fz_context *ctx = get_context(env);
  1636. pdf_document *doc = from_PDFDocument(env, self);
  1637. const char *name = NULL;
  1638. fz_try(ctx)
  1639. name = pdf_layer_name(ctx, doc, layer);
  1640. fz_catch(ctx)
  1641. jni_rethrow(env, ctx);
  1642. return (*env)->NewStringUTF(env, name);
  1643. }
  1644. JNIEXPORT void JNICALL
  1645. FUN(PDFDocument_subsetFonts)(JNIEnv *env, jobject self)
  1646. {
  1647. fz_context *ctx = get_context(env);
  1648. pdf_document *doc = from_PDFDocument(env, self);
  1649. fz_try(ctx)
  1650. pdf_subset_fonts(ctx, doc, 0, NULL);
  1651. fz_catch(ctx)
  1652. jni_rethrow_void(env, ctx);
  1653. }
  1654. JNIEXPORT void JNICALL
  1655. FUN(PDFDocument_bake)(JNIEnv *env, jobject self, jboolean bake_annots, jboolean bake_widgets)
  1656. {
  1657. fz_context *ctx = get_context(env);
  1658. pdf_document *doc = from_PDFDocument(env, self);
  1659. fz_try(ctx)
  1660. pdf_bake_document(ctx, doc, bake_annots, bake_widgets);
  1661. fz_catch(ctx)
  1662. jni_rethrow_void(env, ctx);
  1663. }