diff --git a/stirling-pdf/src/main/java/stirling/software/SPDF/service/AttachmentService.java b/stirling-pdf/src/main/java/stirling/software/SPDF/service/AttachmentService.java index b4fcdea79..4aa6dfe41 100644 --- a/stirling-pdf/src/main/java/stirling/software/SPDF/service/AttachmentService.java +++ b/stirling-pdf/src/main/java/stirling/software/SPDF/service/AttachmentService.java @@ -28,28 +28,17 @@ public class AttachmentService implements AttachmentServiceInterface { @Override public PDDocument addAttachment(PDDocument document, List attachments) throws IOException { - PDDocumentCatalog catalog = document.getDocumentCatalog(); - PDDocumentNameDictionary documentNames = catalog.getNames(); - PDEmbeddedFilesNameTreeNode embeddedFilesTree = new PDEmbeddedFilesNameTreeNode(); - - if (documentNames != null) { - embeddedFilesTree = documentNames.getEmbeddedFiles(); - } else { - documentNames = new PDDocumentNameDictionary(catalog); - documentNames.setEmbeddedFiles(embeddedFilesTree); - } - - catalog.setNames(documentNames); + PDEmbeddedFilesNameTreeNode embeddedFilesTree = getEmbeddedFilesTree(document); Map existingNames; try { - Map originalNames = embeddedFilesTree.getNames(); + Map names = embeddedFilesTree.getNames(); - if (originalNames == null) { + if (names == null) { log.debug("No existing embedded files found, creating new names map."); existingNames = new HashMap<>(); } else { - existingNames = new HashMap<>(originalNames); + existingNames = new HashMap<>(names); log.debug("Embedded files: {}", existingNames.keySet()); } } catch (IOException e) { @@ -95,4 +84,22 @@ public class AttachmentService implements AttachmentServiceInterface { return document; } + + private PDEmbeddedFilesNameTreeNode getEmbeddedFilesTree(PDDocument document) { + PDDocumentCatalog catalog = document.getDocumentCatalog(); + PDDocumentNameDictionary documentNames = catalog.getNames(); + + if (documentNames == null) { + documentNames = new PDDocumentNameDictionary(catalog); + } + + catalog.setNames(documentNames); + PDEmbeddedFilesNameTreeNode embeddedFilesTree = documentNames.getEmbeddedFiles(); + + if (embeddedFilesTree == null) { + embeddedFilesTree = new PDEmbeddedFilesNameTreeNode(); + documentNames.setEmbeddedFiles(embeddedFilesTree); + } + return embeddedFilesTree; + } }