1 50 package com.lowagie.tools; 51 52 import java.io.FileOutputStream ; 53 import java.util.HashMap ; 54 55 import com.lowagie.text.pdf.PdfEncryptor; 56 import com.lowagie.text.pdf.PdfReader; 57 import com.lowagie.text.pdf.PdfWriter; 58 59 63 public class encrypt_pdf { 64 65 private final static int INPUT_FILE = 0; 66 private final static int OUTPUT_FILE = 1; 67 private final static int USER_PASSWORD = 2; 68 private final static int OWNER_PASSWORD = 3; 69 private final static int PERMISSIONS = 4; 70 private final static int STRENGTH = 5; 71 private final static int MOREINFO = 6; 72 private final static int permit[] = { 73 PdfWriter.AllowPrinting, 74 PdfWriter.AllowModifyContents, 75 PdfWriter.AllowCopy, 76 PdfWriter.AllowModifyAnnotations, 77 PdfWriter.AllowFillIn, 78 PdfWriter.AllowScreenReaders, 79 PdfWriter.AllowAssembly, 80 PdfWriter.AllowDegradedPrinting}; 81 82 private static void usage() { 83 System.out.println("usage: input_file output_file user_password owner_password permissions 128|40 [new info string pairs]"); 84 System.out.println("permissions is 8 digit long 0 or 1. Each digit has a particular security function:"); 85 System.out.println(); 86 System.out.println("AllowPrinting"); 87 System.out.println("AllowModifyContents"); 88 System.out.println("AllowCopy"); 89 System.out.println("AllowModifyAnnotations"); 90 System.out.println("AllowFillIn (128 bit only)"); 91 System.out.println("AllowScreenReaders (128 bit only)"); 92 System.out.println("AllowAssembly (128 bit only)"); 93 System.out.println("AllowDegradedPrinting (128 bit only)"); 94 System.out.println("Example permissions to copy and print would be: 10100000"); 95 } 96 97 102 public static void main (String args[]) { 103 System.out.println("PDF document encryptor"); 104 if (args.length <= STRENGTH || args[PERMISSIONS].length() != 8) { 105 usage(); 106 return; 107 } 108 try { 109 int permissions = 0; 110 String p = args[PERMISSIONS]; 111 for (int k = 0; k < p.length(); ++k) { 112 permissions |= (p.charAt(k) == '0' ? 0 : permit[k]); 113 } 114 System.out.println("Reading " + args[INPUT_FILE]); 115 PdfReader reader = new PdfReader(args[INPUT_FILE]); 116 System.out.println("Writing " + args[OUTPUT_FILE]); 117 HashMap moreInfo = new HashMap (); 118 for (int k = MOREINFO; k < args.length - 1; k += 2) 119 moreInfo.put(args[k], args[k + 1]); 120 PdfEncryptor.encrypt(reader, new FileOutputStream (args[OUTPUT_FILE]), 121 args[USER_PASSWORD].getBytes(), args[OWNER_PASSWORD].getBytes(), permissions, args[STRENGTH].equals("128"), moreInfo); 122 System.out.println("Done."); 123 } 124 catch (Exception e) { 125 e.printStackTrace(); 126 } 127 } 128 } | Popular Tags |