A fix for the bug : Extract Pages produce output pdf of same size (issue #1480) (#4636)

# Description of Changes

<!--
Please provide a summary of the changes, including:

Previously, the extraction logic reused the same PDF document instance.
It deleted all existing pages, then re-added only the pages that needed
to be extracted. However, some PDFs define a shared Resources dictionary
at the document level. In those cases, even after page deletion, the
shared resources remained, leading to incorrect or bloated output.

The updated implementation now creates a new PDF document based on the
old document. It copies only the selected pages from the original
document into this new file. This ensures no leftover shared resources
or unwanted metadata are carried over from the source PDF.

Closes #1480 
-->

---

## Checklist

### General

- [x] I have read the [Contribution
Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md)
- [x] I have read the [Stirling-PDF Developer
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md)
(if applicable)
- [ ] I have read the [How to add new languages to
Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md)
(if applicable)
- [x] I have performed a self-review of my own code
- [x] My changes generate no new warnings

### Documentation

- [ ] I have updated relevant docs on [Stirling-PDF's doc
repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/)
(if functionality has heavily changed)
- [ ] I have read the section [Add New Translation
Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md#add-new-translation-tags)
(for new translation tags only)

### UI Changes (if applicable)

- [ ] Screenshots or videos demonstrating the UI changes are attached
(e.g., as comments or direct attachments in the PR)

### Testing (if applicable)

- [x] I have tested my changes locally. Refer to the [Testing
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md#6-testing)
for more details.

---------

Co-authored-by: YAOU Reda <yaoureda24@gmail.com>
This commit is contained in:
OUNZAR Aymane 2025-10-16 23:19:05 +02:00 committed by GitHub
parent 74c9176000
commit 9ea0edccde
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -260,18 +260,17 @@ public class RearrangePagesPDFController {
newPages.add(document.getPage(newPageOrder.get(i)));
}
// Remove all the pages from the original document
for (int i = document.getNumberOfPages() - 1; i >= 0; i--) {
document.removePage(i);
}
// Create a new document based on the original one
PDDocument rearrangedDocument =
pdfDocumentFactory.createNewDocumentBasedOnOldDocument(document);
// Add the pages in the new order
for (PDPage page : newPages) {
document.addPage(page);
rearrangedDocument.addPage(page);
}
return WebResponseUtils.pdfDocToWebResponse(
document,
rearrangedDocument,
GeneralUtils.generateFilename(
pdfFile.getOriginalFilename(), "_rearranged.pdf"));
} catch (IOException e) {