防止复制使用iText生成的pdf内容 [英] Prevent copying pdf contents generated using iText

查看:801
本文介绍了防止复制使用iText生成的pdf内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个Web应用程序,它生成一个报告并使用iText将其保存为pdf。我们希望阻止用户复制此文件中的内容。我认为这可以通过将pdf内容转换为图像然后将该图像添加到pdf来实现,但似乎iText无法将pdf转换为图像。

We have a web application which generates a report and saves it in pdf using iText. We want to prevent user from copying the contents from this file. I thought this can be achieved by converting pdf contents to image and then add that image to pdf but it seems iText cannot convert pdf to an image.

有没有办法可以用iText做到这一点?

Is there a way I can do this using iText?

推荐答案

正如@YuriyGalanter所指出的,PDF中的限制性权限主要是在PDF加密的上下文中定义的,参见PDF规范的第7.6.3.1节 ISO 32000- 1:2008

As @YuriyGalanter indicated, restrictive permissions in PDFs are primarily defined in the context of encryption in PDFs, cf. section 7.6.3.1 of the PDF specification ISO 32000-1:2008:


如果指定了密码或访问限制,则应对文档进行加密,并提供所需的权限和信息验证密码应存储在加密字典中。仅加密文件附件的文档应使用与用户和所有者密码相同的密码。

If passwords or access restrictions are specified, the document shall be encrypted, and the permissions and information required to validate the passwords shall be stored in the encryption dictionary. Documents in which only file attachments are encrypted shall use the same password as the user and owner password.


  • 打开文档与正确的所有者密码应允许完全(所有者)访问该文档。此无限制访问包括更改文档密码和访问权限的功能。

  • Opening the document with the correct owner password should allow full (owner) access to the document. This unlimited access includes the ability to change the document’s passwords and access permissions.

使用正确的用户密码打开文档(或使用默认密码打开文档)应该允许根据文档加密字典中指定的用户访问权限执行其他操作。

Opening the document with the correct user password (or opening a document with the default password) should allow additional operations to be performed according to the user access permissions specified in the document’s encryption dictionary.

因此,如果使用所有者密码加密PDF但没有用户密码,则任何人都可以打开PDF并受加密期间选择的权限限制。只允许以其所有者密码打开该PDF的人可以无限制地访问PDF。

Thus, if a PDF is encrypted using an owner password but no user password, anyone can open the PDF and is restricted by the permissions selected during encryption. Only someone opening that PDF with its owner password is permitted has unlimited access to the PDF.

显然,这样的方案很容易被破坏并且保护被删除。这取决于PDF处理软件的道德行为。

Obviously such a scheme can easily be broken and ´the protection removed. It depends on the ethical behaviour of PDF processing software.

示例 iText in Action - 第2版第12章的rel =nofollow> EncryptionPdf.java 演示如何使用iText应用加密。

The example EncryptionPdf.java from chapter 12 of iText in Action — 2nd Edition demonstrates how to apply encryption using iText.

方法 createPdf 说明了在创建新PDF时如何应用加密:

The method createPdf illustrates how to apply encryption when creating a new PDF:

public void createPdf(String filename) throws IOException, DocumentException {
    // step 1
    Document document = new Document();
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
    writer.setEncryption(USER, OWNER, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
    writer.createXmpMetadata();
    // step 3
    document.open();
    // step 4
    document.add(new Paragraph("Hello World"));
    // step 5
    document.close();
}

方法 encryptPdf 说明了如何加密现有PDF:

The method encryptPdf illustrates how to encrypt an existing PDF:

public void encryptPdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    stamper.setEncryption(USER, OWNER,
        PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
    stamper.close();
    reader.close();
}

这里 USER OWNER 是上面提到的用户和所有者密码。

Here USER and OWNER are the user and owner passwords mentioned above.

该示例仅设置一个权限, PdfWriter.ALLOW_PRINTING ;还有其他多个,只需检查 PdfWriter的所有 ALLOW _ * 常量。

The sample only sets one permission, PdfWriter.ALLOW_PRINTING; there are multiple other ones, simply inspect all the ALLOW_* constants of PdfWriter.

这篇关于防止复制使用iText生成的pdf内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆