diff --git a/src/main/java/stirling/software/SPDF/controller/api/RearrangePagesPDFController.java b/src/main/java/stirling/software/SPDF/controller/api/RearrangePagesPDFController.java index a1e80af3..3a9ed47b 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/RearrangePagesPDFController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/RearrangePagesPDFController.java @@ -145,6 +145,28 @@ public class RearrangePagesPDFController { return newPageOrder; } + /** + * Rearrange pages in a PDF file by merging odd and even pages. The first half of the pages will + * be the odd pages, and the second half will be the even pages as input.
+ * This method is visible for testing purposes only. + * + * @param totalPages Total number of pages in the PDF file. + * @return List of page numbers in the new order. The first page is 0. + */ + List oddEvenMerge(int totalPages) { + List newPageOrderZeroBased = new ArrayList<>(); + int numberOfOddPages = (totalPages + 1) / 2; + + for (int oneBasedIndex = 1; oneBasedIndex < (numberOfOddPages + 1); oneBasedIndex++) { + newPageOrderZeroBased.add((oneBasedIndex - 1)); + if (numberOfOddPages + oneBasedIndex <= totalPages) { + newPageOrderZeroBased.add((numberOfOddPages + oneBasedIndex - 1)); + } + } + + return newPageOrderZeroBased; + } + private List processSortTypes(String sortTypes, int totalPages) { try { SortTypes mode = SortTypes.valueOf(sortTypes.toUpperCase()); @@ -159,6 +181,8 @@ public class RearrangePagesPDFController { return sideStitchBooklet(totalPages); case ODD_EVEN_SPLIT: return oddEvenSplit(totalPages); + case ODD_EVEN_MERGE: + return oddEvenMerge(totalPages); case REMOVE_FIRST: return removeFirst(totalPages); case REMOVE_LAST: diff --git a/src/main/java/stirling/software/SPDF/model/SortTypes.java b/src/main/java/stirling/software/SPDF/model/SortTypes.java index eb10ff58..a7a699b8 100644 --- a/src/main/java/stirling/software/SPDF/model/SortTypes.java +++ b/src/main/java/stirling/software/SPDF/model/SortTypes.java @@ -6,6 +6,7 @@ public enum SortTypes { BOOKLET_SORT, SIDE_STITCH_BOOKLET_SORT, ODD_EVEN_SPLIT, + ODD_EVEN_MERGE, REMOVE_FIRST, REMOVE_LAST, REMOVE_FIRST_AND_LAST, diff --git a/src/main/java/stirling/software/SPDF/model/api/general/RearrangePagesRequest.java b/src/main/java/stirling/software/SPDF/model/api/general/RearrangePagesRequest.java index b20ced47..7ba2d84c 100644 --- a/src/main/java/stirling/software/SPDF/model/api/general/RearrangePagesRequest.java +++ b/src/main/java/stirling/software/SPDF/model/api/general/RearrangePagesRequest.java @@ -19,6 +19,7 @@ public class RearrangePagesRequest extends PDFWithPageNums { + "DUPLEX_SORT: Sorts pages as if all fronts were scanned then all backs in reverse (1, n, 2, n-1, ...). " + "BOOKLET_SORT: Arranges pages for booklet printing (last, first, second, second last, ...).\n" + "ODD_EVEN_SPLIT: Splits and arranges pages into odd and even numbered pages.\n" + + "ODD_EVEN_MERGE: Merges pages and organises them alternately into odd and even pages.\n" + "REMOVE_FIRST: Removes the first page.\n" + "REMOVE_LAST: Removes the last page.\n" + "REMOVE_FIRST_AND_LAST: Removes both the first and the last pages.\n") diff --git a/src/main/resources/messages_de_DE.properties b/src/main/resources/messages_de_DE.properties index d19c4c43..e685e7f5 100644 --- a/src/main/resources/messages_de_DE.properties +++ b/src/main/resources/messages_de_DE.properties @@ -821,6 +821,7 @@ pdfOrganiser.mode.6=Ungerade-Gerade-Teilung pdfOrganiser.mode.7=Erste entfernen pdfOrganiser.mode.8=Letzte entfernen pdfOrganiser.mode.9=Erste und letzte entfernen +pdfOrganiser.mode.10=Ungerade-Gerade-Zusammenführung pdfOrganiser.placeholder=(z.B. 1,3,2 oder 4-8,2,10-12 oder 2n-1) diff --git a/src/main/resources/messages_en_GB.properties b/src/main/resources/messages_en_GB.properties index 4e1260cb..d8d3e148 100644 --- a/src/main/resources/messages_en_GB.properties +++ b/src/main/resources/messages_en_GB.properties @@ -821,6 +821,7 @@ pdfOrganiser.mode.6=Odd-Even Split pdfOrganiser.mode.7=Remove First pdfOrganiser.mode.8=Remove Last pdfOrganiser.mode.9=Remove First and Last +pdfOrganiser.mode.10=Odd-Even Merge pdfOrganiser.placeholder=(e.g. 1,3,2 or 4-8,2,10-12 or 2n-1) diff --git a/src/main/resources/messages_en_US.properties b/src/main/resources/messages_en_US.properties index 3e59bf67..45d6352a 100644 --- a/src/main/resources/messages_en_US.properties +++ b/src/main/resources/messages_en_US.properties @@ -821,6 +821,7 @@ pdfOrganiser.mode.6=Odd-Even Split pdfOrganiser.mode.7=Remove First pdfOrganiser.mode.8=Remove Last pdfOrganiser.mode.9=Remove First and Last +pdfOrganiser.mode.10=Odd-Even Merge pdfOrganiser.placeholder=(e.g. 1,3,2 or 4-8,2,10-12 or 2n-1) diff --git a/src/main/resources/templates/pdf-organizer.html b/src/main/resources/templates/pdf-organizer.html index 85b4acab..7896f18d 100644 --- a/src/main/resources/templates/pdf-organizer.html +++ b/src/main/resources/templates/pdf-organizer.html @@ -28,6 +28,7 @@ + diff --git a/src/test/java/stirling/software/SPDF/controller/api/RearrangePagesPDFControllerTest.java b/src/test/java/stirling/software/SPDF/controller/api/RearrangePagesPDFControllerTest.java new file mode 100644 index 00000000..209c7f54 --- /dev/null +++ b/src/test/java/stirling/software/SPDF/controller/api/RearrangePagesPDFControllerTest.java @@ -0,0 +1,82 @@ +package stirling.software.SPDF.controller.api; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class RearrangePagesPDFControllerTest { + + /** + * Tests the behavior of the oddEvenMerge method when there are no pages in the document. + */ + @Test + void oddEvenMerge_noPages() { + RearrangePagesPDFController sut = new RearrangePagesPDFController(); + int totalNumberOfPages = 0; + + List newPageOrder = sut.oddEvenMerge(totalNumberOfPages); + + assertNotNull(newPageOrder, "Returning null instead of page order list"); + assertEquals(List.of(), newPageOrder, "Page order doesn't match"); + } + + /** + * Tests the behavior of the oddEvenMerge method when there are odd total pages in the document. + */ + @Test + void oddEvenMerge_oddTotalPageNumber() { + RearrangePagesPDFController sut = new RearrangePagesPDFController(); + int totalNumberOfPages = 5; + + List newPageOrder = sut.oddEvenMerge(totalNumberOfPages); + + assertNotNull(newPageOrder, "Returning null instead of page order list"); + assertEquals(Arrays.asList(0,3,1,4,2), newPageOrder, "Page order doesn't match"); + } + + /** + * Tests the behavior of the oddEvenMerge method when there are even total pages in the document. + */ + @Test + void oddEvenMerge_evenTotalPageNumber() { + RearrangePagesPDFController sut = new RearrangePagesPDFController(); + int totalNumberOfPages = 6; + + List newPageOrder = sut.oddEvenMerge(totalNumberOfPages); + + assertNotNull(newPageOrder, "Returning null instead of page order list"); + assertEquals(Arrays.asList(0,3,1,4,2,5), newPageOrder, "Page order doesn't match"); + } + + /** + * Tests the behavior of the oddEvenMerge method with multiple test cases of multiple pages. + * @param totalNumberOfPages The total number of pages in the document. + * @param expectedPageOrder The expected order of the pages after rearranging. + */ + @ParameterizedTest + @CsvSource({ + "1, '0'", + "2, '0,1'", + "3, '0,2,1'", + "4, '0,2,1,3'", + "5, '0,3,1,4,2'", + "6, '0,3,1,4,2,5'", + "10, '0,5,1,6,2,7,3,8,4,9'", + "50, '0,25,1,26,2,27,3,28,4,29,5,30,6,31,7,32,8,33,9,34,10,35," + + "11,36,12,37,13,38,14,39,15,40,16,41,17,42,18,43,19,44,20,45,21,46," + + "22,47,23,48,24,49'" + }) + void oddEvenMerge_multi_test(int totalNumberOfPages, String expectedPageOrder) { + RearrangePagesPDFController sut = new RearrangePagesPDFController(); + + List newPageOrder = sut.oddEvenMerge(totalNumberOfPages); + + assertNotNull(newPageOrder, "Returning null instead of page order list"); + assertEquals(Arrays.stream(expectedPageOrder.split(",")).map(Integer::parseInt).toList(), newPageOrder, "Page order doesn't match"); + } +} \ No newline at end of file