|
|
@@ -0,0 +1,51 @@
|
|
|
+package me.zhengjie.base.pdf;
|
|
|
+
|
|
|
+import org.apache.pdfbox.multipdf.Splitter;
|
|
|
+
|
|
|
+public class CustomPageSplitter extends Splitter {
|
|
|
+
|
|
|
+ private int[] splitLength;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * This will tell the splitting algorithm where to split the pages. The default
|
|
|
+ * is 1, so every page will become a new document. If it was two then each
|
|
|
+ * document would contain 2 pages. If the source document had 5 pages it would
|
|
|
+ * split into 3 new documents, 2 documents containing 2 pages and 1 document
|
|
|
+ * containing one page.
|
|
|
+ *
|
|
|
+ * @param split The number of pages each split document should contain.
|
|
|
+ * @throws IllegalArgumentException if the page is smaller than one.
|
|
|
+ */
|
|
|
+ public void setSplitAtPage(int... split) {
|
|
|
+ if (split == null || split.length <= 0) {
|
|
|
+ throw new IllegalArgumentException("Number of pages is smaller than one");
|
|
|
+ }
|
|
|
+ splitLength = split;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Check if it is necessary to create a new document. By default a split occurs
|
|
|
+ * at every page. If you wanted to split based on some complex logic then you
|
|
|
+ * could override this method. For example. <code>
|
|
|
+ * protected void splitAtPage()
|
|
|
+ * {
|
|
|
+ * // will split at pages with prime numbers only
|
|
|
+ * return isPrime(pageNumber);
|
|
|
+ * }
|
|
|
+ * </code>
|
|
|
+ *
|
|
|
+ * @param pageNumber the 0-based page number to be checked as splitting page
|
|
|
+ *
|
|
|
+ * @return true If a new document should be created.
|
|
|
+ */
|
|
|
+ protected boolean splitAtPage(int pageNumber) {
|
|
|
+ int result = 0;
|
|
|
+ for (int i = 0; i < splitLength.length; i++) {
|
|
|
+ result = splitLength[i] + result;
|
|
|
+ if (result == pageNumber) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|