COSStream已关闭,无法读取 [英] COSStream has been closed and cannot be read

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

问题描述

我的项目中有下一个代码,它不时地落在COSStream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed? 它发生在不同的时间和不同的工作量,所以我想解决它。 提前谢谢。

public void transferBankActPagesToPdfFile(List<PdfBankActPage> acts, HttpServletResponse response)
            throws IOException {
        try (PDDocument outPDDocument = new PDDocument()) {
            for (PdfBankActPage pdfBankActPage : acts) {
                String templateFilename = TEMPLATES_FOLDER + DELIMETER + pdfBankActPage.getPdfTemplateName();
                PDDocument templatePDDocument = PDDocument.load(readResource(templateFilename));
                PDPage pdPage = templatePDDocument.getPage(0);
                String fontTemplatePath = TEMPLATES_FOLDER + DELIMETER + FONT_TEMPLATE;
                PDDocument fontTemplatePdf = PDDocument.load(readResource(fontTemplatePath));
                PDPage fontTemplatePage = fontTemplatePdf.getPage(0);
                PDResources fontTemplateResources = fontTemplatePage.getResources();
                PDFont cyrillicFont = null;
                for (COSName cosName : fontTemplateResources.getFontNames()) {
                    if (cosName.getName().equals("F4")) {
                        cyrillicFont = fontTemplateResources.getFont(cosName);
                    }
                }
                outPDDocument.addPage(pdPage);
                PDPageContentStream contentStream = new PDPageContentStream(templatePDDocument, pdPage,
                        PDPageContentStream.AppendMode.APPEND, true, true);
                List<PdfTextLine> textLines = pdfBankActPage.getTextLines();
                if (textLines != null) {
                    for (PdfTextLine textLine : textLines) {
                        contentStream.setFont(cyrillicFont, textLine.getFontSize());
                        contentStream.beginText();
                        contentStream.newLineAtOffset(textLine.getOffsetX(), textLine.getOffsetY());
                        contentStream.showText(textLine.getText());
                        contentStream.endText();
                    }
                }
                contentStream.close();
            }
            response.setContentType(MediaType.APPLICATION_PDF_VALUE);
            outPDDocument.save(response.getOutputStream());
        }
    }

和此处部分加载资源:

 private InputStream readResource(String resourceFilename) {
        InputStream inputStream = PdfBankActPagesToPdfFile.class.getResourceAsStream(resourceFilename);
        if (inputStream == null) {
            inputStream = PdfBankActPagesToPdfFile.class.getClassLoader().getResourceAsStream(resourceFilename);
        }
        return inputStream;
    }

推荐答案

您使用重新创建的模板文档(templatePDDocumentfontTemplatePdf)中的流,并在每次循环迭代中提供免费的垃圾回收。因此,在您调用outPDDocument.save之前,这些模板文档中的一些可能已经被垃圾回收完成,从而导致您观察到的错误。

如果保留此基本体系结构,则可以通过将这些模板文档全部添加到某个集合并仅在调用outPDDocument.save后清除该集合来防止这些模板文档过早定版。

或者,您也可以切换到克隆模板页面并使用克隆,而不是使用原始模板页面。

这篇关于COSStream已关闭,无法读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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