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?

31st May 2016
admin-marketing

Here is my code:

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

In iText 7 all manipulations with PDF are done using PdfDocument instance. Encryption can be done like this:

PdfReader reader = new PdfReader(src);
WriterProperties props = new WriterProperties()
        .setStandardEncryption("Hello".getBytes(), "World".getBytes(), EncryptionConstants.ALLOW_PRINTING,
                EncryptionConstants.ENCRYPTION_AES_128 | EncryptionConstants.DO_NOT_ENCRYPT_METADATA);
PdfWriter writer = new PdfWriter(new FileOutputStream(dest), props);
PdfDocument pdfDoc = new PdfDocument(reader, writer);
pdfDoc.close();

The problem you faced with, was about not closed PdfStamper. Don’t forget to close the PdfDocument instance from now on.

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



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