java - PDFBox IO异常: COSStream has been closed and cannot be read

标签 java apache pdfbox

我在使用 PDFBox 以 Java 编写的一些代码时遇到问题。我正在尝试根据从 Excel 电子表格读取的值使用特定表单填充 PDF。下面是我的类文件。

import java.io.FileInputStream;
import java.io.File;
import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.hssf.usermodel.*;

/**
 * This is a test file for reading and populating a PDF with specific forms
 */
public class JU_TestFile {

    PDPage Stick_Form;
    PDPage IKE_Form;
    PDPage BO_Form;

    /**
     * Constructor.
     */
    public JU_TestFile() throws IOException
    {
        this.BO_Form = (PDPage) PDDocument.load(new File("C:\\Users\\saf\\Desktop\\JavaTest\\BO Pole Form.pdf")).getPage(0);
        this.IKE_Form = (PDPage) PDDocument.load(new File("C:\\Users\\saf\\Desktop\\JavaTest\\IKE Form.pdf")).getPage(0);
        this.Stick_Form = (PDPage) PDDocument.load(new File("C:\\Users\\saf\\Desktop\\JavaTest\\Sticking Form.pdf")).getPage(0);
    }

    public void buildFile(String fileName, String excelSheet) throws IOException {
        // Create a Blank PDF Document and load in JU Excel Spreadsheet
        PDDocument workingDocument = new PDDocument();
        FileInputStream fis = new FileInputStream(new File(excelSheet));

        // Load in the workbook
        HSSFWorkbook JU_XML = new HSSFWorkbook(fis);

        int sheetNumber = 0;
        int rowNumber = 0;
        String cellValue = "Starting Value";

        HSSFSheet currentSheet = JU_XML.getSheetAt(sheetNumber);

        // While we have not reached the 25th row in our current sheet
        while (rowNumber <= 24) {
            // Get the value in the current row, on the 8th column in the xls file
            cellValue = currentSheet.getRow(rowNumber + 6).getCell(7).getStringCellValue();

            // If it has stuff in it, 
            if (cellValue != "") {
                // Check if it has the letters "IKE" and append the IKE form to our PDF
                if (cellValue != "IKE") {
                    workingDocument.importPage(IKE_Form);
                // If it is anything else (other than empty), append the Stick Form to our PDF  
                } else {
                    workingDocument.importPage(Stick_Form);

                }
                // Let's move on to the next row
                rowNumber++;

                // If the next row number is the "26th" row, we know we need to move on to the
                // next sheet, and also reset the rows to the first row of that next sheet
                if (rowNumber == 25) {
                    rowNumber = 0;
                    currentSheet = JU_XML.getSheetAt(++sheetNumber);
                }
            // if the 9th row is empty, we should break out of the loop and save/close our PDF, we are done
            } else {
                break;
            }
        }

        workingDocument.save(fileName);
        workingDocument.close();
    }
}

我收到以下错误: 线程“main”中出现异常 java.io.IOException:COSStream 已关闭且无法读取。也许其封闭的 PDDocument 已关闭?

我已经进行了研究,在运行 workingDocument.save(fileName) 命令之前,PDDocument 似乎正在关闭。我不太确定如何解决这个问题,而且我也对如何找到解决方法感到有点迷失。我对我的编程有点生疏,所以任何帮助将非常感激!此外,任何有关如何使 future 的帖子内容更丰富的反馈都会很棒。

提前致谢

最佳答案

请尝试一下

PDFMergerUtility merger = new PDFMergerUtility();
PDDocument combine = PDDocument.load(file);
merger.appendDocument(getDocument(), combine);
merger.mergeDocuments();
combine.close();

更新: 由于 merger.mergeDocuments(); 在最近的 API 中已被弃用,因此请尝试使用以下重载方法来使用相同的方法...

merger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());

merger.mergeDocuments(MemoryUsageSetting.setupTempFileOnly());

根据您的内存使用情况,您可以通过传递 MemoryUsageSetting 对象进一步微调此方法。

关于java - PDFBox IO异常: COSStream has been closed and cannot be read,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40714266/

相关文章:

java - 如何调整文件中图像的大小?

java - JCEKS keystore 作为 War 中的资源

java - 使用 Apache Common Math 确定指数函数的系数

java - 使用 PDFBox 库在 PDF 中生成分层书签

java - PDFBox - 查找页面尺寸

java - 显示 java 对象时的 iframe 问题

java - 为什么 JFrame 重新定义它从接口(interface) "EXIT_ON_CLOSE"继承的 "WindowConstants"?

python - 通过两个单独的域访问 Django Web 应用程序

python - 配置不当 : Error importing middleware django. middleware.common: "No module named _md5"

java - 如何使用PDFBox向PDF添加背景图像?