2013-02-05 54 views
5

随着qpdf,你可以简单地从PDF删除限制/加密,像这样:从PDFBOX PDF中删除加密,像qpdf

qpdf --decrypt infile outfile 

我愿做同样的事情PDFBox的在Java中:

PDDocument doc = PDDocument.load(inputFilename); 
if(doc.isEncrypted()) 
{ 
    //remove the encryption to alter the document 
} 

我已经试过StandardDecryptionMaterial这一点,但我不知道主人密码。 qpdf如何做到这一点?

样品文件:https://issues.apache.org/jira/secure/attachment/12514714/in.pdf

+1

根据的PDF加密类型很容易解密(怎么回事,以显示它)。但是它击败了这种加密的概念,允许没有授权的人(所有者密码)删除加密。如果您已加密PDF文件并且没有所有者密码,则如果您需要未加密的版本,则需要转向文档所有者。 – mkl

回答

17

这是你需要做什么。受到了PDFBox WriteDecodedDoc工具的启发。您可能需要包括BouncyCastle的罐子(http://www.bouncycastle.org/latest_releases.html

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

这是不赞成使用的API。请按照http://stackoverflow.com/a/29676262/873282中所述使用新的API。 – koppor

+0

没有这样的方法doc.decrypt() - 这是足够使用doc.setAllSecurityToBeRemoved(真); –