[ 
https://issues.apache.org/jira/browse/PDFBOX-6102?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18040559#comment-18040559
 ] 

Tilman Hausherr commented on PDFBOX-6102:
-----------------------------------------

Great. Note that this isn't the final fix because of trouble mentioned at the 
end of the other issue, so don't use this snapshot for production at this time. 
I've added you as a watcher there.

> 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
>            Priority: Major
>             Fix For: 3.0.7 PDFBox
>
>         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.
>  
>  
> {code:java}
>    @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());
>         }
>     }
> {code}



--
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