无法使用 itextpdf 挂起来加密 pdf



我正在尝试将文本文件转换为pdf并在此过程中传递密码。方法如下:

public static void convertStatementFiles(File sourceDir, File destDir, String logoImgPath, String backImagePath, String threadId) {
log.debug("Attempting File Conversion to PDF........");
FilenameFilter only = new OnlyExt("LST");
String[] filenames = sourceDir.list(only);
log.debug("Source Files" + sourceDir.getAbsolutePath());
Config cfg = new Config();
try {
for (int k = 0; k < filenames.length; k++) {
FileInputStream fs = new FileInputStream(sourceDir.getAbsolutePath() + System.getProperty("file.separator") + filenames[k]);
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
String accountNumber;
for (int j = 0; j < 11; j++) {
br.readLine();
}
accountNumber = br.readLine().trim().substring(0, 13);
File img = new File(logoImgPath);
if (!img.exists()) {
FileUtils.writeByteArrayToFile(new File(logoImgPath), cfg.getLogoImage());
}
//Get Background Image
File backImg = new File(backImagePath);
if (!backImg.exists()) {
FileUtils.writeByteArrayToFile(new File(backImagePath), cfg.getBackgroundImage());
}
//Create Pdf file
Document document = new Document(PageSize.B3);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(destDir.getAbsolutePath() + System.getProperty("file.separator") + accountNumber + ".pdf"));
PdfEvent event = new PdfEvent(img.getAbsolutePath(), backImg.getAbsolutePath());
writer.setPageEvent(event);
String password = accountNumber.substring(0, 2) + accountNumber.substring(11, 13);
writer.setEncryption(PdfWriter.ENCRYPTION_AES_128, password, password, PdfWriter.AllowPrinting);
writer.createXmpMetadata();
document.open();
br = new BufferedReader(new FileReader(sourceDir.getAbsolutePath() + System.getProperty("file.separator") + filenames[k]));
String line;
Paragraph p;
Font normal = new Font(Font.FontFamily.COURIER, 12, Font.BOLD);
Font bold = new Font(Font.FontFamily.COURIER, 12, Font.BOLD);
boolean title = true;
int num = 0;
int pagebreak = 0;
while ((line = br.readLine()) != null) {
num++;
p = new Paragraph(line, title ? bold : normal);
p.setAlignment(Element.ALIGN_JUSTIFIED);
title = line.isEmpty();
document.add(p);
if (line.trim().startsWith("Page Total")) {
pagebreak = num + 1;
}
if (num == pagebreak) {
document.newPage();
}
}
document.close();
}
log.debug("Conversion to PDF Done........");
cleanStatementsDirectories(sourceDir, threadId);
} catch (Exception asd) {
System.out.println(asd.getMessage());
}
}

如果没有这一行writer.setEncryption(PdfWriter.ENCRYPTION_AES_128, password, password, PdfWriter.AllowPrinting);该过程当然无需密码即可完成,但是一旦该行存在,该过程就会在这一点上挂起。我做错了什么?

我想出了问题所在,尽管我应该为可能面临相同问题的任何人发布答案。问题不在于代码,而在于类路径中的bcprov-jdk库。我有这个库的不同版本,当我拉出其中一个时,问题就解决了。

最新更新