Topic: attempting to open a COSStream after you have added files to your mainPDF. In other words, attempting to write text to your main PDF Document after you have added PDF files to your main PDF File. I do not want to save or close my mainPDF because I plan on adding more PDFFiles and more text again and again. In my case, I am attempting to open a PDPageContentStream contentStream after using TreeMerge. Apparently, the main document is closing prematurely? Scroll down to see my code below
There is certainly lots of trouble with this topic since around 2014: I really wish the answers to these questions would post complete code. So, I made a list of posts of similar problems and attempts at solving this (scroll below). I am sure many more begginners are going to run into this problem.
@mkl and @community https://stackoverflow.com/a/49973366/14092356
- Try using
newDoc.addPage(newDoc.importPage(doc.getPage(0)));
instead ofnewDoc.addPage(doc.getPage(1));
- Remember to close the new files you are inserting int your mainPDF
- When adding your new files, make sure you assign them to a variable so garbage collection does not close it prematurely either
- Try adding these files to an ArrayList of PDDocuments https://stackoverflow.com/a/61299092/14092356
- Try using
Where the PDPages are instantiated matters. COSStream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed?
From the post: PDFBox IO Exception: COSStream has been closed and cannot be read. The comment https://stackoverflow.com/a/55591429/14092356 - did not work for me: I post the code here
'''
public void tableOfContents() throws IOException { String path1 = new File("Index1.pdf").getAbsoluteFile().toString(); File file1 = new File(path1); String path2 = new File("Index2.pdf").getAbsoluteFile().toString(); File file2 = new File(path2); PDFMergerUtility merger = new PDFMergerUtility(); PDDocument combine = PDDocument.load(file1); PDDocument combine2 = PDDocument.load(file2); merger.appendDocument(mainDocument, combine); //combine.close(); merger.appendDocument(mainDocument, combine2); merger.mergeDocuments(); merger.mergeDocuments(MemoryUsageSetting.setupTempFileOnly()); }
More Resources
- PDFBox COSStream closed before use
- java.io.IOException: COSStream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed?
- "IOException: COSStream has been closed and cannot be read" when trying to save PDF after adding page with PdfBox
- Merging lots of pdf files is going to be difficult because you need to keep track of opening and closing https://issues.apache.org/jira/browse/PDFBOX-3901
- Reading a particular page from a PDF document using PDFBox
Here is my code with my problem
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
import java.io.IOException;
public class main {
public static void main(String[] args) throws IOException {
PDDocument document = new PDDocument();
PDOutlineItem pagesOutline = new PDOutlineItem();
for (int numberOfPages = 0; numberOfPages < 5; numberOfPages++) {
//Creating a blank page
PDPage blankPage = new PDPage();
//Adding the blank page to the document
document.addPage(blankPage);
}
ExampleImportingClass example = new ExampleImportingClass(document);
document.save("exampleError.pdf");
System.out.println("PDF created");
document.close();
}
}
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import java.io.File;
import java.io.IOException;
public class ExampleImportingClass {
private PDDocument document;
public ExampleImportingClass(PDDocument document) throws IOException {
this.document = document;
page1();
page2_3();
page4();
}
public void page1() throws IOException {
PDPage page1 = document.getPage(0);
String path = new File("catPicture3.jpg").getAbsoluteFile().toString();
PDImageXObject pdImage = PDImageXObject.createFromFile(path, document);
PDPageContentStream content1 = new PDPageContentStream(document, page1);
content1.beginText();
content1.endText();
content1.drawImage(pdImage, 50, 400,300,300);
content1.close();
}
public void page2_3() throws IOException {
String path1 = new File("Example1.pdf").getAbsoluteFile().toString();
File file1 = new File(path1);
String path2 = new File("Example2.pdf").getAbsoluteFile().toString();
File file2 = new File(path2);
PDPage page1 = document.getPage(0);
PDPage page2 = document.getPage(2);
PDPageTree mergePD = document.getPages();
PDDocument doc1 = PDDocument.load(file1);
PDDocument doc2 = PDDocument.load(file2);
mergePD.insertAfter(doc1.getPage(0), page1);
mergePD.insertAfter(doc2.getPage(0), page2);
}
public void page4() throws IOException {
PDPage page = document.getPage(3);
PDPageContentStream content1 = new PDPageContentStream(document, page);
content1.beginText();
content1.setFont(PDType1Font.TIMES_ROMAN, 14);
content1.newLineAtOffset(50, 350);
content1.showText("If we remove page4() method AND its not working, then its not ");
content1.endText();
content1.close();
}
Here is the infamous error stack trace:
Aug 27, 2020 11:10:48 PM org.apache.pdfbox.cos.COSDocument finalize WARNING: Warning: You did not close a PDF Document Aug 27, 2020 11:10:48 PM org.apache.pdfbox.cos.COSDocument finalize WARNING: Warning: You did not close a PDF Document
Exception in thread "main" java.io.IOException: COSStream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed?
at org.apache.pdfbox.cos.COSStream.checkClosed(COSStream.java:154)
at org.apache.pdfbox.cos.COSStream.createRawInputStream(COSStream.java:204)
at org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(COSWriter.java:1219)
at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:475)
at org.apache.pdfbox.cos.COSObject.accept(COSObject.java:158)
at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:526)
at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObjects(COSWriter.java:464)
at org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:448)
at org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:1113)
at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:452)
at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1386)
at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1273)
at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1357)
at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1328)
at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1316)
at main.main(main.java:22)
at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1316) at Runner.main(Runner.java:25)
. I doubtRunner
is a PDFBox class, so it appears to be yours, and the exception occurs while your code attempts to save. – mkl