java.io.IOException: COSStream 已关闭且无法读取.也许它的封闭 PDDocument 已关闭? [英] java.io.IOException: COSStream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed?

查看:59
本文介绍了java.io.IOException: COSStream 已关闭且无法读取.也许它的封闭 PDDocument 已关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 apache PDFBox 保存 PDF 文件,但出现错误

I've tried to save PDF file with apache PDFBox but have error

java.io.IOException: COSStream 已关闭且无法读取.也许它的封闭 PDDocument 已关闭?

java.io.IOException: COSStream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed?

怎么了?

PDDocument document = PDDocument.load(new File(fileName));
try {
    ...
    document.save(storeFileName);
} finally {
    document.close();
}

推荐答案

即使正确关闭了 PDDoc,我也会看到上述错误.可能是什么问题.

I see the above error even if the PDDoc is closed properly. What might be the issue.

我的代码是:

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;
}

这篇关于java.io.IOException: COSStream 已关闭且无法读取.也许它的封闭 PDDocument 已关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆