Disallow editing, but allow page extraction in Java iText / PDF

I use iText to create PDF files. I want to disable PDF editing, but allow the reader to extract pages. Here is my code to set the encryption:

writer.setEncryption(null, null, 0xffffffff, PdfWriter.STANDARD_ENCRYPTION_128);

The third parameter indicates permissions. I use 0xffffffff instead of individual iText ALLOW_PRINTING flags, etc. This will ask iText to include everything. But this is what I get in the PDF file:

enter image description here

I should think that I am allowed to enable extraction, but disable editing, but I'm not sure. Below are the permission bits for Adobe: enter image description hereenter image description here

(From here , but be warned about it 30 meg)

So turn off bits 6 and 11, but leave it on the rest (especially bits 5 and 10), and this will disable editing, but allow extraction. In any case, specifying 0xffffffff, I would have thought that everything would be allowed; but instead, only an exception exception is allowed.

I took the source code of iText for setting permissions and see nothing that could cause this. Here is the relevant code from PdfEncryption.setupAllKeys:

 permissions |= (revision == STANDARD_ENCRYPTION_128 || revision == AES_128 || revision == AES_256) ? 0xfffff0c0 : 0xffffffc0; permissions &= 0xfffffffc; 

The first line executes OR and therefore will not remove any permissions; the second line sets the two extreme bites to 0 according to the PDF specification.

I am wondering if this is an iText thing, a thing in PDF format, or if I am doing something else wrong.

thanks

+6
source share
1 answer

A similar problem has already been raised here .

Using encryption is actually counterproductive because it can only be used to remove permissions, not to add them.

Accordingly, it may be useful to fully unlock the PDF:

 PdfReader reader = new PdfReader(file.toURI().toURL()); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream( file.getAbsolutePath().replace(".pdf", "_UNLOCKED.pdf"))); stamper.close(); reader.close(); 

Then you can grab the output and start from scratch (mess with resolution bits). Hope this helps.

EDIT: If you do not have access to a password, you can change iText sources . Just comment if (!reader.isOpenedWithFullPermissions()) throw ... (lines 121 and 122 in version 5.5.0) in com.itextpdf.text.pdf.PdfStamperImp .

+1
source

All Articles