Techfee

About Pdfs With Only Owner's Password Deciphere by Using Pdfbox

| Comments

Some pdfs have owner password to prevent user from copying, extracting or printing … etc …

By using pdf box, you can easily deciphere this kind of open and read only pdfs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
     PDDocument document = PDDocument.load(filePath, true);

      if (document.isEncrypted()) {
          try {
              document.decrypt("");
              document.setAllSecurityToBeRemoved(true);
          } catch (Exception e) {
              throw new UnableToDeciphereException("The document is encrypted, and we can't decrypt it.", e);
          }
      }

      ByteArrayOutputStream outStream = new ByteArrayOutputStream();

      document.save(outStream);
      
      document.close();

      byte[] decipheredBytes = outStream.toByteArray();

or you can use a FileOutputStream to save it into a file. Actually, document.save() has a parameter filepath, so you can , for example : document.save(“/Users/User/Desktop/abc.pdf”) .

Comments