2

How to set password for an existing PDF?

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
Sreekanth P
  • 41
  • 1
  • 5
  • Please always provide a [mcve](http://stackoverflow.com/help/mcve) of your problem. In addition, describe what you already tried. Which language are you using? – Andre Hofmeister Dec 02 '14 at 12:26
  • possible duplicate of [Password Protection of PDF Files](http://stackoverflow.com/questions/2032380/password-protection-of-pdf-files) –  Dec 02 '14 at 12:33

2 Answers2

5

Did you look at the EncryptionPdf example in chapter 12 of my book?

It's as simple as this:

public void encryptPdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    stamper.setEncryption(USER, OWNER,
        PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
    stamper.close();
    reader.close();
}

Note that USER and OWNER are of type byte[]. You have different options for the permissions (look for constants starting with ALLOW_) and you can choose from different encryption algorithms.

As for the parameters: src is the path to the existing PDF. dest is the path of the encrypted PDF. It should be obvious that you can not write to a file while you are reading it. That is explained here: How to update a PDF without creating a new PDF?

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • 1
    how do u encrypt a pdf if only you have the byte array as input - and need another byte array as output, it is posible? – Alberto Acuña May 24 '19 at 10:40
  • @AlbertoAcuña, giving an answer to your question and referring to Bruno's answer too. – Deva Nov 26 '19 at 12:34
  • throws java.lang.NoClassDefFoundError: Failed resolution of: Lorg/spongycastle/crypto/engines/AESFastEngine; – EAS Jan 28 '22 at 09:46
  • Strangely, Chrome seems to display the PDFs created with this code without asking for the password. Firefox and the Preview-App in MacOS ask for it. – yglodt Apr 11 '22 at 21:50
  • My previous comment is probably related to the fact that I use iText 2.1.7. When using this call the encryption worked stamper.setEncryption(userPassword.getBytes(), ownerPassword.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128); – yglodt Apr 12 '22 at 21:43
0

Answer for @Alberto Question: How do u encrypt a pdf if only you have the byte array as input - and need another byte array as output, Used the previous answer.

I have a method called addPassword(byte[] templateByte) which accepts byte array of PDF file as a parameter and returns the encrypted byte array as a response.

public byte[] addPassword(byte[] templateByte)
    {

        String USER_PASS = "Hello123";
        String OWNER_PASS = "Deva123";
        PdfReader pdfReader = null;

        ByteArrayOutputStream byteArrayOutputStream = ByteArrayOutputStream(templateByte.length);

//removed manual write to output stream

        try
        {

            pdfReader = new PdfReader(templateByte);
            PdfStamper stamper = new PdfStamper(pdfReader, byteArrayOutputStream);
            stamper.setEncryption(USER_PASS.getBytes(), OWNER_PASS.getBytes(),
                    PdfWriter.ALLOW_PRINTING,
                    PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
            stamper.close();
            pdfReader.close();

            return byteArrayOutputStream.toByteArray();

        }
        catch (Exception e)
        {

            e.printStackTrace();
        }

        return byteArrayOutputStream.toByteArray();
    }
Community
  • 1
  • 1
Deva
  • 1,851
  • 21
  • 22