From e81f8075d2616e24489ae64233f90a07409c2ddb Mon Sep 17 00:00:00 2001 From: Shivamdhuria Date: Fri, 22 Feb 2019 21:59:22 +0530 Subject: [PATCH] #420-add splitting options (#567) --- .../fragment/SplitFilesFragment.java | 39 ++++++++++- .../swati4star/createpdf/util/PDFUtils.java | 66 +++++++++++++++---- .../main/res/layout/fragment_split_files.xml | 36 ++++++---- app/src/main/res/values-es/strings.xml | 2 +- app/src/main/res/values-fr/strings.xml | 2 +- app/src/main/res/values-ja/strings.xml | 2 +- app/src/main/res/values-ru/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- 8 files changed, 118 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/swati4star/createpdf/fragment/SplitFilesFragment.java b/app/src/main/java/swati4star/createpdf/fragment/SplitFilesFragment.java index 046ce39f3..59624b92d 100644 --- a/app/src/main/java/swati4star/createpdf/fragment/SplitFilesFragment.java +++ b/app/src/main/java/swati4star/createpdf/fragment/SplitFilesFragment.java @@ -3,7 +3,9 @@ import android.app.Activity; import android.content.Context; import android.content.Intent; +import android.graphics.pdf.PdfRenderer; import android.os.Bundle; +import android.os.ParcelFileDescriptor; import android.support.annotation.NonNull; import android.support.design.widget.BottomSheetBehavior; import android.support.v4.app.Fragment; @@ -12,6 +14,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; @@ -20,6 +23,9 @@ import com.airbnb.lottie.LottieAnimationView; import com.dd.morphingbutton.MorphingButton; +import java.io.File; +import java.io.FileDescriptor; +import java.io.IOException; import java.util.ArrayList; import butterknife.BindView; @@ -37,6 +43,7 @@ import swati4star.createpdf.util.ViewFilesDividerItemDecoration; import static android.app.Activity.RESULT_OK; +import static android.os.ParcelFileDescriptor.MODE_READ_ONLY; import static swati4star.createpdf.util.CommonCodeUtils.populateUtil; import static swati4star.createpdf.util.FileUriUtils.getFilePath; import static swati4star.createpdf.util.StringUtils.showSnackbar; @@ -73,6 +80,8 @@ public class SplitFilesFragment extends Fragment implements MergeFilesAdapter.On RecyclerView mSplittedFiles; @BindView(R.id.splitfiles_text) TextView splitFilesSuccessText; + @BindView(R.id.split_config) + EditText mSplitConfitEditText; @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, @@ -111,7 +120,8 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) throw @OnClick(R.id.splitFiles) public void parse() { - ArrayList outputFilePaths = mPDFUtils.splitPDF(mPath); + ArrayList outputFilePaths = mPDFUtils.splitPDFByConfig(mPath, + mSplitConfitEditText.getText().toString()); int numberOfPages = outputFilePaths.size(); if (numberOfPages > 0) { String output = String.format(mActivity.getString(R.string.split_success), numberOfPages); @@ -156,6 +166,33 @@ private void setTextAndActivateButtons(String path) { mPath = path; mMorphButtonUtility.setTextAndActivateButtons(path, selectFileButton, splitFilesButton); + mSplitConfitEditText.setVisibility(View.VISIBLE); + mSplitConfitEditText.setText(getDefaultSplitConfig(mPath)); + } + + /** + * Gets the total number of pages in the + * selected PDF and returns them in + * default format for single page pdfs + * (1,2,3,4,5,) + */ + private String getDefaultSplitConfig(String mPath) { + String splitConfig = ""; + ParcelFileDescriptor fileDescriptor = null; + try { + if (mPath != null) + fileDescriptor = ParcelFileDescriptor.open(new File(mPath), MODE_READ_ONLY); + if (fileDescriptor != null) { + PdfRenderer renderer = new PdfRenderer(fileDescriptor); + final int pageCount = renderer.getPageCount(); + for (int i = 1; i <= pageCount; i++) { + splitConfig = splitConfig + i + ","; + } + } + } catch (Exception er) { + er.printStackTrace(); + } + return splitConfig; } @Override diff --git a/app/src/main/java/swati4star/createpdf/util/PDFUtils.java b/app/src/main/java/swati4star/createpdf/util/PDFUtils.java index 2d045c203..b0117b69b 100644 --- a/app/src/main/java/swati4star/createpdf/util/PDFUtils.java +++ b/app/src/main/java/swati4star/createpdf/util/PDFUtils.java @@ -44,11 +44,13 @@ import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; +import java.util.Arrays; import swati4star.createpdf.R; import swati4star.createpdf.database.DatabaseHelper; @@ -59,6 +61,7 @@ import static swati4star.createpdf.util.Constants.MASTER_PWD_STRING; import static swati4star.createpdf.util.Constants.STORAGE_LOCATION; import static swati4star.createpdf.util.Constants.appName; +import static swati4star.createpdf.util.Constants.pdfExtension; import static swati4star.createpdf.util.DialogUtils.createCustomDialogWithoutContent; import static swati4star.createpdf.util.StringUtils.getDefaultStorageLocation; import static swati4star.createpdf.util.StringUtils.getSnackbarwithAction; @@ -118,7 +121,7 @@ public void showDetails(File file) { * Create a PDF from a Text File * * @param mTextToPDFOptions TextToPDFOptions Object - * @param fileExtension file extension represented as string + * @param fileExtension file extension represented as string */ public void createPdf(TextToPDFOptions mTextToPDFOptions, String fileExtension) throws DocumentException, IOException { @@ -430,8 +433,9 @@ protected void onPostExecute(String s) { /** * Main function to add images to PDF + * * @param inputPath - path of input PDF - * @param output - path of output PDF + * @param output - path of output PDF * @param imagesUri - list of images to add * @return true, if succeeded, otherwise false */ @@ -504,25 +508,61 @@ public boolean reorderRemovePDF(String inputPath, String output, String pages) { } } - public ArrayList splitPDF(String path) { + /** + * Breaks up the splitDetail String into ranges where a "," + * is found + * @param path the input pdf path + * @param splitDetail string that contains split configuration + * @return + */ + public ArrayList splitPDFByConfig(String path, String splitDetail) { + String splitConfig = splitDetail.replaceAll("\\s+", ""); ArrayList outputPaths = new ArrayList<>(); + String delims = "[,]"; + String[] ranges = splitConfig.split(delims); + Log.v("Ranges", Arrays.toString(ranges)); try { String folderPath = mSharedPreferences.getString(STORAGE_LOCATION, getDefaultStorageLocation()); PdfReader reader = new PdfReader(path); PdfCopy copy; Document document; - int pages = reader.getNumberOfPages(); - for (int i = 1; i <= pages; i++) { - document = new Document(); + for (String range : ranges) { + int startPage; + int endPage; + String fileName = folderPath + FileUtils.getFileName(path); - fileName = fileName.replace(mContext.getString(R.string.pdf_ext), - i + mContext.getString(R.string.pdf_ext)); - Log.v("splitting", fileName); - copy = new PdfCopy(document, new FileOutputStream(fileName)); - document.open(); - copy.addPage(copy.getImportedPage(reader, i)); - document.close(); + + /** + * If the pdf is single page only then convert whole range into int + * else break the range on "-",where startpage will be substring + * from first letter to "-" and endpage will be from "-" till last letter. + * + */ + if (!range.contains("-")) { + startPage = Integer.parseInt(range); + document = new Document(); + fileName = fileName.replace(pdfExtension, + "_" + startPage + pdfExtension); + copy = new PdfCopy(document, new FileOutputStream(fileName)); + + document.open(); + copy.addPage(copy.getImportedPage(reader, startPage)); + document.close(); + + } else { + startPage = Integer.parseInt(range.substring(0, range.indexOf("-"))); + endPage = Integer.parseInt(range.substring(range.indexOf("-") + 1)); + document = new Document(); + fileName = fileName.replace(pdfExtension, + "_" + startPage + "-" + endPage + pdfExtension); + copy = new PdfCopy(document, new FileOutputStream(fileName)); + document.open(); + for (int page = startPage; page <= endPage; page++) { + copy.addPage(copy.getImportedPage(reader, page)); + } + document.close(); + } outputPaths.add(fileName); new DatabaseHelper(mContext).insertRecord(fileName, mContext.getString(R.string.created)); diff --git a/app/src/main/res/layout/fragment_split_files.xml b/app/src/main/res/layout/fragment_split_files.xml index 79162ca4b..c5d10d724 100644 --- a/app/src/main/res/layout/fragment_split_files.xml +++ b/app/src/main/res/layout/fragment_split_files.xml @@ -5,9 +5,7 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - - - + + + + Cambiar valor de la letra fuente por defecto Cambiar valor del Tamaño del Texto por defecto Puede modificar los valores por defecto de varias opciones aquí. - El PDF será dividido en múltiples PDFs de una sola página. + The PDF will be split into multiple PDFs.Format example: 1–5,6–7,8,9 Ingrese unidades anchura del borde sin descripción Ingrese aquí diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index d59fe5c8c..5f2f69900 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -276,7 +276,7 @@ Change default font family Change default font size You can modify the default value for various settings here. - The PDF will be split into multiple PDFs of single page. + The PDF will be split into multiple PDFs.Format example: 1–5,6–7,8,9 Enter border width units no description Enter here diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index 895f77eee..245c66ec9 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -275,7 +275,7 @@ Change default font family Change default font size You can modify the default value for various settings here. - The PDF will be split into multiple PDFs of single page. + The PDF will be split into multiple PDFs.Format example: 1–5,6–7,8,9 Enter border width units no description Enter here diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 5e7110d71..cb4250259 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -283,7 +283,7 @@ Разделить PDF Выбранный PDF файл успешно разделен на %1$d - PDF файл будел разделен на несколько одностраничных файлов. + The PDF will be split into multiple PDFs.Format example: 1–5,6–7,8,9 Удалить страницы diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5c4d1f942..b29ed5659 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -339,7 +339,7 @@ Split PDF The selected PDF was split successfully. It was split into %1$d PDF(s) - The PDF will be split into multiple PDFs of single page. + The PDF will be split into multiple PDFs.Format example: 1–5,6–7,8,9 Remove Pages