2

I am trying to import a pdf in my application using PdfRenderer - Android's in-built solution.

With this i am facing problem mentioned on this link.

google issue

My requirement is to check if any password is password protected or not without using any 3rd party libs or jar.

How can this be done ?

Note : Similar reported issues discuss solution using 3rd party libs only but this is not i want.

I am already able to accomplish most of my job using PdfRenderer class. And just to check if any pdf is password protected, i do not want to use any library.

userv
  • 2,527
  • 4
  • 27
  • 36
  • ***without** using any 3rd party libs* means that you will have to re-invent parts of them. If your input documents are arbItrary, that is not trivial. – mkl Jul 03 '15 at 10:21
  • Where or how should i start ? If it asks for a lot i will reconsider using 3rd party libs. – userv Jul 03 '15 at 11:31
  • *Where or how should i start* - read the [PDF specification](http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/PDF32000_2008.pdf), especially the sections 7.5 "File Structure" and 7.6 "Encryption". Then write code to read the file trailer dictionary (which *may* be absorbed by a cross reference stream dictionary) and inspect its **Encrypt** entry. – mkl Jul 03 '15 at 11:43

1 Answers1

0

In Android SDK there is android.graphics.pdf package to work with pdf document but there is no suitable method to check pdf password is vaild or not.

So I am recommend to use PdfBox-Android library to check pdf password is vaild or not without opening pdf document.

Here is the example code for that.

public boolean isPdfPasswordValid(String filePath, String password) {
    try {
        File file = new File(filePath);
        PDDocument document = PDDocument.load(file,password);
         if (document.isEncrypted()) {
             document.setAllSecurityToBeRemoved(true);
             document.getPage(0);
             document.close();
             return true;
         } else {
             document.close();
             return false;
         }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

Add this gradle dependancy in module level build.gradle file in android project

implementation 'com.tom-roush:pdfbox-android:2.0.27.0'