我尝试使用此代码对我的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。

关于我做错了什么的主意吗?

最佳答案:

NullPointerExceptionstamper.close()PdfReaderPdfStamper,close()PdfReaderPdfReaderPdfWriter