diff --git a/src/main/java/stirling/software/SPDF/controller/api/MergeController.java b/src/main/java/stirling/software/SPDF/controller/api/MergeController.java index 5e9f97df..6bb25650 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/MergeController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/MergeController.java @@ -10,11 +10,16 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.List; +import java.util.stream.Collectors; import org.apache.pdfbox.Loader; import org.apache.pdfbox.multipdf.PDFMergerUtility; import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.PDDocumentCatalog; import org.apache.pdfbox.pdmodel.PDPage; +import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm; +import org.apache.pdfbox.pdmodel.interactive.form.PDField; +import org.apache.pdfbox.pdmodel.interactive.form.PDSignatureField; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.ResponseEntity; @@ -38,6 +43,7 @@ public class MergeController { private static final Logger logger = LoggerFactory.getLogger(MergeController.class); + // Merges a list of PDDocument objects into a single PDDocument public PDDocument mergeDocuments(List documents) throws IOException { PDDocument mergedDoc = new PDDocument(); for (PDDocument doc : documents) { @@ -48,6 +54,7 @@ public class MergeController { return mergedDoc; } + // Returns a comparator for sorting MultipartFile arrays based on the given sort type private Comparator getSortComparator(String sortType) { switch (sortType) { case "byFileName": @@ -108,37 +115,78 @@ public class MergeController { "This endpoint merges multiple PDF files into a single PDF file. The merged file will contain all pages from the input files in the order they were provided. Input:PDF Output:PDF Type:MISO") public ResponseEntity mergePdfs(@ModelAttribute MergePdfsRequest form) throws IOException { - List filesToDelete = new ArrayList(); + List filesToDelete = new ArrayList<>(); // List of temporary files to delete + ByteArrayOutputStream docOutputstream = + new ByteArrayOutputStream(); // Stream for the merged document + PDDocument mergedDocument = null; + + boolean removeCertSign = form.isRemoveCertSign(); + try { MultipartFile[] files = form.getFileInput(); - Arrays.sort(files, getSortComparator(form.getSortType())); - - PDFMergerUtility mergedDoc = new PDFMergerUtility(); - ByteArrayOutputStream docOutputstream = new ByteArrayOutputStream(); + Arrays.sort( + files, + getSortComparator( + form.getSortType())); // Sort files based on the given sort type + PDFMergerUtility mergerUtility = new PDFMergerUtility(); for (MultipartFile multipartFile : files) { - File tempFile = GeneralUtils.convertMultipartFileToFile(multipartFile); - filesToDelete.add(tempFile); - mergedDoc.addSource(tempFile); + File tempFile = + GeneralUtils.convertMultipartFileToFile( + multipartFile); // Convert MultipartFile to File + filesToDelete.add(tempFile); // Add temp file to the list for later deletion + mergerUtility.addSource(tempFile); // Add source file to the merger utility + } + mergerUtility.setDestinationStream( + docOutputstream); // Set the output stream for the merged document + mergerUtility.mergeDocuments(null); // Merge the documents + + byte[] mergedPdfBytes = docOutputstream.toByteArray(); // Get merged document bytes + + // Load the merged PDF document + mergedDocument = Loader.loadPDF(mergedPdfBytes); + + // Remove signatures if removeCertSign is true + if (removeCertSign) { + PDDocumentCatalog catalog = mergedDocument.getDocumentCatalog(); + PDAcroForm acroForm = catalog.getAcroForm(); + if (acroForm != null) { + List fieldsToRemove = + acroForm.getFields().stream() + .filter(field -> field instanceof PDSignatureField) + .collect(Collectors.toList()); + + if (!fieldsToRemove.isEmpty()) { + acroForm.flatten( + fieldsToRemove, + false); // Flatten the fields, effectively removing them + } + } } - mergedDoc.setDestinationFileName( - files[0].getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_merged.pdf"); - mergedDoc.setDestinationStream(docOutputstream); - - mergedDoc.mergeDocuments(null); + // Save the modified document to a new ByteArrayOutputStream + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + mergedDocument.save(baos); + String mergedFileName = + files[0].getOriginalFilename().replaceFirst("[.][^.]+$", "") + + "_merged_unsigned.pdf"; return WebResponseUtils.bytesToWebResponse( - docOutputstream.toByteArray(), mergedDoc.getDestinationFileName()); + baos.toByteArray(), mergedFileName); // Return the modified PDF + } catch (Exception ex) { logger.error("Error in merge pdf process", ex); throw ex; } finally { for (File file : filesToDelete) { if (file != null) { - Files.deleteIfExists(file.toPath()); + Files.deleteIfExists(file.toPath()); // Delete temporary files } } + docOutputstream.close(); + if (mergedDocument != null) { + mergedDocument.close(); // Close the merged document + } } } } diff --git a/src/main/java/stirling/software/SPDF/model/api/general/MergePdfsRequest.java b/src/main/java/stirling/software/SPDF/model/api/general/MergePdfsRequest.java index 0d0a98ae..ddb04295 100644 --- a/src/main/java/stirling/software/SPDF/model/api/general/MergePdfsRequest.java +++ b/src/main/java/stirling/software/SPDF/model/api/general/MergePdfsRequest.java @@ -21,4 +21,10 @@ public class MergePdfsRequest extends MultiplePDFFiles { }, defaultValue = "orderProvided") private String sortType = "orderProvided"; + + @Schema( + description = + "Flag indicating whether to remove certification signatures from the merged PDF. If true, all certification signatures will be removed from the final merged document.", + example = "true") + private boolean isRemoveCertSign; } diff --git a/src/main/resources/messages_ar_AR.properties b/src/main/resources/messages_ar_AR.properties index b9fb7ee4..e76801df 100644 --- a/src/main/resources/messages_ar_AR.properties +++ b/src/main/resources/messages_ar_AR.properties @@ -803,6 +803,7 @@ merge.title=دمج merge.header=دمج ملفات PDF متعددة (2+) merge.sortByName=Sort by name merge.sortByDate=Sort by date +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=دمج diff --git a/src/main/resources/messages_bg_BG.properties b/src/main/resources/messages_bg_BG.properties index 51631a21..1b35b1ca 100644 --- a/src/main/resources/messages_bg_BG.properties +++ b/src/main/resources/messages_bg_BG.properties @@ -803,6 +803,7 @@ merge.title=Обединяване merge.header=Обединяване на множество PDF файлове (2+) merge.sortByName=Сортиране по име merge.sortByDate=Сортиране по дата +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Обединяване diff --git a/src/main/resources/messages_ca_CA.properties b/src/main/resources/messages_ca_CA.properties index 6aba8157..6c15f5be 100644 --- a/src/main/resources/messages_ca_CA.properties +++ b/src/main/resources/messages_ca_CA.properties @@ -803,6 +803,7 @@ merge.title=Fusiona merge.header=Fusiona múltiples PDFs (2+) merge.sortByName=Sort by name merge.sortByDate=Sort by date +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Fusiona diff --git a/src/main/resources/messages_cs_CZ.properties b/src/main/resources/messages_cs_CZ.properties index 7f9ddb68..da388daa 100644 --- a/src/main/resources/messages_cs_CZ.properties +++ b/src/main/resources/messages_cs_CZ.properties @@ -803,6 +803,7 @@ merge.title=Sloučit merge.header=Sloučit více PDF (2+) merge.sortByName=Seřadit podle názvu merge.sortByDate=Seřadit podle data +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Sloučit diff --git a/src/main/resources/messages_de_DE.properties b/src/main/resources/messages_de_DE.properties index 5cbf08fd..cb84ecb0 100644 --- a/src/main/resources/messages_de_DE.properties +++ b/src/main/resources/messages_de_DE.properties @@ -803,6 +803,7 @@ merge.title=Zusammenführen merge.header=Mehrere PDFs zusammenführen (2+) merge.sortByName=Nach Namen sortieren merge.sortByDate=Nach Datum sortieren +merge.removeCertSign=Digitale Signatur in der zusammengeführten Datei entfernen? merge.submit=Zusammenführen diff --git a/src/main/resources/messages_el_GR.properties b/src/main/resources/messages_el_GR.properties index 71177790..ce684555 100644 --- a/src/main/resources/messages_el_GR.properties +++ b/src/main/resources/messages_el_GR.properties @@ -803,6 +803,7 @@ merge.title=Συγχώνευση merge.header=Συγχώνευση πολλαπλών PDFs (2+) merge.sortByName=Ταξινόμηση με βάση το Όνομα merge.sortByDate=Ταξινόμηση με βάση την Ημερομηνία +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Συγχώνευση diff --git a/src/main/resources/messages_en_GB.properties b/src/main/resources/messages_en_GB.properties index 6ad354ec..02875580 100644 --- a/src/main/resources/messages_en_GB.properties +++ b/src/main/resources/messages_en_GB.properties @@ -803,6 +803,7 @@ merge.title=Merge merge.header=Merge multiple PDFs (2+) merge.sortByName=Sort by name merge.sortByDate=Sort by date +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Merge diff --git a/src/main/resources/messages_en_US.properties b/src/main/resources/messages_en_US.properties index 5709509b..07d13f9f 100644 --- a/src/main/resources/messages_en_US.properties +++ b/src/main/resources/messages_en_US.properties @@ -803,6 +803,7 @@ merge.title=Merge merge.header=Merge multiple PDFs (2+) merge.sortByName=Sort by name merge.sortByDate=Sort by date +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Merge diff --git a/src/main/resources/messages_es_ES.properties b/src/main/resources/messages_es_ES.properties index 4fab713e..0e69ee55 100644 --- a/src/main/resources/messages_es_ES.properties +++ b/src/main/resources/messages_es_ES.properties @@ -803,6 +803,7 @@ merge.title=Unir merge.header=Unir múltiples PDFs (2+) merge.sortByName=Ordenar por nombre merge.sortByDate=Ordenar por fecha +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Unir diff --git a/src/main/resources/messages_eu_ES.properties b/src/main/resources/messages_eu_ES.properties index bff15fa9..8a9f7ede 100644 --- a/src/main/resources/messages_eu_ES.properties +++ b/src/main/resources/messages_eu_ES.properties @@ -803,6 +803,7 @@ merge.title=Elkartu merge.header=Elkartu zenbait PDF (2+) merge.sortByName=Sort by nameOrdenatu izenaren arabera merge.sortByDate=Ordenatu dataren arabera +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Elkartu diff --git a/src/main/resources/messages_fr_FR.properties b/src/main/resources/messages_fr_FR.properties index 30dd9fd9..1600e068 100644 --- a/src/main/resources/messages_fr_FR.properties +++ b/src/main/resources/messages_fr_FR.properties @@ -803,6 +803,7 @@ merge.title=Fusionner merge.header=Fusionner plusieurs PDF merge.sortByName=Trier par nom merge.sortByDate=Trier par date +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Fusionner diff --git a/src/main/resources/messages_hi_IN.properties b/src/main/resources/messages_hi_IN.properties index 8eae2d77..1610b17c 100644 --- a/src/main/resources/messages_hi_IN.properties +++ b/src/main/resources/messages_hi_IN.properties @@ -803,6 +803,7 @@ merge.title=मर्ज merge.header=एक से अधिक PDF एक साथ मर्ज करें (2+) merge.sortByName=नाम से क्रमबद्ध करें merge.sortByDate=तारीख से क्रमबद्ध करें +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=मर्ज करें diff --git a/src/main/resources/messages_hr_HR.properties b/src/main/resources/messages_hr_HR.properties index abe0f234..1ecc56b6 100644 --- a/src/main/resources/messages_hr_HR.properties +++ b/src/main/resources/messages_hr_HR.properties @@ -803,6 +803,7 @@ merge.title=Spajanje merge.header=Spajanje više PDF-ova (2+) merge.sortByName=Poredaj po imenu merge.sortByDate=Poredaj po datumu +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Spajanje diff --git a/src/main/resources/messages_hu_HU.properties b/src/main/resources/messages_hu_HU.properties index a58cbe73..9167e007 100644 --- a/src/main/resources/messages_hu_HU.properties +++ b/src/main/resources/messages_hu_HU.properties @@ -803,6 +803,7 @@ merge.title=Összevonás merge.header=Több PDF összevonása (2+) merge.sortByName=Név szerinti rendezés merge.sortByDate=Dátum szerinti rendezés +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Összevonás diff --git a/src/main/resources/messages_id_ID.properties b/src/main/resources/messages_id_ID.properties index b7be633b..9d64b6e9 100644 --- a/src/main/resources/messages_id_ID.properties +++ b/src/main/resources/messages_id_ID.properties @@ -803,6 +803,7 @@ merge.title=Gabungkan merge.header=Gabungkan beberapa PDFs (2+) merge.sortByName=Sortir berdasarkan nama merge.sortByDate=Sortir berdasrkan tanggal +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Gabungkan diff --git a/src/main/resources/messages_it_IT.properties b/src/main/resources/messages_it_IT.properties index 1687127c..5416a937 100644 --- a/src/main/resources/messages_it_IT.properties +++ b/src/main/resources/messages_it_IT.properties @@ -803,6 +803,7 @@ merge.title=Unisci merge.header=Unisci 2 o più PDF merge.sortByName=Ordina per nome merge.sortByDate=Ordina per data +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Unisci diff --git a/src/main/resources/messages_ja_JP.properties b/src/main/resources/messages_ja_JP.properties index 0da77329..b430a22f 100644 --- a/src/main/resources/messages_ja_JP.properties +++ b/src/main/resources/messages_ja_JP.properties @@ -803,6 +803,7 @@ merge.title=結合 merge.header=複数のPDFを結合 (2ファイル以上) merge.sortByName=名前で並べ替え merge.sortByDate=日付で並べ替え +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=結合 diff --git a/src/main/resources/messages_ko_KR.properties b/src/main/resources/messages_ko_KR.properties index 158e8e62..8829c326 100644 --- a/src/main/resources/messages_ko_KR.properties +++ b/src/main/resources/messages_ko_KR.properties @@ -803,6 +803,7 @@ merge.title=병합 merge.header=여러 개의 PDF 병합 (2개 이상) merge.sortByName=이름순 정렬 merge.sortByDate=날짜순 정렬 +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=병합 diff --git a/src/main/resources/messages_nl_NL.properties b/src/main/resources/messages_nl_NL.properties index d8c5c5ee..643f348a 100644 --- a/src/main/resources/messages_nl_NL.properties +++ b/src/main/resources/messages_nl_NL.properties @@ -803,6 +803,7 @@ merge.title=Samenvoegen merge.header=Meerdere PDF's samenvoegen (2+) merge.sortByName=Sorteer op naam merge.sortByDate=Sorteer op datum +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Samenvoegen diff --git a/src/main/resources/messages_no_NB.properties b/src/main/resources/messages_no_NB.properties index db6b5d09..51a1bef8 100644 --- a/src/main/resources/messages_no_NB.properties +++ b/src/main/resources/messages_no_NB.properties @@ -803,6 +803,7 @@ merge.title=Slå sammen merge.header=Slå sammen flere PDF-er (2+) merge.sortByName=Sorter etter navn merge.sortByDate=Sorter etter dato +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Slå sammen diff --git a/src/main/resources/messages_pl_PL.properties b/src/main/resources/messages_pl_PL.properties index 2a8c988f..35a3ace7 100644 --- a/src/main/resources/messages_pl_PL.properties +++ b/src/main/resources/messages_pl_PL.properties @@ -803,6 +803,7 @@ merge.title=Połącz merge.header=Połącz wiele dokumentów PDF (2+) merge.sortByName=Sort by name merge.sortByDate=Sort by date +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Połącz diff --git a/src/main/resources/messages_pt_BR.properties b/src/main/resources/messages_pt_BR.properties index 137efec7..39a656ca 100644 --- a/src/main/resources/messages_pt_BR.properties +++ b/src/main/resources/messages_pt_BR.properties @@ -803,6 +803,7 @@ merge.title=Mesclar merge.header=Mesclar Vários PDFs (2+) merge.sortByName=Sort by name merge.sortByDate=Sort by date +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Mesclar diff --git a/src/main/resources/messages_pt_PT.properties b/src/main/resources/messages_pt_PT.properties index a7d88eab..80ac3377 100644 --- a/src/main/resources/messages_pt_PT.properties +++ b/src/main/resources/messages_pt_PT.properties @@ -803,6 +803,7 @@ merge.title=Juntar merge.header=Juntar Vários PDFs (2+) merge.sortByName=Ordenar por nome merge.sortByDate=Ordenar por data +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Juntar diff --git a/src/main/resources/messages_ro_RO.properties b/src/main/resources/messages_ro_RO.properties index 96687f49..dfa54890 100644 --- a/src/main/resources/messages_ro_RO.properties +++ b/src/main/resources/messages_ro_RO.properties @@ -803,6 +803,7 @@ merge.title=Unire merge.header=Unirea mai multor PDF-uri (2+) merge.sortByName=Sort by name merge.sortByDate=Sort by date +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Unire diff --git a/src/main/resources/messages_ru_RU.properties b/src/main/resources/messages_ru_RU.properties index 48a35b52..05aa7bc1 100644 --- a/src/main/resources/messages_ru_RU.properties +++ b/src/main/resources/messages_ru_RU.properties @@ -803,6 +803,7 @@ merge.title=Объединить merge.header=Объединение нескольких PDF-файлов (2+) merge.sortByName=Сортировка по имени merge.sortByDate=Сортировка по дате +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Объединить diff --git a/src/main/resources/messages_sk_SK.properties b/src/main/resources/messages_sk_SK.properties index 1587585c..3bd2cecd 100644 --- a/src/main/resources/messages_sk_SK.properties +++ b/src/main/resources/messages_sk_SK.properties @@ -803,6 +803,7 @@ merge.title=Zlúčiť merge.header=Zlúčiť viacero PDF (2+) merge.sortByName=Zoradiť podľa názvu merge.sortByDate=Zoradiť podľa dátumu +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Zlúčiť diff --git a/src/main/resources/messages_sr_LATN_RS.properties b/src/main/resources/messages_sr_LATN_RS.properties index 0483960d..3ed747a7 100644 --- a/src/main/resources/messages_sr_LATN_RS.properties +++ b/src/main/resources/messages_sr_LATN_RS.properties @@ -803,6 +803,7 @@ merge.title=Spajanje merge.header=Spajanje više PDF fajlova (2+) merge.sortByName=Sortiraj po imenu merge.sortByDate=Sortiraj po datumu +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Spajanje diff --git a/src/main/resources/messages_sv_SE.properties b/src/main/resources/messages_sv_SE.properties index 71726d23..8cc8bdce 100644 --- a/src/main/resources/messages_sv_SE.properties +++ b/src/main/resources/messages_sv_SE.properties @@ -803,6 +803,7 @@ merge.title=Sammanfoga merge.header=Slå samman flera PDF-filer (2+) merge.sortByName=Sort by name merge.sortByDate=Sort by date +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Slå samman diff --git a/src/main/resources/messages_tr_TR.properties b/src/main/resources/messages_tr_TR.properties index b7b2141b..cb295c8f 100644 --- a/src/main/resources/messages_tr_TR.properties +++ b/src/main/resources/messages_tr_TR.properties @@ -803,6 +803,7 @@ merge.title=Birleştir merge.header=Çoklu PDF'leri Birleştir (2+) merge.sortByName=İsme göre sırala merge.sortByDate=Tarihe göre sırala +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Birleştir diff --git a/src/main/resources/messages_uk_UA.properties b/src/main/resources/messages_uk_UA.properties index 24fc81dc..2cf06e96 100644 --- a/src/main/resources/messages_uk_UA.properties +++ b/src/main/resources/messages_uk_UA.properties @@ -803,6 +803,7 @@ merge.title=Об'єднати merge.header=Об'єднання кількох PDF-файлів (2+) merge.sortByName=Сортування за ім'ям merge.sortByDate=Сортування за датою +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=Об'єднати diff --git a/src/main/resources/messages_zh_CN.properties b/src/main/resources/messages_zh_CN.properties index 39fbae25..66fb5c53 100644 --- a/src/main/resources/messages_zh_CN.properties +++ b/src/main/resources/messages_zh_CN.properties @@ -803,6 +803,7 @@ merge.title=合并 merge.header=合并多个PDF(2个以上)。 merge.sortByName=按名称排序 merge.sortByDate=按日期排序 +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=合并 diff --git a/src/main/resources/messages_zh_TW.properties b/src/main/resources/messages_zh_TW.properties index c0e0b125..2071c6c9 100644 --- a/src/main/resources/messages_zh_TW.properties +++ b/src/main/resources/messages_zh_TW.properties @@ -803,6 +803,7 @@ merge.title=合併 merge.header=合併多個 PDF merge.sortByName=依名稱排序 merge.sortByDate=依日期排序 +merge.removeCertSign=Remove digital signature in the merged file? merge.submit=合併 diff --git a/src/main/resources/templates/merge-pdfs.html b/src/main/resources/templates/merge-pdfs.html index 930c04c8..00401ea6 100644 --- a/src/main/resources/templates/merge-pdfs.html +++ b/src/main/resources/templates/merge-pdfs.html @@ -19,9 +19,13 @@
- +
+
+ + +