Example usage for org.apache.pdfbox.pdmodel.encryption PublicKeyRecipient PublicKeyRecipient

List of usage examples for org.apache.pdfbox.pdmodel.encryption PublicKeyRecipient PublicKeyRecipient

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.encryption PublicKeyRecipient PublicKeyRecipient.

Prototype

PublicKeyRecipient

Source Link

Usage

From source file:com.ackpdfbox.app.Encrypt.java

License:Apache License

private void encrypt(String[] args) throws IOException, CertificateException {
    if (args.length < 1) {
        usage();//from w w w . j a  v a 2 s. c  o m
    } else {
        AccessPermission ap = new AccessPermission();

        String infile = null;
        String outfile = null;
        String certFile = null;
        String userPassword = "";
        String ownerPassword = "";

        int keyLength = 40;

        PDDocument document = null;

        try {
            for (int i = 0; i < args.length; i++) {
                String key = args[i];
                if (key.equals("-O")) {
                    ownerPassword = args[++i];
                } else if (key.equals("-U")) {
                    userPassword = args[++i];
                } else if (key.equals("-canAssemble")) {
                    ap.setCanAssembleDocument(args[++i].equalsIgnoreCase("true"));
                } else if (key.equals("-canExtractContent")) {
                    ap.setCanExtractContent(args[++i].equalsIgnoreCase("true"));
                } else if (key.equals("-canExtractForAccessibility")) {
                    ap.setCanExtractForAccessibility(args[++i].equalsIgnoreCase("true"));
                } else if (key.equals("-canFillInForm")) {
                    ap.setCanFillInForm(args[++i].equalsIgnoreCase("true"));
                } else if (key.equals("-canModify")) {
                    ap.setCanModify(args[++i].equalsIgnoreCase("true"));
                } else if (key.equals("-canModifyAnnotations")) {
                    ap.setCanModifyAnnotations(args[++i].equalsIgnoreCase("true"));
                } else if (key.equals("-canPrint")) {
                    ap.setCanPrint(args[++i].equalsIgnoreCase("true"));
                } else if (key.equals("-canPrintDegraded")) {
                    ap.setCanPrintDegraded(args[++i].equalsIgnoreCase("true"));
                } else if (key.equals("-certFile")) {
                    certFile = args[++i];
                } else if (key.equals("-keyLength")) {
                    try {
                        keyLength = Integer.parseInt(args[++i]);
                    } catch (NumberFormatException e) {
                        throw new NumberFormatException(
                                "Error: -keyLength is not an integer '" + args[i] + "'");
                    }
                } else if (infile == null) {
                    infile = key;
                } else if (outfile == null) {
                    outfile = key;
                } else {
                    usage();
                }
            }
            if (infile == null) {
                usage();
            }
            if (outfile == null) {
                outfile = infile;
            }
            document = PDDocument.load(new File(infile));

            if (!document.isEncrypted()) {
                if (certFile != null) {
                    PublicKeyProtectionPolicy ppp = new PublicKeyProtectionPolicy();
                    PublicKeyRecipient recip = new PublicKeyRecipient();
                    recip.setPermission(ap);

                    CertificateFactory cf = CertificateFactory.getInstance("X.509");

                    InputStream inStream = null;
                    try {
                        inStream = new FileInputStream(certFile);
                        X509Certificate certificate = (X509Certificate) cf.generateCertificate(inStream);
                        recip.setX509(certificate);
                    } finally {
                        if (inStream != null) {
                            inStream.close();
                        }
                    }

                    ppp.addRecipient(recip);

                    ppp.setEncryptionKeyLength(keyLength);

                    document.protect(ppp);
                } else {
                    StandardProtectionPolicy spp = new StandardProtectionPolicy(ownerPassword, userPassword,
                            ap);
                    spp.setEncryptionKeyLength(keyLength);
                    document.protect(spp);
                }
                document.save(outfile);
            } else {
                System.err.println("Error: Document is already encrypted.");
            }
        } finally {
            if (document != null) {
                document.close();
            }
        }
    }
}