JNIEXPORT void JNICALL Java_org_libharu_Document_setPassword (JNIEnv *env, jobject obj, jstring jownerpass, jstring juserpass) { haru_setup_error_handler(env, __func__); const char *ownerpass = (char*)env->GetStringUTFChars(jownerpass, NULL); if (ownerpass == NULL) return; const char *userpass = (char*)env->GetStringUTFChars(juserpass, NULL); if (userpass == NULL) return; HPDF_Doc pdf = get_HPDF_Doc(env, obj); HPDF_SetPassword(pdf, ownerpass, userpass); env->ReleaseStringUTFChars(jownerpass, ownerpass); env->ReleaseStringUTFChars(juserpass, userpass); haru_clear_error_handler(); }
/** saves the pdf with optional encryption */ nserror save_pdf(const char *path) { nserror res = NSERROR_OK; if (option_enable_PDF_password && owner_pass != NULL ) { HPDF_SetPassword(pdf_doc, owner_pass, user_pass); HPDF_SetEncryptionMode(pdf_doc, HPDF_ENCRYPT_R3, 16); free(owner_pass); free(user_pass); } if (path != NULL) { if (HPDF_SaveToFile(pdf_doc, path) != HPDF_OK) { remove(path); res = NSERROR_SAVE_FAILED; } } HPDF_Free(pdf_doc); pdf_doc = NULL; return res; }
/** saves the pdf optionally encrypting it before*/ void save_pdf(const char *path) { bool success = false; if (option_enable_PDF_password && owner_pass != NULL ) { HPDF_SetPassword(pdf_doc, owner_pass, user_pass); HPDF_SetEncryptionMode(pdf_doc, HPDF_ENCRYPT_R3, 16); free(owner_pass); free(user_pass); } if (path != NULL) { if (HPDF_SaveToFile(pdf_doc, path) != HPDF_OK) remove(path); else success = true; } if (!success) warn_user("Unable to save PDF file.", 0); HPDF_Free(pdf_doc); pdf_doc = NULL; }