Encrypting / Decrypting PDFs

This is a code example of iText PDF, discover more.

6th November 2015
admin-marketing

Switch code for this example

EncryptPdf.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/27867868/how-can-i-decrypt-a-pdf-document-with-the-owner-password
  4.  */
  5. package sandbox.security;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.PdfReader;
  9. import com.itextpdf.text.pdf.PdfStamper;
  10. import com.itextpdf.text.pdf.PdfWriter;
  11.  
  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15.  
  16. public class EncryptPdf {
  17.  
  18.     public static final String SRC = "resources/pdfs/hello.pdf";
  19.     public static final String DEST = "results/security/hello_encrypted.pdf";
  20.    
  21.     public static void main(String[] args) throws IOException, DocumentException {
  22.         File file = new File(DEST);
  23.         file.getParentFile().mkdirs();
  24.         new EncryptPdf().manipulatePdf(SRC, DEST);
  25.     }
  26.    
  27.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  28.         PdfReader reader = new PdfReader(src);
  29.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  30.         stamper.setEncryption("Hello".getBytes(), "World".getBytes(),
  31.             PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
  32.         stamper.close();
  33.         reader.close();
  34.     }
  35. }
DecryptPdf.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/27867868/how-can-i-decrypt-a-pdf-document-with-the-owner-password
  4.  */
  5. package sandbox.security;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.PdfReader;
  9. import com.itextpdf.text.pdf.PdfStamper;
  10.  
  11. import java.io.File;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14.  
  15. public class DecryptPdf {
  16.  
  17.     public static final String SRC = "resources/pdfs/hello_encrypted.pdf";
  18.     public static final String DEST = "results/security/hello.pdf";
  19.    
  20.     public static void main(String[] args) throws IOException, DocumentException {
  21.         File file = new File(DEST);
  22.         file.getParentFile().mkdirs();
  23.         new DecryptPdf().manipulatePdf(SRC, DEST);
  24.     }
  25.    
  26.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  27.         PdfReader reader = new PdfReader(src, "World".getBytes());
  28.         System.out.println(new String(reader.computeUserPassword()));
  29.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  30.         stamper.close();
  31.         reader.close();
  32.     }
  33. }
EncryptPdfWithoutUserPassword.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/27867868/how-can-i-decrypt-a-pdf-document-with-the-owner-password
  4.  */
  5. package sandbox.security;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.PdfReader;
  9. import com.itextpdf.text.pdf.PdfStamper;
  10. import com.itextpdf.text.pdf.PdfWriter;
  11.  
  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15.  
  16. public class EncryptPdfWithoutUserPassword {
  17.  
  18.     public static final String SRC = "resources/pdfs/hello.pdf";
  19.     public static final String DEST = "results/security/hello_encrypted2.pdf";
  20.    
  21.     public static void main(String[] args) throws IOException, DocumentException {
  22.         File file = new File(DEST);
  23.         file.getParentFile().mkdirs();
  24.         new EncryptPdfWithoutUserPassword().manipulatePdf(SRC, DEST);
  25.     }
  26.    
  27.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  28.         PdfReader reader = new PdfReader(src);
  29.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  30.         stamper.setEncryption(null, "World".getBytes(),
  31.             PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
  32.         stamper.close();
  33.         reader.close();
  34.     }
  35. }
DecryptPdf2.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/27867868/how-can-i-decrypt-a-pdf-document-with-the-owner-password
  4.  */
  5. package sandbox.security;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.PdfReader;
  9. import com.itextpdf.text.pdf.PdfStamper;
  10.  
  11. import java.io.File;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14.  
  15. public class DecryptPdf2 {
  16.  
  17.     public static final String SRC = "resources/pdfs/hello_encrypted2.pdf";
  18.     public static final String DEST = "results/security/hello2.pdf";
  19.    
  20.     public static void main(String[] args) throws IOException, DocumentException {
  21.         File file = new File(DEST);
  22.         file.getParentFile().mkdirs();
  23.         new DecryptPdf2().manipulatePdf(SRC, DEST);
  24.     }
  25.    
  26.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  27.         PdfReader.unethicalreading = true;
  28.         PdfReader reader = new PdfReader(src);
  29.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  30.         stamper.close();
  31.         reader.close();
  32.     }
  33. }
DecryptPdf3.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/27867868/how-can-i-decrypt-a-pdf-document-with-the-owner-password
  4.  */
  5. package sandbox.security;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.PdfReader;
  9. import com.itextpdf.text.pdf.PdfStamper;
  10.  
  11. import java.io.File;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14.  
  15. public class DecryptPdf3 {
  16.  
  17.     class MyReader extends PdfReader {
  18.         public MyReader(String filename) throws IOException {
  19.             super(filename);
  20.         }
  21.         public void decryptOnPurpose() {
  22.             encrypted = false;
  23.         }
  24.     }
  25.    
  26.     public static final String SRC = "resources/pdfs/hello_encrypted2.pdf";
  27.     public static final String DEST = "results/security/hello3.pdf";
  28.    
  29.     public static void main(String[] args) throws IOException, DocumentException {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new DecryptPdf3().manipulatePdf(SRC, DEST);
  33.     }
  34.    
  35.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  36.         MyReader.unethicalreading = true;
  37.         MyReader reader = new MyReader(src);
  38.         reader.decryptOnPurpose();
  39.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  40.         stamper.close();
  41.         reader.close();
  42.     }
  43. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now