title: Copying pages from one PDF to another gives an error on save
body: <p>I'm trying to take pages from one PDF, scale them down, and put them side-by-side in another PDF. To do this I make an intermediate PDF that has all of the pages from the source scaled down to the size I need to place them side-by-side. Then I go thought the scaled PDF and copy the pages two at a time to the final PDF. My thinking is that I'm down with the scaled PDF so I can close it but when I do that I get an error trying to save the final PDF that says</p>
  <pre><code>COSStream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed?
  </code></pre>
  <p>I'm not sure why the intermediate doc should matter when I try to save the final doc. It could be that I'm doing something wrong in the copying of pages? Here's the code I use for that:</p>
  <pre><code>private PDDocument sideBySide(PaperSize paperSize, PaperSize pageSize) throws IOException {
      PDRectangle targetPaperSize = getRect(paperSize);
      PDRectangle targetPageSize = getRect(pageSize);
  
      PDDocument scaledDoc = scaleDoc(pageSize, doc);
      PDDocument outputDoc = new PDDocument();
  
      final double theta = Math.PI / 2;
      for (int offset = 0; offset &lt; scaledDoc.getNumberOfPages() - 1; offset+=2) {
          PDPage twoUp = new PDPage(targetPaperSize);
          twoUp.setRotation(90);
          twoUp.setResources(new PDResources());
          outputDoc.addPage(twoUp);
  
          PDPage leftPage = scaledDoc.getPage(offset);
          PDPage rightPage = scaledDoc.getPage(offset + 1);
  
          PDFormXObject leftObject = importAsXObject(outputDoc, leftPage);
          twoUp.getResources().add(leftObject);
  
          PDFormXObject rightObject = importAsXObject(outputDoc, rightPage);
          twoUp.getResources().add(rightObject);
  
          PDPageContentStream content = new PDPageContentStream(outputDoc, twoUp);
  
          AffineTransform leftTrans = AffineTransform.getRotateInstance(theta);
          leftTrans.concatenate(AffineTransform.getTranslateInstance(0, -targetPageSize.getHeight()));
  
          AffineTransform rightTrans = AffineTransform.getRotateInstance(theta);
          rightTrans.concatenate(AffineTransform.getTranslateInstance(targetPageSize.getWidth(), -targetPageSize.getHeight()));
  
          leftObject.setMatrix(leftTrans);
          rightObject.setMatrix(rightTrans);
  
          content.drawForm(leftObject);
          content.drawForm(rightObject);
          content.close();
      }
  
      scaledDoc.close();
      return outputDoc;
  }
  </code></pre>