我尝试使用apache PDFBox保存PDF文件,但有错误
java.io.IOException:COSStream已关闭且无法读取。或许其封闭的PDDocument已被关闭?
问题是什么?
PDDocument document = PDDocument.load(new File(fileName));
try {
...
document.save(storeFileName);
} finally {
document.close();
}
答案 0 :(得分:0)
即使正确关闭了PDDoc,我仍然看到上述错误。可能是什么问题。
我的代码是:
public PDPage addPageWithSVG(PDDocument document, String svg) throws IOException {
log.debug("addPageWithSVG");
// Ensure Fop don't auto system scan font to avoid RHEL pitfall link with legit infinite path
PDFTranscoder transcoder = new PDFTranscoder();
transcoder.addTranscodingHint(PDFTranscoder.KEY_AUTO_FONTS, false);
// end of correction
TranscoderInput transcoderInput = new TranscoderInput(new StringReader(svg));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
TranscoderOutput transcoderOutput = new TranscoderOutput(byteArrayOutputStream);
PDDocument source = null;
PDPage newPage = null;
try {
transcoder.transcode(transcoderInput, transcoderOutput);
byte pdfData[] = byteArrayOutputStream.toByteArray();
source = PDDocument.load(pdfData);
PDPage sourcePage = source.getPage(0);
newPage = document.importPage(sourcePage);
} catch (TranscoderException | IOException e) {
e.printStackTrace();
} finally {
if(source != null){
source.close();
}
}
return newPage;
}