1

下面是我的代码。我的目标是创建一个 PDF,最终用户可以在其中做任何他们想做的事情,除了复制文本(选择文本并复制到记事本)。谁能解释第 18 行应该包含什么代码?我允许打印但不允许 ALLOW_COPY)

我的印象是,下面的代码足以限制用户这样做,但“事实上”他们能够复制选定的文本并将内容粘贴到记事本中。

非常感谢!

package com.itext;

import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.IOException;
import com.itextpdf.text.DocumentException;

public class ProtectMePdf 
{ 
public static void main(String[] args) throws IOException, DocumentException 
{
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("/Users/adhg/protectMe.pdf"));

    //LINE 18: what's wrong with this line? - if you run the code you will be able to copy the selected text. 
    writer.setEncryption(null, null, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);


    writer.createXmpMetadata();
    document.open();
    document.add(new Paragraph("Protect me! if you can do copy-paste of this message to a notepad = NO GOOD :-(")); 
    document.close();
}
}
4

2 回答 2

5

接受的答案是错误的。事实上,您可以禁用复制并允许打印。这很容易,您只需取消权限:

writer.setEncryption(null, null,  ~(PdfWriter.ALLOW_COPY), PdfWriter.STANDARD_ENCRYPTION_128);
于 2015-04-20T03:37:56.080 回答
2

我不是 iText 和 PDF 规范的专家,但我认为您不能同时允许打印和禁用复制和粘贴。您可以在此处找到更多信息

另一种选择是将图像放入 PDF 中,但 OCR 有点先进,可以避免这种情况。

于 2012-06-18T08:39:26.937 回答