0

我正在尝试使用 java 代码加密我现有的 pdf 文件,但是此代码将创建新的 pdf 文件,有人可以告诉我如何加密 pdf 文件而不更改其内容吗?

    import java.io.*; 
    import com.itextpdf.text.*;
    import com.itextpdf.text.pdf.*;
    public class PasswordProtectPDF {    
           public static byte[] UserPassword= "UserPassword".getBytes();    
           public static byte[] OwnerPassword = "OwnerPassword".getBytes();
           public static void main(String[] args){
                  try {
                      String filename=new String();
                      filename="C:\\Agam_341238\\E-Books\\test123.pdf";
                      Document Document_For_Protection = new Document();
                      PdfWriter EncryptPDF=PdfWriter.getInstance(Document_For_Protection, new FileOutputStream(filename));
                      EncryptPDF.setEncryption(UserPassword, OwnerPassword,
                ~(PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING), PdfWriter.STANDARD_ENCRYPTION_128);

        EncryptPDF.createXmpMetadata();
        Document_For_Protection.open();
        Document_For_Protection.add(new Paragraph("Some Contents"));
        Document_For_Protection.close();
        System.out.println("Done");
    }
    catch (Exception i)
    {
        System.out.println(i);
    }
}

}

4

2 回答 2

0

您可以为此使用压模。检查此以获取详细信息http://what-when-how.com/itext-5/encrypting-a-pdf-document-itext-5/

于 2013-06-10T08:04:57.843 回答
-1

你需要像这样传递你的输入文件

    File filename; 
    File outputFile;
    String userPassword;
    String ownerPassword;
    PdfReader reader = new PdfReader(filename);
    PdfEncryptor.encrypt(reader, new FileOutputStream(outputFile),
          ENCRYPTION_AES128, userPassword, ownerPassword, 
          permissions);
于 2013-06-10T08:01:04.873 回答