0
votes

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.

More Resources

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)
1
don't close doc1 and doc2 until done with the result file.Tilman Hausherr
Thank you, but if I do not close doc1 and doc2, I get the same error.Sina A
Then post that code, and also the last 10 lines of the result PDF. That one is the object that is closed prematurely.Tilman Hausherr
You say "I do not want to save or close my mainPDF" when the issue occurs. But the stack trace says otherwise, it says the exception occurs at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1316) at Runner.main(Runner.java:25). I doubt Runner is a PDFBox class, so it appears to be yours, and the exception occurs while your code attempts to save.mkl
I posted the code for the entire project. I can also email you the project. I recreated the project to isolate the problem. If you remove the text from page 1 or page4, it no longer works.Sina A

1 Answers

0
votes

Several things can cause this error. What finally worked for me was adding an offset value and blank text at the beginning of the stream. So I thought this was merging error because the problem disappeared when I stopped merging new PDF Files. However, it was a problem with my cover page. The COSStream from the cover page interfered with pdf files being merged.

My cover page only had an image and no text. For reasons still unknown to me, this was causing the error. Once I an offset for text and blank text, the program compiled. Note that PDImage already had an offset value before. Apparently this was not enough for the compiler.

content1.newLineAtOffset(50, 350);

content1.setFont(PDType1Font.TIMES_ROMAN, 14);

content1.showText("If we add this code it works now. Strange");

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.newLineAtOffset(50, 350);
        content1.setFont(PDType1Font.TIMES_ROMAN, 14);
        content1.showText("If we add this code it works now. Strange");

        content1.endText();
        content1.drawImage(pdImage, 50, 400,300,300);
        content1.close();
    }