From fa0152aa2db61d00cd5be70b4fe3cec3b8bb8e04 Mon Sep 17 00:00:00 2001 From: PingLin8888 <88387490+PingLin8888@users.noreply.github.com> Date: Mon, 19 Aug 2024 19:42:55 +0100 Subject: [PATCH] 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> --- .../SPDF/service/PdfImageRemovalService.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/service/PdfImageRemovalService.java b/src/main/java/stirling/software/SPDF/service/PdfImageRemovalService.java index 985d1f48..029d0924 100644 --- a/src/main/java/stirling/software/SPDF/service/PdfImageRemovalService.java +++ b/src/main/java/stirling/software/SPDF/service/PdfImageRemovalService.java @@ -1,6 +1,8 @@ package stirling.software.SPDF.service; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import org.apache.pdfbox.cos.COSName; import org.apache.pdfbox.pdmodel.PDDocument; @@ -16,7 +18,7 @@ public class PdfImageRemovalService { /** * Removes all image objects from the provided PDF document. * - * This method iterates over each page in the document and removes any image XObjects found + *
This method iterates over each page in the document and removes any image XObjects found
* in the page's resources.
*
* @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
for (PDPage page : document.getPages()) {
PDResources resources = page.getResources();
+ // Collect the XObject names to remove
+ List