PDFBox: Split PDF and add bookmarks IOException

In my project I split a pdf file by bookmarks. A new pdf file should be created containing the splitted pages and some new bookmarks.

This causes following exception:
Exception in thread “main” java.io.IOException: COSStream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed?

If I remove the line outline.addLast(pagesOutline); the new pdf contains the splitted pages (no exception) but obviously not the bookmarks. I think there is something wrong with the way I try to add bookmarks.

My code:

private void tryCreatePDF(List<String> listOfBookmarks) throws IOException
{
    document = PDDocument.load(new File("C:/Users/me/Desktop/existingpdf.pdf"));
    PDDocumentCatalog catalog = document.getDocumentCatalog();
    PDDocumentOutline bookmarksInExistingFile = catalog.getDocumentOutline();

    destinationPDF = new PDDocument();
    outline = new PDDocumentOutline();
    destinationPDF.getDocumentCatalog().setDocumentOutline(outline);

    pagesOutline = new PDOutlineItem();
    pagesOutline.setTitle("First Bookmark");
    outline.addLast(pagesOutline);

    for(String bookmarkToSplit : listOfBookmarks)
    {
        addPagesFromExistingFileToNewPDF(bookmarksInExistingFile, bookmarkToSplit);
    }

    createNewPDF();
}

private void addPagesFromExistingFileToNewPDF(PDOutlineNode outlineNodeFromExistingPDF, String bookmarkToSplit) throws IOException
{
    PDOutlineItem current = outlineNodeFromExistingPDF.getFirstChild();

    while(current != null)
    {
        PDPage currentPage = current.findDestinationPage(document);
        Integer pageNumber = document.getDocumentCatalog().getPages().indexOf(currentPage) + 1;

        addPagesFromExistingFileToNewPDF(current, bookmarkToSplit);

        String bookmark = current.getTitle().trim();

        if(bookmark.matches(bookmarkToSplit + "//s.+"))
        {
            Splitter splitter = new Splitter();

            splitter.setStartPage(pageNumber);
            splitter.setEndPage(pageNumber);

            List<PDDocument> pages = splitter.split(document);

            addPagesToNewDocument(pages);
        }

        current = current.getNextSibling();
    }
}

private void addPagesToNewDocument(List<PDDocument> pages) throws IOException
{
    PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();

    for(int index = 0; index < pages.size(); index++)
    {
        PDDocument doc = pages.get(index);

        pdfMergerUtility.appendDocument(destinationPDF, doc);

        if(index == 0)
        {
            addNewBookmarkToNewPDF(doc);
        }

        doc.close();
    }
}

private void addNewBookmarkToNewPDF(PDDocument doc)
{
    PDPageFitWidthDestination dest = new PDPageFitWidthDestination();
    dest.setPage(doc.getPage(0));

    PDOutlineItem bookmark = new PDOutlineItem();
    bookmark.setDestination(dest);
    bookmark.setTitle("Child Bookmark");

    pagesOutline.addLast(bookmark);
}

private void createNewPDF() throws IOException
{
    File targetFile = new File("C:/Users/me/Desktop/newpdf.pdf");

    pagesOutline.openNode();
    outline.openNode();

    destinationPDF.save(targetFile);
    destinationPDF.close();

    document.close();
}

The stacktrace:

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:77)
    at org.apache.pdfbox.cos.COSStream.createRawInputStream(COSStream.java:125)
    at org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(COSWriter.java:1203)
    at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:383)
    at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:522)
    at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObjects(COSWriter.java:460)
    at org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:444)
    at org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:1099)
    at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:419)
    at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1370)
    at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1257)
    at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1267)
    at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1238)
    at askstackoverflow.SplitPDFByBookmarks.createNewPDF(SplitPDFByBookmarks.java:126)
    at askstackoverflow.SplitPDFByBookmarks.tryCreatePDF(SplitPDFByBookmarks.java:55)
    at askstackoverflow.SplitPDFByBookmarks.<init>(SplitPDFByBookmarks.java:33)
    at askstackoverflow.Main.main(Main.java:9)

Edit:
@All the following readers of this question:
After solving the problem, my program doesn’t work as I expected. (Every bookmark is pointing to the first page.) But this problem wasn’t part of this question.

Closing in addPagesToNewDocument() is too early. This also closes resources from the source document because these resources are used by both. Better start closing only after all documents have been saved.