public static void encrypt(PDFDocument doc, OutputStream os, String newUserPassword, String newOwnerPassword, int permissions, int encryption) throws ApplicationException, DocumentException, IOException { byte[] user = newUserPassword==null?null:newUserPassword.getBytes(); byte[] owner = newOwnerPassword==null?null:newOwnerPassword.getBytes(); PdfReader pr = doc.getPdfReader(); List bookmarks = SimpleBookmark.getBookmark(pr); int n = pr.getNumberOfPages(); Document document = new Document(pr.getPageSizeWithRotation(1)); PdfCopy writer = new PdfCopy(document, os); if(encryption!=ENCRYPT_NONE)writer.setEncryption(user, owner, permissions, encryption); document.open(); PdfImportedPage page; for (int i = 1; i <= n; i++) { page = writer.getImportedPage(pr, i); writer.addPage(page); } PRAcroForm form = pr.getAcroForm(); if (form != null)writer.copyAcroForm(pr); if (bookmarks!=null)writer.setOutlines(bookmarks); document.close(); }