List of usage examples for org.apache.pdfbox.pdmodel.encryption AccessPermission setCanAssembleDocument
public void setCanAssembleDocument(boolean allowAssembly)
From source file:com.ackpdfbox.app.Encrypt.java
License:Apache License
private void encrypt(String[] args) throws IOException, CertificateException { if (args.length < 1) { usage();/*w ww. j a va2s. 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(); } } } }
From source file:com.mirth.connect.connectors.doc.DocumentDispatcher.java
License:Open Source License
private void encryptPDF(InputStream inputStream, OutputStream outputStream, String password) throws Exception { PDDocument document = null;/*from w ww.java 2 s .co m*/ try { document = PDDocument.load(inputStream); AccessPermission accessPermission = new AccessPermission(); accessPermission.setCanAssembleDocument(false); accessPermission.setCanExtractContent(true); accessPermission.setCanExtractForAccessibility(false); accessPermission.setCanFillInForm(false); accessPermission.setCanModify(false); accessPermission.setCanModifyAnnotations(false); accessPermission.setCanPrint(true); accessPermission.setCanPrintDegraded(true); String ownerPassword = System.currentTimeMillis() + "+" + Runtime.getRuntime().freeMemory() + "+" + (ownerPasswordSeq++); StandardProtectionPolicy policy = new StandardProtectionPolicy(ownerPassword, password, accessPermission); policy.setEncryptionKeyLength(128); document.protect(policy); document.save(outputStream); } catch (Exception e) { throw e; } finally { if (document != null) { document.close(); } } }
From source file:noprint.NoPrint.java
/** * @param args the command line arguments * @throws IOException in case input file is can't be read or output written * @throws org.apache.pdfbox.pdmodel.encryption.BadSecurityHandlerException * @throws org.apache.pdfbox.exceptions.COSVisitorException *///ww w. j a va 2 s .c om public static void main(String[] args) throws IOException, BadSecurityHandlerException, COSVisitorException { String infile = "input.pdf"; String outfile = "output.pdf"; String ownerPass = ""; String userPass = ""; /** * TODO: read up what the actual difference is between * userpassword and ownerpassword. */ int keylength = 40; AccessPermission ap = new AccessPermission(); PDDocument document = null; ap.setCanAssembleDocument(true); ap.setCanExtractContent(true); ap.setCanExtractForAccessibility(true); ap.setCanFillInForm(true); ap.setCanModify(true); ap.setCanModifyAnnotations(true); ap.setCanPrintDegraded(true); ap.setCanPrint(false); // YOU CAN'T PRINT // at least not when your PDFreader adheres to DRM (some don't) // also this is trivial to remove document = PDDocument.load(infile); if (!document.isEncrypted()) { StandardProtectionPolicy spp; spp = new StandardProtectionPolicy(ownerPass, userPass, ap); spp.setEncryptionKeyLength(keylength); document.protect(spp); document.save(outfile); } if (document != null) { document.close(); } }
From source file:org.nuxeo.pdf.PDFEncryption.java
License:Apache License
/** * Encrypts the PDF with readonly permission. * <p>/* w ww . ja va2s . com*/ * WARNING: If you are familiar with PDFBox @ AccessPermission}, notice our encryptReadOnly() method is not the same * as {@link AccessPermission#AccessPermission#setReadOnly}. The later just makes sure the code cannot call other * setter later on. * <p> * <code>encryptReadOnly</code> sets the following permissions on the document: * <ul> * <li>Can print: True</li> * <li>Can Modify: False</li> * <li>Can Extract Content: True</li> * <li>Can Add/Modify annotations: False</li> * <li>Can Fill Forms: False</li> * <li>Can Extract Info for Accessibility: True</li> * <li>Can Assemble: False</li> * <li>Can print degraded: True</li> * </ul> * <p> * <b>IMPORTANT * </p> * : It is required that the following setters are called <i>before</i> * <ul> * <li>{@link setOriginalOwnerPwd}: Only if the original pdf already is encrypted. This password allows to open it * for modification</li> * <li>{@link setKeyLength}: To set the length of the key</li> * <li>{@link setOwnerPwd}: The password for the owner. If not called, <code>originalOwnerPwd</code> is used instead * </li> * <li>{@link setUserPwd}: The password for the user.</li> * </ul> * * @return a copy of the blob with the readonly permissions set. * @since 8.1 */ public Blob encryptReadOnly() { AccessPermission ap = new AccessPermission(); ap.setCanPrint(true); ap.setCanModify(false); ap.setCanExtractContent(true); ap.setCanModifyAnnotations(false); ap.setCanFillInForm(false); ap.setCanExtractForAccessibility(true); ap.setCanAssembleDocument(false); ap.setCanPrintDegraded(true); return encrypt(ap); }