How can I improve performance of viewing a PDF on Android with custom encryption?

Question:

We have PDF files encrypted using our own encryption, but find that it takes a while to view the PDF file, especially on slower hardware. Currently we pass a Stream to the PDFDoc constructor. How can I improve this?

Answer:

If the following conditions are true then there is something you can do.

  1. Encrypted PDF is on disk or in memory.
  2. Encrypted PDF is same size as plain text PDF, and data is not rearranged at all.
  3. You have random access to any offset of the encrypted file
  4. You can decrypt any part/chunk in any order.

If all the above is true, the you could create a CustomFilter, and instantiate your PDFDoc object using the Filter. The advantage here is that decryption and rendering would be done on the fly, and you would see the first page render much sooner.

This is the class you need to implement.
http://www.pdftron.com/pdfnet/mobile/docs/Android/pdfnet/javadoc/reference/com/pdftron/filters/CustomFilter.html

Note, the reason for #2, is that your filter will get requests for different chunks of data, as PDFNet jumps around the PDF getting the data it needs. You need to be able to decrypt these chunks and provide the data. If you can’t decrypt in random order, and if the offsets are different, then this would not work.

If the encrypted PDF is larger than the plain text version, and/or encrypted data has been moved around, then the byte offset requests going to your CustomFilter will not be correct. You might be able to work around this by translating your self, but this would certainly be more complex.