2012-06-16 55 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);