Stirling-PDF/app/core
Balázs Szücs c895b09142
feat(crop): add auto-crop functionality to detect and remove white space (#4847)
# Description of Changes

TLDR:
- Implemented automatic content bounds detection in CropController
- Added new auto-crop option in CropPdfForm and HTML template
- Updated JavaScript for better PDF rendering and interaction handling
- Also updated error handling and resource management in cropping
functions

Introduces auto-crop feature for automatic whitespace detection and
removal, plus resource management improvements.

### Auto-Crop Detection
- Automatically detects and removes whitespace using content boundary
detection
- Adaptive pixel sampling for images >2000px
- Sequential page processing for memory efficiency
- New opt-in checkbox in crop interface
- 150 DPI rendering for accurate detection

### Resource Management
- Fixed document lifecycle to prevent resource access violations
- Source documents remain open during LayerUtility operations
- Proper nested try-with-resources implementation
- Prevents crashes with `LayerUtility.importPageAsForm()`

### Memory Optimization
- Early exit for images <1px
- Adaptive sampling: O(n) instead of O(n²) for large pages
- Explicit BufferedImage nulling after processing

### Error Handling
- Proper try-with-resources for temporary files
- Defensive null checks and interrupt handling
- Updated exception messages

### CropPdfForm
- Changed coordinates from primitive to wrapper types (Float) for
nullable support
- Added `autoCrop` boolean field (default false)
- Maintains backward compatibility


### crop.js
- Track and cancel render tasks to prevent memory leaks
- Improved resize handling without file reloading
- Fixed nested event listeners
- Canvas clearing before rendering prevents artifacts
- DOM layout stability improvements


### Front-end

<img width="956" height="802" alt="image"
src="https://github.com/user-attachments/assets/2e8e5bd2-4948-4df1-9937-3358b36d03a0"
/>


### Samples:

<img width="960" height="775" alt="image"
src="https://github.com/user-attachments/assets/b27d3c65-1517-4318-b3d2-ca2d9864abf9"
/>


[test_cropped-2.pdf](https://github.com/user-attachments/files/23436674/test_cropped-2.pdf)

<img width="960" height="775" alt="image"
src="https://github.com/user-attachments/assets/095374fb-9e89-4ea1-a5c7-4287c909e20a"
/>


[pdf_hyperlink_example_cropped.pdf](https://github.com/user-attachments/files/23436678/pdf_hyperlink_example_cropped.pdf)

<img width="960" height="775" alt="image"
src="https://github.com/user-attachments/assets/b01e3633-15b7-4eea-a99b-09d875a380b4"
/>


[Sample-Fillable-PDF_cropped.pdf](https://github.com/user-attachments/files/23436680/Sample-Fillable-PDF_cropped.pdf)

<img width="960" height="858" alt="image"
src="https://github.com/user-attachments/assets/74824e8f-2f45-4e9d-9bd3-ed1248146f81"
/>


[sample-2_cropped.pdf](https://github.com/user-attachments/files/23436684/sample-2_cropped.pdf)

Closes: #1351

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

- What was changed
- Why the change was made
- Any challenges encountered

Closes #(issue_number)
-->

---

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

### Translations (if applicable)

- [ ] I ran
[`scripts/counter_translation.py`](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/docs/counter_translation.md)

### UI Changes (if applicable)

- [X] 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.

---------

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
Co-authored-by: Anthony Stirling <77850077+Frooodle@users.noreply.github.com>
2025-11-17 11:53:15 +00:00
..
src feat(crop): add auto-crop functionality to detect and remove white space (#4847) 2025-11-17 11:53:15 +00:00
.gitignore feat(security): add PFX alias for PKCS12; accept .crt/.cer/.der certs & .key keys; add certificate-signing tests (#4297) 2025-09-04 15:30:32 +01:00
build.gradle build(deps): bump commons-io:commons-io from 2.20.0 to 2.21.0 (#4852) 2025-11-10 15:03:16 +00:00