title: PDFBox IO Exception: COSStream has been closed and cannot be read
body: <p>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.</p>
  
  <pre><code>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 &lt;= 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();
      }
  }
  </code></pre>
  
  <p>I am getting the following error: 
  <strong>Exception in thread "main" java.io.IOException: COSStream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed?</strong></p>
  
  <p>I've done research and it seems like a PDDocument is closing before I run the <em>workingDocument.save(fileName)</em> 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.</p>
  
  <p>Thanks in advance</p>