public static void main(String[] args) {
    try {
        ByteArrayOutputStream fopout = new ByteArrayOutputStream();
        FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, fopout);

        Transformer transformer = TransformerFactory.newInstance().newTransformer(
                new StreamSource(new File(args[1])));
        transformer.transform(new StreamSource(new File(args[0])),
                new SAXResult(fop.getDefaultHandler()));

        String userPassword = "pdf";
        PdfReader reader = new PdfReader(new ByteArrayInputStream(fopout.toByteArray()));
        PdfWriter writer = new PdfWriter(args[2], new WriterProperties().setStandardEncryption(
                userPassword.getBytes(),
                null,
                EncryptionConstants.ALLOW_COPY,
                EncryptionConstants.STANDARD_ENCRYPTION_40));
        PdfDocument doc = new PdfDocument(reader, writer);
        doc.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}