Legacy notice!

iText 5 is the previous major version of iText's leading PDF SDK. iText 5 has been EOL, and is no longer developed. Switch your project to iText 7, integrating the latest developments.
Check related iText 7 content!

When attempting to encrypt a pdf, I get a zero size file as a result. Any ideas what is causing this?


File f = new File("C://secure_abc.pdf");
FileOutputStream out = new FileOutputStream(f);
PdfReader reader = new PdfReader("C://abc.pdf");
System.out.println("reader.getFileLength(): "+reader.getFileLength());
PdfStamper stamp = new PdfStamper(reader, out);
stamp.setEncryption(null, null,
    PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);

Posted on StackOverflow on Jul 8, 2014 by integratedsolns

Please add the following line at the very end:

stamp.close();

You create a zero-length file when you do this:

FileOutputStream out = new FileOutputStream(f);

But no bytes are written to that output stream up until you close the PdfStamper instance.

Click this link if you want to see how to answer this question in iText 7.