1

I have to create a function that tells me is an uploaded PDF file encrypted-only, timestamped-only, none, or both. So far I am using PDFBox 2.0.5 and only success knowing timestamped-only and none file.

Here is the current code:

try{
    InputStream fis = new ByteArrayInputStream(file.getBytes());
    PDDocument d = PDDodument.load(fis); //encrypted file will go to InvalidPasswordException error
    //***file not encrypted***
    List<PDSignature> a = d.getSignatureDictionaries();
    List<PDSignatureField> b = d.getSignatureFields();
    if(a.size()==0 && b.size()==0){
        //***file not timestamped***
    }else{
        //***file timestamped***
    }
} catch(InvalidPasswordException e){
    //***file encrypted***
    //***is file timestamped?***
    //***is file not timestamped?***
} catch(IOException e){
}

My question is, how to know if there any timestamp (as signature) on encrypted pdf file?

Note: I have found several PDFBox usage example but rarely find PDFBox latest version implemented example

Update note: file is an uploaded MultipartFile object, having hard time getting its path avoiding copying or transferring it into another object type

Valiant
  • 11
  • 5
  • Do you have the password / private key for decrypting the encrypted documents? – mkl Jul 05 '17 at 05:01
  • @mkl unfortunately the function wouldnt know each pdf password, I hope it is possible to get through it without password though – Valiant Jul 05 '17 at 08:09
  • You can call `PDDocument.load(file.getBytes());` that is more efficient. I have some code for the timestamps but it looks quite crazy, it is based on http://www.massapi.com/source/maven/20/11/2011209121/maven2/br/net/woodstock/rockframework/rockframework-security/3.0.0/rockframework-security-3.0.0-sources.jar/rockframework-security-3.0.0-sources.jar.unzip/br/net/woodstock/rockframework/security/sign/impl/BouncyCastlePKCS7Signer.java.html#286 so I'll wait if somebody has anything better. – Tilman Hausherr Jul 05 '17 at 08:18
  • 1
    Do also call isEncrypted(), pdf files may have an empty user password but non empty owner password, this is for restricted rights. And there can be signatures without timestamps. – Tilman Hausherr Jul 05 '17 at 08:25
  • @TilmanHausherr that would be great if I could use isEncrypted(), but I cant even construct object without getting password false error from a temporary multipartfile type of object – Valiant Jul 06 '17 at 06:18
  • I meant call `isEncrypted()` after you were able to open a file without getting exception. At the place where you wrote `***file not encrypted***`. – Tilman Hausherr Jul 06 '17 at 06:36

0 Answers0