java.io.IOException.COSStream已被关闭,无法读取。COSStream已被关闭,无法读取。也许它的外层PDDocument已被关闭?

问题描述 投票:0回答:1

我试着用apache PDFBox保存PDF文件,但出现了错误。

java.io.IOException.COSStream已被关闭,无法读取。COSStream已被关闭,无法读取。也许它的外层PDDocument已经被关闭了?

这到底是怎么回事?

PDDocument document = PDDocument.load(new File(fileName));
try {
    ...
    document.save(storeFileName);
} finally {
    document.close();
}
java pdfbox
1个回答
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;
}
© www.soinside.com 2019 - 2024. All rights reserved.