我正在使用 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");
但没有什么对我有用。请指教。谢谢