Password Protection of PDF Files

元气小坏坏 提交于 2019-12-06 12:10:18

问题


We have a requirement to protect PDF files using a password. Are there any Java-based, open source tools which will help us in this regard?


回答1:


I would recommend using the iText java PDF library.

Inside iText, there is a class called PdfEncrypter which should let you password protect a PDF file.




回答2:


You can easily make the Password protected pdf file in java......to do so you will require two addtional jar/lib bctsp-jdk16-1.46.jar and bcprov-jdk16-1.46.jar along with the itextpdf-5.2.1.jar.
Download all jars from here Download Jars

Also below is the snippet of the code

private static String USER_PASSWORD = "password";
private static String OWNER_PASSWORD = "naveen";
public static void main(String[] args) throws IOException {

    Document document = new Document();
      try
      {

         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("E:\\HelloWorld.pdf"));
         writer.setEncryption(USER_PASSWORD.getBytes(),OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING,PdfWriter.ENCRYPTION_AES_128);
         document.open();
         document.add(new Paragraph("This is Password Protected PDF document."));
         document.close();
         writer.close();
      } catch (DocumentException e)
      {
         e.printStackTrace();
      } catch (FileNotFoundException e)
      {
         e.printStackTrace();
      }
}



回答3:


you can do it with iText PDF for java:

some examples:

http://1t3xt.info/examples/browse/?page=example&id=42




回答4:


FOP library also allows encryption:

http://xmlgraphics.apache.org/fop/0.94/pdfencryption.html



来源:https://stackoverflow.com/questions/2032380/password-protection-of-pdf-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!