iText 的writer.setEncryption()方法用于为生成的PDF设置密码。
private static String USER_PASSWORD = 'password';
private static String OWNER_PASSWORD = 'jack';
public static void main(String[] args) {
try
{
OutputStream file = new FileOutputStream(new File('PasswordProtected.pdf'));
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, file);
writer.setEncryption(USER_PASSWORD.getBytes(),
OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING,
PdfWriter.ENCRYPTION_AES_128);
document.open();
document.add(new Paragraph('为pdf设置密码'));
document.close();
file.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
日期:2020-09-17 00:09:30 来源:oir作者:oir
