PDF encryption requires two passwords: the "user password" to open and view the file with restricted permissions, the "owner password" to access the file with all permission.
This small sample shows how to encrypt a file so that it can be viewed, but not printed.
PDDocument doc = PDDocument.load("filename.pdf");
// Define the length of the encryption key.
// Possible values are 40 or 128 (256 will be available in PDFBox 2.0).
int keyLength = 128;
AccessPermission ap = new AccessPermission();
// Disable printing, everything else is allowed
ap.setCanPrint(false);
// Owner password (to open the file with all permissions) is "12345"
// User password (to open the file but with restricted permissions, is empty here)
StandardProtectionPolicy spp = new StandardProtectionPolicy("12345", "", ap);
spp.setEncryptionKeyLength(keyLength);
spp.setPermissions(ap);
doc.protect(spp);
doc.save("filename-encrypted.pdf");
doc.close();