java设置pdf不可编辑,禁止编辑但允许在Java iText / PDF中进行页面提取

I'm using iText to generate PDF files. I want to disallow editing of the PDF, but allow the reader to extract pages. Here's my code to set encryption:

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

The third parameter specifies permissions. I'm using 0xffffffff instead of the individual iText flags ALLOW_PRINTING etc. This will ask iText to enable everything. But this is what I get in the PDF file:

B127m.png

I should think I should be allowed to enable extraction but disable editing, but am not certain. Here are the permissions bits per Adobe:

ZXuTM.png

SwiR9.png

(From here, but be warned it's 30 meg)

So turn off bits 6 and 11 but leave on the others (especially bits 5 and 10), and that would turn off editing but allow extraction. In any case, by specifying 0xffffffff I would think that everything would be allowed; but instead everything except extraction is allowed.

I've skimmed the iText source code for setting permissions and don't see anything that would 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 is doing an OR and so wouldn't remove any permissions; the second line is setting the two right-most bites to 0, per the PDF specification.

I'm wondering if it's an iText thing, a PDF thing or if I'm doing something else wrong.

Thanks

解决方案

A similar issue already has been raised here.

Using encryption actually is counter-productive as it can only be used to remove permissions, not to add them.

According to this, it might be helpful to completely unlock the PDF first:

PdfReader reader = new PdfReader(file.toURI().toURL());

PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(

file.getAbsolutePath().replace(".pdf", "_UNLOCKED.pdf")));

stamper.close();

reader.close();

Afterwards you can grab the output and start from scratch (mess around with the permission bits). Hope this helps.

EDIT: If you don't have access to the password, the iText sources can be modified. Simply comment out if (!reader.isOpenedWithFullPermissions()) throw ... (line 121 and 122 in version 5.5.0) in com.itextpdf.text.pdf.PdfStamperImp.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值