File: PDFEncrypt.java

package info (click to toggle)
endesive 2.18.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,540 kB
  • sloc: python: 17,934; java: 1,000; xml: 800; sh: 306; javascript: 118; makefile: 8
file content (37 lines) | stat: -rw-r--r-- 1,038 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.io.File;
 
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;

public class PDFEncrypt {

   public static void main(String args[]) throws Exception {
      //Loading an existing document
      File file = new File(args[0]);
      PDDocument document = PDDocument.load(file);

      //Creating access permission object
      AccessPermission ap = new AccessPermission();

      //Creating StandardProtectionPolicy object
      StandardProtectionPolicy spp = new StandardProtectionPolicy(args[2], args[2], ap);

      //Setting the length of the encryption key
      spp.setEncryptionKeyLength(128);

      //Setting the access permissions
      spp.setPermissions(ap);

      //Protecting the document
      document.protect(spp);

      System.out.println("Document encrypted");

      //Saving the document
      document.save(args[1]);
      //Closing the document
      document.close();

   }
}