我正在尝试使用 java 代码加密我现有的 pdf 文件,但是此代码将创建新的 pdf 文件,有人可以告诉我如何加密 pdf 文件而不更改其内容吗?
import java.io.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
public class PasswordProtectPDF {
public static byte[] UserPassword= "UserPassword".getBytes();
public static byte[] OwnerPassword = "OwnerPassword".getBytes();
public static void main(String[] args){
try {
String filename=new String();
filename="C:\\Agam_341238\\E-Books\\test123.pdf";
Document Document_For_Protection = new Document();
PdfWriter EncryptPDF=PdfWriter.getInstance(Document_For_Protection, new FileOutputStream(filename));
EncryptPDF.setEncryption(UserPassword, OwnerPassword,
~(PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING), PdfWriter.STANDARD_ENCRYPTION_128);
EncryptPDF.createXmpMetadata();
Document_For_Protection.open();
Document_For_Protection.add(new Paragraph("Some Contents"));
Document_For_Protection.close();
System.out.println("Done");
}
catch (Exception i)
{
System.out.println(i);
}
}
}