Shawn
Shawn

Reputation: 51

PDFBox IO Exception: COSStream has been closed and cannot be read

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

I am getting the following error: Exception in thread "main" java.io.IOException: COSStream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed?

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.

Thanks in advance

Upvotes: 5

Views: 6819

Answers (1)

Pham Duy Thai
Pham Duy Thai

Reputation: 41

Please try it

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

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

or

merger.mergeDocuments(MemoryUsageSetting.setupTempFileOnly());

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

Upvotes: 4

Related Questions