Issue
I would like to get a subset of pages out of a PDF file. Basically I give the function a file, split it into pages and then create a new file with just the pages between the start and end page. As the users are entering the page numbers I need correct for Lists starting at 0. Here is the code I have.
public static String SplitPdf(File source, Integer startPage, Integer endPage) throws Exception {
String sourceFileName = source.getAbsolutePath().replace("\\", "/");
File file = new File(sourceFileName);
PDDocument document = PDDocument.load(file);
Splitter splitter = new Splitter();
List<PDDocument> pages = splitter.split(document);
Integer correctedStartPage = startPage < 1 ? 1 : startPage - 1;
Integer correctedEndPage = endPage < 1 ? 1 : endPage - 1;
if (pages.size() <= correctedStartPage) throw new Exception("Start page: " + startPage + " No of pages: " + pages.size());
if (pages.size() <= correctedEndPage) throw new Exception("End page: " + endPage + " No of pages: " + pages.size());
if (startPage > endPage)
throw new Exception("Start page before end page. Start page: " + startPage + " End page: " + endPage);
PDDocument newDoc = new PDDocument();
String filePath = file.getParent() + "\\" + FilenameUtils.getBaseName(file.getName()) + "-fittingPart" +
"." + FilenameUtils.getExtension(file.getName());
for (int i = correctedStartPage; i <= correctedEndPage; i++) {
PDDocument currentPage = pages.get(i);
PDPage page = currentPage.getPage(0);
newDoc.addPage(page);
currentPage.close();
}
newDoc.save(filePath);
newDoc.close();
return filePath;
}
the error I am getting is:
COSStream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed?
I did check that the file exists
As far as I can see I am not closing the PDDocument too early... any ideas?
PS: I need to use apache PDFBox and not some other library 😊
Solution
Your need to close all PDDocument objects, the source object and the objects you've generated with splitter
and not just the final object. You should also make your life easier by using the splitter methods setStartPage()
and setEndPage()
instead of creating all these intermediate documents and then taking the first file. Make sure you're closing the objects only after all have been saved (due to possible sharing of resources)
Answered By - Tilman Hausherr