【发布时间】:2014-11-12 16:01:41
【问题描述】:
我正在使用 iTextPdf 5.5.3 创建 PDF/A 文档,我希望用户通过上传字体的 .ttf 文件来选择自定义字体,因为FontFactory.getFont() 方法只需要字体名称作为字符串我必须将上传的文件写入用户的驱动器(我知道,我要求我的客户允许写入驱动器),然后将上传文件的路径传递给getFont() 方法,一切都完成后,我想从驱动器中删除上传的文件。这是我的代码:
File fontFile = new File("d:/temp/testFont.ttf");
try {
FileOutputStream outStream = new FileOutputStream(fontFile);
outStream.write(the bytes of the uploaded font file);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Font font = FontFactory.getFont(fontFile.getAbsolutePath(), BaseFont.CP1250 , BaseFont.EMBEDDED);
fontFile.delete();
此代码不起作用,不知何故getFont() 方法正在锁定字体文件,因此该文件没有被删除。我尝试了很多方法来做到这一点,例如:fontFile.deleteOnExit(); 或 FileDeleteStrategy.FORCE.delete("file path");,但没有什么对我有用。请指教。谢谢
【问题讨论】:
标签: java fonts itext itextpdf delete-file