PDFbox saying PDDocument closed when its not

后端 未结 2 1421
既然无缘
既然无缘 2021-01-25 11:24

I am trying to populate repeated forms with PDFbox. I am using a TreeMap and populating the forms with individual records. The format of the pdf form is such that there are si

2条回答
  •  抹茶落季
    2021-01-25 11:47

    Even though the above answer was marked as the solution to the problem, since the solution is buried in the comments, I wanted to add this answer at this level. I spent several hours searching for the solution.

    My code snippets and comments.

    // Collection solely for purpose of preventing premature garbage collection
    List sourceDocuments = new ArrayList<>( );
    
    ...
    
    // Source document (actually inside a loop)
    PDDocument docIn = PDDocument.load( artifactBytes );
    
    // Add document to collection before using it to prevent the problem
    sourceDocuments.add( docIn );
    
    // Extract from source document 
    PDPage extractedPage = docIn.getPage( 0 );
    // Add page to destination document
    docOut.addPage( extractedPage );
    
    ...
    
    // This was failing with "COSStream has been closed and cannot be read."
    // Now it works.
    docOut.save( bundleStream );
    

提交回复
热议问题