我正在尝试将文本文件转换为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);
行,该过程当然会在没有密码的情况下完成,但是一旦出现该行,该过程就将挂起。我在做什么错了?
答案 0 :(得分:0)
我知道了问题所在,尽管我应该将答案发布给可能遇到相同问题的任何人。问题不在代码中,而在类路径中的bcprov-jdk
库中。我有这个库的不同版本,当我取出其中一个时,问题就解决了。