Why does PdfStamper create a file with 0 bytes?

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); Pdf

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 created 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.


Share this article

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
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