我尝试使用此代码来加密我的 PDF,因此用户无法从 PDF 中复制内容(仅用于测试,我知道有些东西是 OCR'ing :p)
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。
关于我做错了什么有什么想法吗?