diff --git a/app/core/src/main/java/stirling/software/SPDF/controller/api/SplitPdfBySectionsController.java b/app/core/src/main/java/stirling/software/SPDF/controller/api/SplitPdfBySectionsController.java index c2bbd31b5..f787831e9 100644 --- a/app/core/src/main/java/stirling/software/SPDF/controller/api/SplitPdfBySectionsController.java +++ b/app/core/src/main/java/stirling/software/SPDF/controller/api/SplitPdfBySectionsController.java @@ -4,8 +4,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; +import java.util.*; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -31,8 +30,10 @@ import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; +import stirling.software.SPDF.model.SplitTypes; import stirling.software.SPDF.model.api.SplitPdfBySectionsRequest; import stirling.software.common.service.CustomPDFDocumentFactory; +import stirling.software.common.util.GeneralUtils; import stirling.software.common.util.WebResponseUtils; @RestController @@ -48,20 +49,30 @@ public class SplitPdfBySectionsController { summary = "Split PDF pages into smaller sections", description = "Split each page of a PDF into smaller sections based on the user's choice" - + " (halves, thirds, quarters, etc.), both vertically and horizontally." + + " which page to split, and how to split" + + " ( halves, thirds, quarters, etc.), both vertically and horizontally." + " Input:PDF Output:ZIP-PDF Type:SISO") public ResponseEntity splitPdf(@ModelAttribute SplitPdfBySectionsRequest request) throws Exception { List splitDocumentsBoas = new ArrayList<>(); MultipartFile file = request.getFileInput(); + String pageNumbers = request.getPageNumbers(); + SplitTypes splitMode = + Optional.ofNullable(request.getSplitMode()) + .map(SplitTypes::valueOf) + .orElse(SplitTypes.SPLIT_ALL); + PDDocument sourceDocument = pdfDocumentFactory.load(file); + Set pagesToSplit = + getPagesToSplit(pageNumbers, splitMode, sourceDocument.getNumberOfPages()); + // Process the PDF based on split parameters int horiz = request.getHorizontalDivisions() + 1; int verti = request.getVerticalDivisions() + 1; boolean merge = Boolean.TRUE.equals(request.getMerge()); - List splitDocuments = splitPdfPages(sourceDocument, verti, horiz); + List splitDocuments = splitPdfPages(sourceDocument, verti, horiz, pagesToSplit); String filename = Filenames.toSimpleFileName(file.getOriginalFilename()) @@ -109,52 +120,111 @@ public class SplitPdfBySectionsController { } } + // Based on the mode, get the pages that need to be split and return the pages set + private Set getPagesToSplit(String pageNumbers, SplitTypes splitMode, int totalPages) { + Set pagesToSplit = new HashSet<>(); + + switch (splitMode) { + case CUSTOM: + if (pageNumbers == null || pageNumbers.isBlank()) { + throw new IllegalArgumentException("Custom mode requires page numbers input."); + } + String[] pageOrderArr = pageNumbers.split(","); + List pageListToSplit = + GeneralUtils.parsePageList(pageOrderArr, totalPages, false); + pagesToSplit.addAll(pageListToSplit); + break; + + case SPLIT_ALL: + for (int i = 0; i < totalPages; i++) { + pagesToSplit.add(i); + } + break; + + case SPLIT_ALL_EXCEPT_FIRST: + for (int i = 1; i < totalPages; i++) { + pagesToSplit.add(i); + } + break; + + case SPLIT_ALL_EXCEPT_LAST: + for (int i = 0; i < totalPages - 1; i++) { + pagesToSplit.add(i); + } + break; + + case SPLIT_ALL_EXCEPT_FIRST_AND_LAST: + for (int i = 1; i < totalPages - 1; i++) { + pagesToSplit.add(i); + } + break; + + default: + throw new IllegalArgumentException("Unsupported split mode: " + splitMode); + } + + return pagesToSplit; + } + public List splitPdfPages( - PDDocument document, int horizontalDivisions, int verticalDivisions) + PDDocument document, + int horizontalDivisions, + int verticalDivisions, + Set pagesToSplit) throws IOException { List splitDocuments = new ArrayList<>(); + int pageIndex = 0; for (PDPage originalPage : document.getPages()) { - PDRectangle originalMediaBox = originalPage.getMediaBox(); - float width = originalMediaBox.getWidth(); - float height = originalMediaBox.getHeight(); - float subPageWidth = width / horizontalDivisions; - float subPageHeight = height / verticalDivisions; + // If current page is not to split, add it to the splitDocuments directly. + if (!pagesToSplit.contains(pageIndex)) { + PDDocument newDoc = pdfDocumentFactory.createNewDocument(); + newDoc.addPage(originalPage); + splitDocuments.add(newDoc); + } else { + // Otherwise, split current page. + PDRectangle originalMediaBox = originalPage.getMediaBox(); + float width = originalMediaBox.getWidth(); + float height = originalMediaBox.getHeight(); + float subPageWidth = width / horizontalDivisions; + float subPageHeight = height / verticalDivisions; - LayerUtility layerUtility = new LayerUtility(document); + LayerUtility layerUtility = new LayerUtility(document); - for (int i = 0; i < horizontalDivisions; i++) { - for (int j = 0; j < verticalDivisions; j++) { - PDDocument subDoc = new PDDocument(); - PDPage subPage = new PDPage(new PDRectangle(subPageWidth, subPageHeight)); - subDoc.addPage(subPage); + for (int i = 0; i < horizontalDivisions; i++) { + for (int j = 0; j < verticalDivisions; j++) { + PDDocument subDoc = new PDDocument(); + PDPage subPage = new PDPage(new PDRectangle(subPageWidth, subPageHeight)); + subDoc.addPage(subPage); - PDFormXObject form = - layerUtility.importPageAsForm( - document, document.getPages().indexOf(originalPage)); + PDFormXObject form = + layerUtility.importPageAsForm( + document, document.getPages().indexOf(originalPage)); - try (PDPageContentStream contentStream = - new PDPageContentStream( - subDoc, subPage, AppendMode.APPEND, true, true)) { - // Set clipping area and position - float translateX = -subPageWidth * i; + try (PDPageContentStream contentStream = + new PDPageContentStream( + subDoc, subPage, AppendMode.APPEND, true, true)) { + // Set clipping area and position + float translateX = -subPageWidth * i; - // float translateY = height - subPageHeight * (verticalDivisions - j); - float translateY = -subPageHeight * (verticalDivisions - 1 - j); + // float translateY = height - subPageHeight * (verticalDivisions - j); + float translateY = -subPageHeight * (verticalDivisions - 1 - j); - contentStream.saveGraphicsState(); - contentStream.addRect(0, 0, subPageWidth, subPageHeight); - contentStream.clip(); - contentStream.transform(new Matrix(1, 0, 0, 1, translateX, translateY)); + contentStream.saveGraphicsState(); + contentStream.addRect(0, 0, subPageWidth, subPageHeight); + contentStream.clip(); + contentStream.transform(new Matrix(1, 0, 0, 1, translateX, translateY)); - // Draw the form - contentStream.drawForm(form); - contentStream.restoreGraphicsState(); + // Draw the form + contentStream.drawForm(form); + contentStream.restoreGraphicsState(); + } + + splitDocuments.add(subDoc); } - - splitDocuments.add(subDoc); } } + pageIndex++; } return splitDocuments; diff --git a/app/core/src/main/java/stirling/software/SPDF/model/SplitTypes.java b/app/core/src/main/java/stirling/software/SPDF/model/SplitTypes.java new file mode 100644 index 000000000..19c1b5a5b --- /dev/null +++ b/app/core/src/main/java/stirling/software/SPDF/model/SplitTypes.java @@ -0,0 +1,9 @@ +package stirling.software.SPDF.model; + +public enum SplitTypes { + CUSTOM, + SPLIT_ALL_EXCEPT_FIRST_AND_LAST, + SPLIT_ALL_EXCEPT_FIRST, + SPLIT_ALL_EXCEPT_LAST, + SPLIT_ALL +} diff --git a/app/core/src/main/java/stirling/software/SPDF/model/api/SplitPdfBySectionsRequest.java b/app/core/src/main/java/stirling/software/SPDF/model/api/SplitPdfBySectionsRequest.java index 3a89ab686..c22135bbc 100644 --- a/app/core/src/main/java/stirling/software/SPDF/model/api/SplitPdfBySectionsRequest.java +++ b/app/core/src/main/java/stirling/software/SPDF/model/api/SplitPdfBySectionsRequest.java @@ -5,11 +5,29 @@ import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; import lombok.EqualsAndHashCode; +import stirling.software.SPDF.model.SplitTypes; import stirling.software.common.model.api.PDFFile; @Data @EqualsAndHashCode(callSuper = true) public class SplitPdfBySectionsRequest extends PDFFile { + @Schema( + description = "Pages to be split by section", + defaultValue = "all", + requiredMode = Schema.RequiredMode.REQUIRED) + private String pageNumbers; + + @Schema( + implementation = SplitTypes.class, + description = + "Modes for page split. Valid values are:\n" + + "SPLIT_ALL_EXCEPT_FIRST_AND_LAST: Splits all except the first and the last pages.\n" + + "SPLIT_ALL_EXCEPT_FIRST: Splits all except the first page.\n" + + "SPLIT_ALL_EXCEPT_LAST: Splits all except the last page.\n" + + "SPLIT_ALL: Splits all pages.\n" + + "CUSTOM: Custom split.\n") + private String splitMode; + @Schema( description = "Number of horizontal divisions for each PDF page", defaultValue = "0", diff --git a/app/core/src/main/resources/messages_en_GB.properties b/app/core/src/main/resources/messages_en_GB.properties index d6056e856..8e12f6b11 100644 --- a/app/core/src/main/resources/messages_en_GB.properties +++ b/app/core/src/main/resources/messages_en_GB.properties @@ -838,8 +838,8 @@ home.overlay-pdfs.desc=Overlays PDFs on-top of another PDF overlay-pdfs.tags=Overlay home.split-by-sections.title=Split PDF by Sections -home.split-by-sections.desc=Divide each page of a PDF into smaller horizontal and vertical sections -split-by-sections.tags=Section Split, Divide, Customize,Customise +home.split-by-sections.desc=Split the content of specified PDF pages into smaller horizontal and vertical sections. +split-by-sections.tags=Section Split, Divide, Customize, Customise home.AddStampRequest.title=Add Stamp to PDF home.AddStampRequest.desc=Add text or add image stamps at set locations @@ -1619,6 +1619,15 @@ split-by-sections.horizontal.placeholder=Enter number of horizontal divisions split-by-sections.vertical.placeholder=Enter number of vertical divisions split-by-sections.submit=Split PDF split-by-sections.merge=Merge Into One PDF +split-by-sections.pageToSplit=Pages to split (Enter a comma-separated list of page numbers) : +split-by-sections.pageToSplit.placeholder=(e.g. 1,2,6) +split-by-sections.mode=Split Mode +split-by-sections.mode.1=Split All Except First & Last +split-by-sections.mode.2=Split All Except First +split-by-sections.mode.3=Split All Except Last +split-by-sections.mode.4=Split All Pages +split-by-sections.mode.5=Custom: Specify Pages + #printFile diff --git a/app/core/src/main/resources/static/css/split-pdf-by-sections.css b/app/core/src/main/resources/static/css/split-pdf-by-sections.css index 7520c10e5..b949000af 100644 --- a/app/core/src/main/resources/static/css/split-pdf-by-sections.css +++ b/app/core/src/main/resources/static/css/split-pdf-by-sections.css @@ -8,3 +8,6 @@ position: absolute; background-color: red; /* Line color */ } +#pageToSplitSection { + display: none; +} diff --git a/app/core/src/main/resources/templates/pdf-organizer.html b/app/core/src/main/resources/templates/pdf-organizer.html index 746a6bafd..8e2937eea 100644 --- a/app/core/src/main/resources/templates/pdf-organizer.html +++ b/app/core/src/main/resources/templates/pdf-organizer.html @@ -42,22 +42,29 @@ -
+
+ th:placeholder="#{pdfOrganiser.placeholder}">
diff --git a/app/core/src/main/resources/templates/split-pdf-by-sections.html b/app/core/src/main/resources/templates/split-pdf-by-sections.html index 1f94d8a23..e795738a3 100644 --- a/app/core/src/main/resources/templates/split-pdf-by-sections.html +++ b/app/core/src/main/resources/templates/split-pdf-by-sections.html @@ -20,6 +20,21 @@
+
+ + +
+
+ + +

@@ -74,6 +89,28 @@ // Initial draw updateVisualAid(); + + // Removes all whitespace characters (spaces, tabs, newlines) as the user types + document.getElementById('pageToSplit').addEventListener('input', function () { + this.value = this.value.replace(/\s+/g, '');; + }); + + // Only display the page input field when split mode is custom. + function togglePageInput() { + const mode = document.getElementById('splitMode').value; + const pageInputSection = document.getElementById('pageToSplitSection'); + if (mode === "CUSTOM") { + pageInputSection.style.display = "block"; + document.getElementById('pageToSplit').setAttribute("required", "true"); + } else { + pageInputSection.style.display = "none"; + document.getElementById('pageToSplit').removeAttribute("required"); + } + } + + document.getElementById('splitMode').addEventListener('change', togglePageInput); + window.addEventListener('DOMContentLoaded', togglePageInput); +