Encrypting / Decrypting PDFs

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

31st May 2016
admin-marketing

Switch code for this example

DecryptPdf.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/27867868/how-can-i-decrypt-a-pdf-document-with-the-owner-password
  11.  */
  12. package com.itextpdf.samples.sandbox.security;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfReader;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.kernel.pdf.ReaderProperties;
  18. import com.itextpdf.samples.GenericTest;
  19. import com.itextpdf.test.annotations.type.SampleTest;
  20.  
  21. import org.junit.experimental.categories.Category;
  22.  
  23. import java.io.File;
  24.  
  25. @Category(SampleTest.class)
  26. public class DecryptPdf extends GenericTest {
  27.     public static final String DEST = "./target/test/resources/sandbox/security/decrypt_pdf.pdf";
  28.     public static final String SRC = "./src/test/resources/pdfs/hello_encrypted.pdf";
  29.  
  30.     public static void main(String[] args) throws Exception {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new DecryptPdf().manipulatePdf(DEST);
  34.     }
  35.  
  36.     @Override
  37.     protected void manipulatePdf(String dest) throws Exception {
  38.         PdfReader reader = new PdfReader(SRC, new ReaderProperties().setPassword("World".getBytes()));
  39.         PdfDocument pdfDoc = new PdfDocument(reader, new PdfWriter(DEST));
  40.         System.out.println(new String(reader.computeUserPassword()));
  41.         pdfDoc.close();
  42.     }
  43. }
DecryptPdf2.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/27867868/how-can-i-decrypt-a-pdf-document-with-the-owner-password
  11.  */
  12. package com.itextpdf.samples.sandbox.security;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfReader;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.samples.GenericTest;
  18. import com.itextpdf.test.annotations.type.SampleTest;
  19.  
  20. import org.junit.experimental.categories.Category;
  21.  
  22. import java.io.File;
  23.  
  24. @Category(SampleTest.class)
  25. public class DecryptPdf2 extends GenericTest {
  26.     public static final String DEST = "./target/test/resources/sandbox/security/decrypt_pdf2.pdf";
  27.     public static final String SRC = "./src/test/resources/pdfs/encrypt_pdf_without_user_password.pdf";
  28.  
  29.     public static void main(String[] args) throws Exception {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new DecryptPdf2().manipulatePdf(DEST);
  33.     }
  34.  
  35.     @Override
  36.     protected void manipulatePdf(String dest) throws Exception {
  37.         PdfReader reader = new PdfReader(SRC);
  38.         reader.setUnethicalReading(true);
  39.         PdfDocument pdfDoc = new PdfDocument(reader, new PdfWriter(DEST));
  40.         pdfDoc.close();
  41.     }
  42. }
DecryptPdf3.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/27867868/how-can-i-decrypt-a-pdf-document-with-the-owner-password
  11.  */
  12. package com.itextpdf.samples.sandbox.security;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfReader;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.samples.GenericTest;
  18. import com.itextpdf.test.annotations.type.SampleTest;
  19.  
  20. import org.junit.experimental.categories.Category;
  21.  
  22. import java.io.File;
  23. import java.io.IOException;
  24.  
  25. @Category(SampleTest.class)
  26. public class DecryptPdf3 extends GenericTest {
  27.     public static final String DEST = "./target/test/resources/sandbox/security/decrypt_pdf3.pdf";
  28.     public static final String SRC = "./src/test/resources/pdfs/encrypt_pdf_without_user_password.pdf";
  29.  
  30.     public static void main(String[] args) throws Exception {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new DecryptPdf3().manipulatePdf(DEST);
  34.     }
  35.  
  36.     @Override
  37.     protected void manipulatePdf(String dest) throws Exception {
  38.         MyReader reader = new MyReader(SRC);
  39.         reader.setUnethicalReading(true);
  40.         reader.decryptOnPurpose();
  41.         PdfDocument pdfDoc = new PdfDocument(reader, new PdfWriter(DEST));
  42.         pdfDoc.close();
  43.         reader.close();
  44.     }
  45.  
  46.  
  47.     class MyReader extends PdfReader {
  48.         public MyReader(String filename) throws IOException {
  49.             super(filename);
  50.         }
  51.  
  52.         public void decryptOnPurpose() {
  53.             encrypted = false;
  54.         }
  55.     }
  56. }
EncryptPdf.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/27867868/how-can-i-decrypt-a-pdf-document-with-the-owner-password
  11.  */
  12. package com.itextpdf.samples.sandbox.security;
  13.  
  14. import com.itextpdf.kernel.pdf.*;
  15. import com.itextpdf.kernel.utils.CompareTool;
  16. import com.itextpdf.samples.GenericTest;
  17. import com.itextpdf.test.annotations.type.SampleTest;
  18.  
  19. import org.junit.Assert;
  20. import org.junit.experimental.categories.Category;
  21.  
  22. import java.io.File;
  23.  
  24. @Category(SampleTest.class)
  25. public class EncryptPdf extends GenericTest {
  26.     public static final String DEST = "./target/test/resources/sandbox/security/encrypt_pdf.pdf";
  27.     public static final String SRC = "./src/test/resources/pdfs/hello_encrypted.pdf";
  28.  
  29.     public static void main(String[] args) throws Exception {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new EncryptPdf().manipulatePdf(DEST);
  33.     }
  34.  
  35.     @Override
  36.     protected void manipulatePdf(String dest) throws Exception {
  37.         PdfReader reader = new PdfReader(SRC, new ReaderProperties().setPassword("World".getBytes()));
  38.         PdfWriter writer = new PdfWriter(DEST, new WriterProperties()
  39.                 .setStandardEncryption("Hello".getBytes(), "World".getBytes(), EncryptionConstants.ALLOW_PRINTING,
  40.                         EncryptionConstants.ENCRYPTION_AES_128 | EncryptionConstants.DO_NOT_ENCRYPT_METADATA));
  41.         PdfDocument pdfDoc = new PdfDocument(reader, writer);
  42.         pdfDoc.close();
  43.     }
  44.  
  45.     // Override the comparison method in order to pass owner passwords
  46.     @Override
  47.     protected void comparePdf(String dest, String cmp) throws Exception {
  48.         if (cmp == null || cmp.length() == 0) return;
  49.         CompareTool compareTool = new CompareTool();
  50.         compareTool.enableEncryptionCompare();
  51.         String outPath = new File(dest).getParent();
  52.         new File(outPath).mkdirs();
  53.         if (compareXml) {
  54.             if (!compareTool.compareXmls(dest, cmp)) {
  55.                 addError("The XML structures are different.");
  56.             }
  57.         } else {
  58.             if (compareRenders) {
  59.                 addError(compareTool.compareVisually(dest, cmp, outPath, differenceImagePrefix));
  60.                 addError(compareTool.compareLinkAnnotations(dest, cmp));
  61.             } else {
  62.                 addError(compareTool.compareByContent(dest, cmp, outPath, differenceImagePrefix, "World".getBytes(), "World".getBytes()));
  63.             }
  64.             addError(compareTool.compareDocumentInfo(dest, cmp, "World".getBytes(), "World".getBytes()));
  65.         }
  66.  
  67.         if (errorMessage != null) Assert.fail(errorMessage);
  68.     }
  69. }
EncryptPdfWithoutUserPassword.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/27867868/how-can-i-decrypt-a-pdf-document-with-the-owner-password
  11.  */
  12. package com.itextpdf.samples.sandbox.security;
  13.  
  14. import com.itextpdf.kernel.pdf.*;
  15. import com.itextpdf.samples.GenericTest;
  16. import com.itextpdf.test.annotations.type.SampleTest;
  17.  
  18. import org.junit.experimental.categories.Category;
  19.  
  20. import java.io.File;
  21.  
  22. @Category(SampleTest.class)
  23. public class EncryptPdfWithoutUserPassword extends GenericTest {
  24.     public static final String DEST = "./target/test/resources/sandbox/security/encrypt_pdf_without_user_password.pdf";
  25.     public static final String SRC = "./src/test/resources/pdfs/hello_encrypted.pdf";
  26.  
  27.     public static void main(String[] args) throws Exception {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new EncryptPdfWithoutUserPassword().manipulatePdf(DEST);
  31.     }
  32.  
  33.     @Override
  34.     protected void manipulatePdf(String dest) throws Exception {
  35.         PdfReader reader = new PdfReader(SRC, new ReaderProperties().setPassword("World".getBytes()));
  36.         PdfWriter writer = new PdfWriter(DEST,
  37.                 new WriterProperties().setStandardEncryption(null, "World".getBytes(), EncryptionConstants.ALLOW_PRINTING,
  38.                         EncryptionConstants.ENCRYPTION_AES_128 | EncryptionConstants.DO_NOT_ENCRYPT_METADATA));
  39.         PdfDocument pdfDoc = new PdfDocument(reader, writer);
  40.         pdfDoc.close();
  41.     }
  42. }
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