我想这个代码加密我的PDF格式,使用户无法从PDF复制内容(只是用于测试,我知道有一些为OCR'ing:P)加密PDF文件使用的iText不允许的内容复印和打印
import java.io.FileOutputStream;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
public class EncryptPDF {
private static final String INPUT_FILENAME = "/tmp/test.pdf";
private static final String OUTPUT_FILENAME = "/tmp/test_encrypted.pdf";
private static final String USER_PASSWORD = "";
private static final String OWNER_PASSWORD = "foobar";
public static void main(String[] args) {
PdfReader reader = null;
FileOutputStream out = null;
PdfStamper stamper = null;
try {
// Define input
reader = new PdfReader(INPUT_FILENAME);
// Define output
out = new FileOutputStream(OUTPUT_FILENAME);
// Encrypt document
stamper = new PdfStamper(reader, out);
stamper.setEncryption(USER_PASSWORD.getBytes(), OWNER_PASSWORD.getBytes(), ~(PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING), PdfWriter.STANDARD_ENCRYPTION_128);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (stamper != null) {
try {
stamper.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
}
...但是当我打开PDF时,我仍然可以从中选择内容。我正在使用iText 5.0.2。
任何想法对我做错了什么?
另请提供您生成的PDF。 – mkl 2013-02-09 20:04:20
只需提供您想要允许的权限,而不是否定OR-ed在一起的权限即可。你为什么在那里使用否定? – 2013-02-09 21:28:22
@mkl:这是你可以使用的一个:http://www.selab.isti.cnr.it/ws-mate/example.pdf – 2013-02-10 18:18:02