1
0
mirror of https://github.com/Stirling-Tools/Stirling-PDF.git synced 2024-09-21 12:20:13 +02:00

Fix ConcurrentModificationException by modifying resources outside the iteration. (#1719)

Fix ConcurrentModificationException by collecting XObject names

- Changed  to use a list to collect XObject names before removal.
- Avoids ConcurrentModificationException by modifying resources outside the iteration.

Co-authored-by: Anthony Stirling <77850077+Frooodle@users.noreply.github.com>
This commit is contained in:
PingLin8888 2024-08-19 19:42:55 +01:00 committed by GitHub
parent e1d0f2cd3e
commit fa0152aa2d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,6 +1,8 @@
package stirling.software.SPDF.service; package stirling.software.SPDF.service;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.pdfbox.cos.COSName; import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocument;
@ -16,7 +18,7 @@ public class PdfImageRemovalService {
/** /**
* Removes all image objects from the provided PDF document. * Removes all image objects from the provided PDF document.
* *
* This method iterates over each page in the document and removes any image XObjects found * <p>This method iterates over each page in the document and removes any image XObjects found
* in the page's resources. * in the page's resources.
* *
* @param document The PDF document from which images will be removed. * @param document The PDF document from which images will be removed.
@ -27,14 +29,22 @@ public class PdfImageRemovalService {
// Iterate over each page in the PDF document // Iterate over each page in the PDF document
for (PDPage page : document.getPages()) { for (PDPage page : document.getPages()) {
PDResources resources = page.getResources(); PDResources resources = page.getResources();
// Collect the XObject names to remove
List<COSName> namesToRemove = new ArrayList<>();
// Iterate over all XObject names in the page's resources // Iterate over all XObject names in the page's resources
for (COSName name : resources.getXObjectNames()) { for (COSName name : resources.getXObjectNames()) {
// Check if the XObject is an image // Check if the XObject is an image
if (resources.isImageXObject(name)) { if (resources.isImageXObject(name)) {
// Remove the image XObject by setting it to null // Collect the name for removal
resources.put(name, (PDXObject) null); namesToRemove.add(name);
} }
} }
// Now, modify the resources by removing the collected names
for (COSName name : namesToRemove) {
resources.put(name, (PDXObject) null);
}
} }
return document; return document;
} }