用于加密/解密pdf文件的Java API

DG.*_*DG. 7 java pdf encryption

我需要加密和解密pdf文件.是否有免费或低成本的Java API可以做到这一点?基本上我需要隐藏普通用户的文件.有关以编程方式实现该建议的任何其他建议吗?

谢谢,深

Fre*_*rik 7

使用iText:

  // Assuming you provide the following yourself:
  File inputFile; 
  File outputFile;
  String userPassword;
  String ownerPassword;
  // A bit-field containing file permissions.
  int permissions = PDFWriter.ALLOW_PRINTING | PDFWriter.ALLOW_COPY;

  PdfReader reader = new PdfReader(inputFile);
  PdfEncryptor.encrypt(reader, new FileOutputStream(outputFile),
      ENCRYPTION_AES128, userPassword, ownerPassword, 
      permissions);
Run Code Online (Sandbox Code Playgroud)

这是PDFEncryptorPDFWriter的API (用于权限).


Ant*_* O. 7

使用PDFBox(基于Decrypt.java代码):

PDDocument document = null;

try
{
    document = PDDocument.load( infile );

    if( document.isEncrypted() )
    {
        DecryptionMaterial decryptionMaterial = null;
        decryptionMaterial = new StandardDecryptionMaterial(password);
        document.openProtection(decryptionMaterial);
        AccessPermission ap = document.getCurrentAccessPermission();
        if(ap.isOwnerPermission())
        {
            document.setAllSecurityToBeRemoved(true);
            document.save( outfile );
        }
        else
        {
            throw new IOException(
            "Error: You are only allowed to decrypt a document with the owner password." );
        }
    }
    else
    {
        System.err.println( "Error: Document is not encrypted." );
    }
}
finally
{
    if( document != null )
    {
        document.close();
    }
}
Run Code Online (Sandbox Code Playgroud)


Bil*_*ard 5

iText支持加密.