/** * Encrypts a PDF document. * * @param args * input_file output_file user_password owner_password * permissions 128|40 [new info string pairs] */ public static void main(String args[]) { System.out.println("PDF document encryptor"); if (args.length <= STRENGTH || args[PERMISSIONS].length() != 8) { usage(); return; } try { int permissions = 0; String p = args[PERMISSIONS]; for (int k = 0; k < p.length(); ++k) { permissions |= (p.charAt(k) == '0' ? 0 : permit[k]); } System.out.println("Reading " + args[INPUT_FILE]); PdfReader reader = new PdfReader(args[INPUT_FILE]); System.out.println("Writing " + args[OUTPUT_FILE]); HashMap moreInfo = new HashMap(); for (int k = MOREINFO; k < args.length - 1; k += 2) moreInfo.put(args[k], args[k + 1]); PdfEncryptor.encrypt(reader, new FileOutputStream(args[OUTPUT_FILE]), args[USER_PASSWORD].getBytes(), args[OWNER_PASSWORD].getBytes(), permissions, args[STRENGTH].equals("128"), moreInfo); System.out.println("Done."); } catch (Exception e) { e.printStackTrace(); } }
/** * Encrypts a PDF document. * * @param args input_file output_file user_password owner_password permissions 128|40 [new info string pairs] */ public static void main (String args[]) { System.out.println("PDF document encryptor"); if (args.length <= STRENGTH || args[PERMISSIONS].length() != 8) { usage(); return; } try { int permissions = 0; String p = args[PERMISSIONS]; for (int k = 0; k < p.length(); ++k) { permissions |= (p.charAt(k) == '0' ? 0 : permit[k]); } System.out.println("Reading " + args[INPUT_FILE]); PdfReader reader = new PdfReader(args[INPUT_FILE]); System.out.println("Writing " + args[OUTPUT_FILE]); HashMap moreInfo = new HashMap(); for (int k = MOREINFO; k < args.length - 1; k += 2) moreInfo.put(args[k], args[k + 1]); PdfEncryptor.encrypt(reader, new FileOutputStream(args[OUTPUT_FILE]), args[USER_PASSWORD].getBytes(), args[OWNER_PASSWORD].getBytes(), permissions, args[STRENGTH].equals("128"), moreInfo); System.out.println("Done."); } catch (Exception e) { e.printStackTrace(); } }
/** * Reads and encrypts an existing PDF file. */ @Test public void main() throws Exception { PdfReader reader = new PdfReader(PdfTestBase.RESOURCES_DIR + "ChapterSection.pdf"); PdfEncryptor.encrypt(reader, PdfTestBase.getOutputStream("encrypted.pdf"), "Hello".getBytes(), "World".getBytes(), PdfWriter.ALLOW_PRINTING | PdfWriter.ALLOW_COPY, false); }
/** * Reads and encrypts an existing PDF file. * * @param args * no arguments needed */ public static void main(String[] args) { System.out.println("Encryptor example"); try { //Can't use filename directly // PdfReader reader = new PdfReader("ChapterSection.pdf"); PdfReader reader = new PdfReader(PdfTestRunner.getActivity().getResources().openRawResource(R.raw.chaptersection)); PdfEncryptor.encrypt(reader, new FileOutputStream(android.os.Environment.getExternalStorageDirectory() + java.io.File.separator + "droidtext" + java.io.File.separator + "encrypted.pdf"), "Hello".getBytes(), "World".getBytes(), PdfWriter.ALLOW_PRINTING | PdfWriter.ALLOW_COPY, false); } catch (Exception e) { e.printStackTrace(); } }
/** * Establece permisos pdf a solo impresion * @param pdfOut Outputstream al pdf de salida con los permisos aplicados * @param pdfIn InputStreams del pdf sobre el que se deben aplicar los permisos */ public static void establecerSoloImpresion(OutputStream pdfOut,InputStream pdfIn) throws Exception{ PdfReader reader = new PdfReader(pdfIn); PdfEncryptor.encrypt(reader, pdfOut, null,null, PdfWriter.AllowCopy | PdfWriter.AllowPrinting, false); }