Skip to main content

Get metadata from pdf via Mupdf library in Android

In this link, one of my previous posts, I have explained how to add mupdf library as a module into an Android Studio project. You can either start with this guide or directly continue to this post.

The target file to edit is  mupdf.c. This file contains ndk functions in order to be called from java part. We will just add a ndk function to fetch metadata of a pdf file. Because mupdf library has a functionality to provide it, we don't need any further coding.

JNIEXPORT jobjectArray JNICALL
JNI_FN(MuPDFCore_metadataInternal)(JNIEnv * env, jobject thiz)
{
 char info[64];
 globals *glo = get_globals(env, thiz);
 jobjectArray arr;
 jclass stringClass;
 const int nkeys = 4;
 const char *keys[nkeys];
 int i;
 keys[0] = "Title";
 keys[1] = "Author";
 keys[2] = "Subject";
 keys[3] = "Keywords";

 stringClass = (*env)->FindClass(env, "java/lang/String");
 arr = (*env)->NewObjectArray(env, nkeys, stringClass, NULL);


 LOGI("Getting metadata");
 for(i=0; idoc, FZ_META_INFO, info, sizeof(info));

  LOGI("%s : %s", keys[i], info);
  jstring s = (*env)->NewStringUTF(env, info);
  if (s != NULL) {
   (*env)->SetObjectArrayElement(env, arr, i, s);
  }
  (*env)->DeleteLocalRef(env, s);
 }

 return arr;
}
This ndk function returns a string array which contains metadata information, respectively to keys in keys array. If there is no such info for a key, it returns an empty string. As seen, just 4 of keys are retrieved. There are more in pdf, you can use them if you want too:

  • Title
  • Author
  • Subject
  • Keywords
  • Creator
  • Producer
  • CreationDate
  • ModDate
  • Trapped
Further explanation, refer to pdf reference guide.

I am working with mupdf with version: 1.6. When I use the function above, I have get an error. After some debugging, I recognized that the problem is on following line in pdf-xref.c file.

info = pdf_dict_gets(info, *(char **)ptr);
ptr is a void array and passed as parameter to the function. What I have passed in ndk function above is a char array. It works well until this line, but here it results a fatal error:

(Fatal signal 11 (SIGSEGV), code 1, fault addr 0x6c746954 in tid 29833)
I have done following fix on same line:

info = pdf_dict_gets(info, (char *)ptr);
Then, the problem has been solved and this functionality has begun to work smoothly.

Mupdf is a great library for pdf rendering. It contains many functionalities to perform on pdf files. Beyond all, it can be integrated to mobile platforms. These characteristics easily show why people tend to prefer this library. Even my post about it takes great hits. All these strongly point out that these guys deserve appreciation. Good job!

Comments

  1. Really Great Job...!!!

    When i try copy and pasted into mupdf.c & compiling. it says FZ_META_INFO undeclared. please help me to fix this what shall i do for this.

    ReplyDelete

Post a Comment

Popular posts from this blog

Integration of MuPDF Project as a Library into an Android Studio Project

I have needed to use MuPDF library in my android project. After some research, I have seen that there are many integration tutorials but, but integrated projects are developed on Eclipse. For projects on AndroidStudio+Gradle, there is no example. I mean there is no specific example which exactly refers to this issue. So, after achieving my goal, I want to share the steps publicly so that it can be reused by others.

Migration from Proxmox to Openstack

I needed to migrate virtual machines in proxmox to openstack. VMs are in raw format. I needed to take some actions for a succesfull migration. I have perform all actions on Ubuntu 12.04 with virt-manager. qemu-kvm is installed. Here is the list of actions that I took: First, close the machine and copy the image file into your Ubuntu. Convert raw image to qcow2 format: qemu-img convert -O qcow2 image1.raw image1.qcow2 You need the image in qcow2 format for compatibility with openstack platform.  Open the converted image in virt-manager. Before opening, edit disk options. Under ' advanced options ' section, select ' qcow2 ' as ' storage forma t '. Start the virtual machine. You should see the login screen soon. (If you don't set storage format, vm will not find a bootable device. )   If everything is ok so far, close the vm. Take qcow2 image and upload it into glance. It may take time depending on size of it. After this process is completed, open a...

How to avoid API-level warning of Android Studio

Before giving the solution, let's start with a scenario. setSelectionFromTop() is a new method in Android Lollipop API. This method is basically beneficial to precisely keep scroll state of a ListView. By keeping that info, a developer can go back to old scroll state after doing some operation like data set change. You are aware of API level and you do your control before you call this function: if (currentapiVersion >= Build.VERSION_CODES.LOLLIPOP) { srlistview.setSelectionFromTop(index, top); } But if project minSdk is set to a lower level (in this case it is 15), this warning will still be displayed in Android Studio: