PDFBox IO异常:COSStream已关闭,无法读取 [英] PDFBox IO Exception: COSStream has been closed and cannot be read

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

问题描述

我使用PDFBox在Java中编写的某些代码有问题.我正在尝试根据从Excel电子表格中读取的值,用特定的表格填充PDF.以下是我的课程文件.

I am having an issue with some code I'm writing in Java using PDFBox. I am attempting to populate a PDF with particular forms based on values read from an excel spreadsheet. Below is my class file.

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

我收到以下错误:线程主"中的异常java.io.IOException:COSStream已关闭,无法读取.也许其随附的PDDocument已关闭?

我已经进行了研究,在运行 workingDocument.save(fileName)命令之前,PDDocument似乎已关闭.我不太确定如何解决此问题,而且我对如何找到解决方法也有些困惑.我对编程有些生疏,所以我们将不胜感激!同样,任何有关如何使将来的帖子更具信息性的反馈都是很好的.

I've done research and it seems like a PDDocument is closing before I run the workingDocument.save(fileName) command. I'm not quite sure how to fix this, and I'm also a bit lost on how to find a workaround. I'm a bit rusty on my programming, so any help would be super appreciated! Also any feedback on how to make future posts more informative would be great.

预先感谢

推荐答案

请尝试

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

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

Update: Since merger.mergeDocuments(); is deprecated in recent APIs, try to make use of the same method using following overloaded methods...

merger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());

merger.mergeDocuments(MemoryUsageSetting.setupTempFileOnly());

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

Depends on your memory usage, you can further fine tune this method by passing MemoryUsageSetting object.

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

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