From 2afff669fa8a45fb78707821da3f7af0bd670484 Mon Sep 17 00:00:00 2001 From: Skillkiller Date: Wed, 12 Jun 2024 18:20:18 +0200 Subject: [PATCH 1/6] Add ODD_EVEN_MERGE sort type --- src/main/java/stirling/software/SPDF/model/SortTypes.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/stirling/software/SPDF/model/SortTypes.java b/src/main/java/stirling/software/SPDF/model/SortTypes.java index eb10ff580b1..a7a699b87e9 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, From 4d621f85dfefb0064ff5bc7b41226525d2ee59c2 Mon Sep 17 00:00:00 2001 From: Skillkiller Date: Wed, 12 Jun 2024 18:30:37 +0200 Subject: [PATCH 2/6] Add process method to merge odd and even PDF pages --- .../api/RearrangePagesPDFController.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) 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 a1e80af33a6..3a9ed47b115 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 @@ private List oddEvenSplit(int totalPages) { 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 @@ private List processSortTypes(String sortTypes, int totalPages) { 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: From 574abb993a9d343fb54fcbbed3cd0436b0d8c25c Mon Sep 17 00:00:00 2001 From: Skillkiller Date: Wed, 12 Jun 2024 18:38:08 +0200 Subject: [PATCH 3/6] Add test cases for Odd-Even merge method --- .../api/RearrangePagesPDFControllerTest.java | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/test/java/stirling/software/SPDF/controller/api/RearrangePagesPDFControllerTest.java 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 00000000000..209c7f541b3 --- /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 From 1d05e6bd77ec6549068062f28955a640bb824bb1 Mon Sep 17 00:00:00 2001 From: Skillkiller Date: Wed, 12 Jun 2024 18:41:15 +0200 Subject: [PATCH 4/6] Add Odd-Even Merge mode in PDF Organizer webpage This also add a new translatable text message variable pdfOrganiser.mode.10 with translation for english and german --- src/main/resources/messages_de_DE.properties | 1 + src/main/resources/messages_en_US.properties | 1 + src/main/resources/templates/pdf-organizer.html | 1 + 3 files changed, 3 insertions(+) diff --git a/src/main/resources/messages_de_DE.properties b/src/main/resources/messages_de_DE.properties index cb84ecb0a8c..8d0bc6cb48d 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_US.properties b/src/main/resources/messages_en_US.properties index 07d13f9fa28..fdad5ad4f55 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 85b4acab63c..7896f18dcd6 100644 --- a/src/main/resources/templates/pdf-organizer.html +++ b/src/main/resources/templates/pdf-organizer.html @@ -28,6 +28,7 @@ + From 263b49121191fb5494a62f4e3da6e9440d859740 Mon Sep 17 00:00:00 2001 From: Skillkiller Date: Wed, 12 Jun 2024 18:55:47 +0200 Subject: [PATCH 5/6] Add ODD_EVEN_MERGE documentation to RearrangePagesRequest --- .../software/SPDF/model/api/general/RearrangePagesRequest.java | 1 + 1 file changed, 1 insertion(+) 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 b20ced476a9..7ba2d84cec1 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") From ef66aef8ffd8d441ee05c52088b1b274c367598d Mon Sep 17 00:00:00 2001 From: Skillkiller Date: Wed, 12 Jun 2024 21:59:54 +0200 Subject: [PATCH 6/6] Add english translation for pdfOrganiser.mode.10 --- src/main/resources/messages_en_GB.properties | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/resources/messages_en_GB.properties b/src/main/resources/messages_en_GB.properties index 02875580527..34c45a2966f 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)