|
44 | 44 | import java.io.BufferedReader; |
45 | 45 | import java.io.ByteArrayOutputStream; |
46 | 46 | import java.io.File; |
| 47 | +import java.io.FileNotFoundException; |
47 | 48 | import java.io.FileOutputStream; |
48 | 49 | import java.io.IOException; |
49 | 50 | import java.io.InputStream; |
50 | 51 | import java.io.InputStreamReader; |
51 | 52 | import java.util.ArrayList; |
| 53 | +import java.util.Arrays; |
52 | 54 |
|
53 | 55 | import swati4star.createpdf.R; |
54 | 56 | import swati4star.createpdf.database.DatabaseHelper; |
|
59 | 61 | import static swati4star.createpdf.util.Constants.MASTER_PWD_STRING; |
60 | 62 | import static swati4star.createpdf.util.Constants.STORAGE_LOCATION; |
61 | 63 | import static swati4star.createpdf.util.Constants.appName; |
| 64 | +import static swati4star.createpdf.util.Constants.pdfExtension; |
62 | 65 | import static swati4star.createpdf.util.DialogUtils.createCustomDialogWithoutContent; |
63 | 66 | import static swati4star.createpdf.util.StringUtils.getDefaultStorageLocation; |
64 | 67 | import static swati4star.createpdf.util.StringUtils.getSnackbarwithAction; |
@@ -118,7 +121,7 @@ public void showDetails(File file) { |
118 | 121 | * Create a PDF from a Text File |
119 | 122 | * |
120 | 123 | * @param mTextToPDFOptions TextToPDFOptions Object |
121 | | - * @param fileExtension file extension represented as string |
| 124 | + * @param fileExtension file extension represented as string |
122 | 125 | */ |
123 | 126 | public void createPdf(TextToPDFOptions mTextToPDFOptions, String fileExtension) |
124 | 127 | throws DocumentException, IOException { |
@@ -430,8 +433,9 @@ protected void onPostExecute(String s) { |
430 | 433 |
|
431 | 434 | /** |
432 | 435 | * Main function to add images to PDF |
| 436 | + * |
433 | 437 | * @param inputPath - path of input PDF |
434 | | - * @param output - path of output PDF |
| 438 | + * @param output - path of output PDF |
435 | 439 | * @param imagesUri - list of images to add |
436 | 440 | * @return true, if succeeded, otherwise false |
437 | 441 | */ |
@@ -504,25 +508,61 @@ public boolean reorderRemovePDF(String inputPath, String output, String pages) { |
504 | 508 | } |
505 | 509 | } |
506 | 510 |
|
507 | | - public ArrayList<String> splitPDF(String path) { |
| 511 | + /** |
| 512 | + * Breaks up the splitDetail String into ranges where a "," |
| 513 | + * is found |
| 514 | + * @param path the input pdf path |
| 515 | + * @param splitDetail string that contains split configuration |
| 516 | + * @return |
| 517 | + */ |
| 518 | + public ArrayList<String> splitPDFByConfig(String path, String splitDetail) { |
| 519 | + String splitConfig = splitDetail.replaceAll("\\s+", ""); |
508 | 520 | ArrayList<String> outputPaths = new ArrayList<>(); |
| 521 | + String delims = "[,]"; |
| 522 | + String[] ranges = splitConfig.split(delims); |
| 523 | + Log.v("Ranges", Arrays.toString(ranges)); |
509 | 524 | try { |
510 | 525 | String folderPath = mSharedPreferences.getString(STORAGE_LOCATION, |
511 | 526 | getDefaultStorageLocation()); |
512 | 527 | PdfReader reader = new PdfReader(path); |
513 | 528 | PdfCopy copy; |
514 | 529 | Document document; |
515 | | - int pages = reader.getNumberOfPages(); |
516 | | - for (int i = 1; i <= pages; i++) { |
517 | | - document = new Document(); |
| 530 | + for (String range : ranges) { |
| 531 | + int startPage; |
| 532 | + int endPage; |
| 533 | + |
518 | 534 | String fileName = folderPath + FileUtils.getFileName(path); |
519 | | - fileName = fileName.replace(mContext.getString(R.string.pdf_ext), |
520 | | - i + mContext.getString(R.string.pdf_ext)); |
521 | | - Log.v("splitting", fileName); |
522 | | - copy = new PdfCopy(document, new FileOutputStream(fileName)); |
523 | | - document.open(); |
524 | | - copy.addPage(copy.getImportedPage(reader, i)); |
525 | | - document.close(); |
| 535 | + |
| 536 | + /** |
| 537 | + * If the pdf is single page only then convert whole range into int |
| 538 | + * else break the range on "-",where startpage will be substring |
| 539 | + * from first letter to "-" and endpage will be from "-" till last letter. |
| 540 | + * |
| 541 | + */ |
| 542 | + if (!range.contains("-")) { |
| 543 | + startPage = Integer.parseInt(range); |
| 544 | + document = new Document(); |
| 545 | + fileName = fileName.replace(pdfExtension, |
| 546 | + "_" + startPage + pdfExtension); |
| 547 | + copy = new PdfCopy(document, new FileOutputStream(fileName)); |
| 548 | + |
| 549 | + document.open(); |
| 550 | + copy.addPage(copy.getImportedPage(reader, startPage)); |
| 551 | + document.close(); |
| 552 | + |
| 553 | + } else { |
| 554 | + startPage = Integer.parseInt(range.substring(0, range.indexOf("-"))); |
| 555 | + endPage = Integer.parseInt(range.substring(range.indexOf("-") + 1)); |
| 556 | + document = new Document(); |
| 557 | + fileName = fileName.replace(pdfExtension, |
| 558 | + "_" + startPage + "-" + endPage + pdfExtension); |
| 559 | + copy = new PdfCopy(document, new FileOutputStream(fileName)); |
| 560 | + document.open(); |
| 561 | + for (int page = startPage; page <= endPage; page++) { |
| 562 | + copy.addPage(copy.getImportedPage(reader, page)); |
| 563 | + } |
| 564 | + document.close(); |
| 565 | + } |
526 | 566 | outputPaths.add(fileName); |
527 | 567 | new DatabaseHelper(mContext).insertRecord(fileName, |
528 | 568 | mContext.getString(R.string.created)); |
|
0 commit comments