diff --git a/pdf-toolkit-repo/src/main/java/org/alfresco/extension/pdftoolkit/service/PDFToolkitServiceImpl.java b/pdf-toolkit-repo/src/main/java/org/alfresco/extension/pdftoolkit/service/PDFToolkitServiceImpl.java index 154813e..7d8c3c5 100644 --- a/pdf-toolkit-repo/src/main/java/org/alfresco/extension/pdftoolkit/service/PDFToolkitServiceImpl.java +++ b/pdf-toolkit-repo/src/main/java/org/alfresco/extension/pdftoolkit/service/PDFToolkitServiceImpl.java @@ -53,6 +53,7 @@ import org.apache.pdfbox.multipdf.PDFMergerUtility; import org.apache.pdfbox.multipdf.Splitter; import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException; import org.springframework.extensions.surf.util.I18NUtil; import com.itextpdf.text.Document; @@ -125,6 +126,9 @@ public NodeRef appendPDF(NodeRef targetNodeRef, Map params pdf = PDDocument.load(is); pdfTarget = PDDocument.load(tis); + pdf = decryptPdf(pdfTarget, is); + pdfTarget = decryptPdf(pdfTarget, tis); + // Append the PDFs PDFMergerUtility merger = new PDFMergerUtility(); merger.appendDocument(pdfTarget, pdf); @@ -216,6 +220,17 @@ public NodeRef appendPDF(NodeRef targetNodeRef, Map params return destinationNode; } + + private PDDocument decryptPdf(PDDocument pdf, InputStream tis) throws IOException, InvalidPasswordException { + // if the document is encrypted, try to remove its protection by using the "" (EMPTY) password + // as an attempt before failing during the append operation + if(pdf.isEncrypted()) { + pdf.close(); + pdf = PDDocument.load(tis, ""); + pdf.setAllSecurityToBeRemoved(true); + } + return pdf; + } @Override public NodeRef encryptPDF(NodeRef targetNodeRef, Map params)