【发布时间】:2014-09-05 08:04:51
【问题描述】:
我想让 pdf 文件有密码保护。我只是为了同样的目的而使用它,并在下面找到一个好的解决方案。它工作正常但是在我使用下面给定的代码保护 pdf 之后,它会清除我 pdf 中已经存在的所有数据。
此代码使用的 jar 文件是:
itextpdf-5.2.1.jar
bcmail-jdk16-1.46.jar
bcprov-jdk16-1.46.jar
bctsp-jdk16-1.46.jar
保护 PDF 的代码:
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Secure_file {
private static String USER_PASSWORD = "password";
private static String OWNER_PASSWORD = "secured";
public static void main(String[] args) throws IOException {
Document document = new Document();
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("E:\\sample.pdf"));
writer.setEncryption(USER_PASSWORD.getBytes(),OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING,PdfWriter.ENCRYPTION_AES_128);
document.open();
document.add(new Paragraph("This is Password Protected PDF document."));
document.close();
writer.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
我需要在这个程序中进行哪些更改?
【问题讨论】: