2012-06-16 54 views
1

以下是我的代码。我的目标是创建一个PDF,其中最终用户可以做任何他们想要的除了复制文本(选择文本和复制到记事本)。任何人都可以解释第18行应该出现什么代码?我允许打印但不允许ALLOW_COPY)试图保护PDF(iText)试图禁止用户在pdf的内容上复制

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

非常感谢!

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(); 
} 
} 
+0

OK,我只是消除了印刷(含ALLOW_DEGRADED_PRINTING替换)和它的伎俩 – adhg

+0

很高兴听到,GL HF。 – rlegendi

+0

谢谢rlegendi。你可以发布你的答案,我会给你信用!再次感谢! – adhg

回答

2

我不iText的和PDF规范的专家,但我认为你不能让打印和复印禁止粘贴&在同一时间。你可以找到additional info here

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

3

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

writer.setEncryption(null, null, ~(PdfWriter.ALLOW_COPY), PdfWriter.STANDARD_ENCRYPTION_128);