Christian Haegele created PDFBOX-6102:
-----------------------------------------

             Summary: StackOverflowError when Copying 1000+ bookmarks
                 Key: PDFBOX-6102
                 URL: https://issues.apache.org/jira/browse/PDFBOX-6102
             Project: PDFBox
          Issue Type: Bug
          Components: Utilities
    Affects Versions: 3.0.6 PDFBox
            Reporter: Christian Haegele
         Attachments: 3000_bookmarks.pdf

When copying 1000+ bookmarks from one document to another, a StackOverflowError 
is caused in PDFBox. The Exception doesn't occure in 
pdfbox 2.0.30.
 
 
```
    @Test
    void pdfBoxBookmarksTest() throws IOException {

        Path outputFolder = Files.createTempDirectory("pdfBox30Test");
        Path bookmarkedPdf = outputFolder.resolve("bookmarked.pdf");

        try (PDDocument sourceDocument = 
Loader.loadPDF(stackOverflowPdfPath.toFile());
                PDDocument targetDocument = 
Loader.loadPDF(stackOverflowPdfPath.toFile())) {

            PDDocumentOutline sourceOutline = 
sourceDocument.getDocumentCatalog().getDocumentOutline();

            PDOutlineItem current = sourceOutline.getFirstChild();
            while (current != null) {
                PDPage sourcePage = current.findDestinationPage(sourceDocument);
                if (sourcePage != null) {
                    PDPage copiedPage = targetDocument.importPage(sourcePage);

                    // PdfUtilities.createBookmark(targetDocument, copiedPage, 
current.getTitle());
                    // create bookmark
                    var aBookmarkTitle = current.getTitle();
                    if (aBookmarkTitle != null) {
                        PDPageXYZDestination tempDestXyz = new 
PDPageXYZDestination();
                        int pageIndex = 
targetDocument.getPages().indexOf(copiedPage); // 0-based
                        tempDestXyz.setPageNumber(pageIndex + 1); // 1-based 
for destination
                        tempDestXyz.setLeft((int) 
copiedPage.getCropBox().getLowerLeftX());
                        tempDestXyz.setTop((int) 
copiedPage.getCropBox().getUpperRightY());
                        tempDestXyz.setZoom(0);

                        PDOutlineItem bookmark = new PDOutlineItem();
                        bookmark.setDestination(tempDestXyz);
                        bookmark.setTitle(aBookmarkTitle);
                        
targetDocument.getDocumentCatalog().getDocumentOutline().addLast(bookmark);
                    }

                }
                current = current.getNextSibling();
            }

            targetDocument.save(bookmarkedPdf.toFile());
        } catch (IOException e) {
            Assertions.fail("Failed to create bookmarked PDF: " + 
e.getMessage());
        }

    }    @Test
    void pdfBoxBookmarksTest() throws IOException {

        Path outputFolder = Files.createTempDirectory("pdfBox30Test");
        Path bookmarkedPdf = outputFolder.resolve("bookmarked.pdf");

        try (PDDocument sourceDocument = 
Loader.loadPDF(stackOverflowPdfPath.toFile());
                PDDocument targetDocument = 
Loader.loadPDF(stackOverflowPdfPath.toFile())) {

            PDDocumentOutline sourceOutline = 
sourceDocument.getDocumentCatalog().getDocumentOutline();

            PDOutlineItem current = sourceOutline.getFirstChild();
            while (current != null) {
                PDPage sourcePage = current.findDestinationPage(sourceDocument);
                if (sourcePage != null) {
                    PDPage copiedPage = targetDocument.importPage(sourcePage);

                    // PdfUtilities.createBookmark(targetDocument, copiedPage, 
current.getTitle());
                    // create bookmark
                    var aBookmarkTitle = current.getTitle();
                    if (aBookmarkTitle != null) {
                        PDPageXYZDestination tempDestXyz = new 
PDPageXYZDestination();
                        int pageIndex = 
targetDocument.getPages().indexOf(copiedPage); // 0-based
                        tempDestXyz.setPageNumber(pageIndex + 1); // 1-based 
for destination
                        tempDestXyz.setLeft((int) 
copiedPage.getCropBox().getLowerLeftX());
                        tempDestXyz.setTop((int) 
copiedPage.getCropBox().getUpperRightY());
                        tempDestXyz.setZoom(0);

                        PDOutlineItem bookmark = new PDOutlineItem();
                        bookmark.setDestination(tempDestXyz);
                        bookmark.setTitle(aBookmarkTitle);
                        
targetDocument.getDocumentCatalog().getDocumentOutline().addLast(bookmark);
                    }

                }
                current = current.getNextSibling();
            }

            targetDocument.save(bookmarkedPdf.toFile());
        } catch (IOException e) {
            Assertions.fail("Failed to create bookmarked PDF: " + 
e.getMessage());
        }

    }
```



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to