From 4ccaccd104c1b96921458d2d8cb01d5e56b59cce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vincent=20Rivi=C3=A8re?= Date: Thu, 6 Jun 2024 19:05:34 +0200 Subject: [PATCH] Supprimer les signatures des PJ lors de la fusion --- .../controllers/CandidatureController.java | 3 +- .../ecandidat/utils/PDFMergerUtilityEx.java | 114 ++++++++++++++++++ 2 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 src/main/java/fr/univlorraine/ecandidat/utils/PDFMergerUtilityEx.java diff --git a/src/main/java/fr/univlorraine/ecandidat/controllers/CandidatureController.java b/src/main/java/fr/univlorraine/ecandidat/controllers/CandidatureController.java index 9e76429c..a9891fa2 100644 --- a/src/main/java/fr/univlorraine/ecandidat/controllers/CandidatureController.java +++ b/src/main/java/fr/univlorraine/ecandidat/controllers/CandidatureController.java @@ -103,6 +103,7 @@ import fr.univlorraine.ecandidat.utils.ListenerUtils.OdfListener; import fr.univlorraine.ecandidat.utils.MethodUtils; import fr.univlorraine.ecandidat.utils.NomenclatureUtils; +import fr.univlorraine.ecandidat.utils.PDFMergerUtilityEx; import fr.univlorraine.ecandidat.utils.bean.export.ExportDossierAvis; import fr.univlorraine.ecandidat.utils.bean.export.ExportDossierBac; import fr.univlorraine.ecandidat.utils.bean.export.ExportDossierCandidat; @@ -1675,7 +1676,7 @@ public OnDemandFile downloadDossier(final Candidature candidature, InputStream is = null; try { /* Merger */ - final PDFMergerUtility ut = new PDFMergerUtility(); + final PDFMergerUtility ut = new PDFMergerUtilityEx(); /* Propriétés du document */ final PDDocumentInformation info = new PDDocumentInformation(); diff --git a/src/main/java/fr/univlorraine/ecandidat/utils/PDFMergerUtilityEx.java b/src/main/java/fr/univlorraine/ecandidat/utils/PDFMergerUtilityEx.java new file mode 100644 index 00000000..5a9e0658 --- /dev/null +++ b/src/main/java/fr/univlorraine/ecandidat/utils/PDFMergerUtilityEx.java @@ -0,0 +1,114 @@ +/** + * ESUP-Portail eCandidat - Copyright (c) 2016 ESUP-Portail consortium + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package fr.univlorraine.ecandidat.utils; + +import java.io.IOException; +import org.apache.pdfbox.cos.COSArray; +import org.apache.pdfbox.cos.COSBase; +import org.apache.pdfbox.cos.COSDictionary; +import org.apache.pdfbox.cos.COSName; +import org.apache.pdfbox.cos.COSObject; +import org.apache.pdfbox.multipdf.PDFMergerUtility; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.PDPage; +import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm; + +/** + * + * @author vriviere + */ +public class PDFMergerUtilityEx extends PDFMergerUtility { + + private static void removeAllDigitalSignatures(PDDocument doc) { + COSDictionary catalogDict = doc.getDocumentCatalog().getCOSObject(); + catalogDict.removeItem(COSName.PERMS); + catalogDict.removeItem(COSName.getPDFName("DSS")); + } + + private static void removeAllSignatureFields(PDDocument doc) { + PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm(null); + if (acroForm == null) + return; + + COSArray arrayFields = acroForm.getCOSObject().getCOSArray(COSName.FIELDS); + if (arrayFields == null) + return; + + for (var iter = arrayFields.iterator(); iter.hasNext(); ) { + COSBase baseRefField = iter.next(); + if (!(baseRefField instanceof COSObject)) + continue; + COSObject objRefField = (COSObject)baseRefField; + + COSBase baseField = objRefField.getObject(); + if (!(baseField instanceof COSDictionary)) + continue; + COSDictionary objField = (COSDictionary)baseField; + + COSName fieldType = objField.getCOSName(COSName.FT); + if (fieldType != null && fieldType.equals(COSName.SIG)) + iter.remove(); + } + } + + private static void removeAllVisibleSignatures(PDDocument doc) { + for (PDPage page : doc.getPages()) { + COSBase baseAnnotations = page.getCOSObject().getDictionaryObject(COSName.ANNOTS); + if (!(baseAnnotations instanceof COSArray)) + continue; + COSArray arrayAnnotations = (COSArray)baseAnnotations; + + for (var iter = arrayAnnotations.iterator(); iter.hasNext(); ) { + COSBase baseRefAnnotation = iter.next(); + if (!(baseRefAnnotation instanceof COSObject)) + continue; + + COSObject objRefAnnotation = (COSObject)baseRefAnnotation; + COSBase baseAnnotation = objRefAnnotation.getObject(); + if (!(baseAnnotation instanceof COSDictionary)) + continue; + + COSDictionary dictAnnotation = (COSDictionary)baseAnnotation; + COSName fieldType = dictAnnotation.getCOSName(COSName.FT); + if (fieldType != null && fieldType.equals(COSName.SIG)) + iter.remove(); + } + } + } + + private static void removeSigFlags(PDDocument doc) { + PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm(null); + if (acroForm == null) + return; + + acroForm.getCOSObject().removeItem(COSName.SIG_FLAGS); + } + + public static void removeAllSignatures(PDDocument doc) { + doc.setAllSecurityToBeRemoved(true); + removeAllDigitalSignatures(doc); + removeAllSignatureFields(doc); + removeAllVisibleSignatures(doc); + removeSigFlags(doc); + } + + @Override + public void appendDocument(PDDocument destination, PDDocument source) throws IOException { + removeAllSignatures(source); + super.appendDocument(destination, source); + } +}