diff --git a/src/main/java/stirling/software/SPDF/controller/api/SplitPdfByChaptersController.java b/src/main/java/stirling/software/SPDF/controller/api/SplitPdfByChaptersController.java index 9d03f8b6..0cca8f78 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/SplitPdfByChaptersController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/SplitPdfByChaptersController.java @@ -67,15 +67,6 @@ public class SplitPdfByChaptersController { } PDDocument sourceDocument = Loader.loadPDF(file.getBytes()); - // checks if the document is encrypted by an empty user password - if (sourceDocument.isEncrypted()) { - try { - sourceDocument.setAllSecurityToBeRemoved(true); - logger.info("Removing security from the source document "); - } catch (Exception e) { - logger.warn("Cannot decrypt the pdf"); - } - } PDDocumentOutline outline = sourceDocument.getDocumentCatalog().getDocumentOutline(); if (outline == null) { diff --git a/src/main/java/stirling/software/SPDF/service/CustomPDDocumentFactory.java b/src/main/java/stirling/software/SPDF/service/CustomPDDocumentFactory.java index b3f9eec7..2d3d8990 100644 --- a/src/main/java/stirling/software/SPDF/service/CustomPDDocumentFactory.java +++ b/src/main/java/stirling/software/SPDF/service/CustomPDDocumentFactory.java @@ -7,6 +7,8 @@ import java.io.InputStream; import org.apache.pdfbox.Loader; import org.apache.pdfbox.pdmodel.PDDocument; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; @@ -18,6 +20,8 @@ import stirling.software.SPDF.model.api.PDFFile; @Component public class CustomPDDocumentFactory { + private static final Logger logger = LoggerFactory.getLogger(CustomPDDocumentFactory.class); + private final PdfMetadataService pdfMetadataService; @Autowired @@ -71,6 +75,7 @@ public class CustomPDDocumentFactory { public PDDocument load(byte[] input) throws IOException { PDDocument document = Loader.loadPDF(input); pdfMetadataService.setDefaultMetadata(document); + removezeropassword(document); return document; } @@ -96,5 +101,17 @@ public class CustomPDDocumentFactory { return document; } + private PDDocument removezeropassword(PDDocument document) throws IOException { + if (document.isEncrypted()) { + try { + logger.info("Removing security from the source document"); + document.setAllSecurityToBeRemoved(true); + } catch (Exception e) { + logger.warn("Cannot decrypt the pdf"); + } + } + return document; + } + // Add other load methods as needed, following the same pattern }