Skip to content

Commit

Permalink
#420-add splitting options (#567)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivamdhuria authored and Swati4star committed Feb 22, 2019
1 parent 8019145 commit e81f807
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -111,7 +120,8 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) throw

@OnClick(R.id.splitFiles)
public void parse() {
ArrayList<String> outputFilePaths = mPDFUtils.splitPDF(mPath);
ArrayList<String> 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);
Expand Down Expand Up @@ -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
Expand Down
66 changes: 53 additions & 13 deletions app/src/main/java/swati4star/createpdf/util/PDFUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -504,25 +508,61 @@ public boolean reorderRemovePDF(String inputPath, String output, String pages) {
}
}

public ArrayList<String> 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<String> splitPDFByConfig(String path, String splitDetail) {
String splitConfig = splitDetail.replaceAll("\\s+", "");
ArrayList<String> 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));
Expand Down
36 changes: 22 additions & 14 deletions app/src/main/res/layout/fragment_split_files.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,14 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
<LinearLayout
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:stretchColumns="*"
tools:context=".fragment.AddImagesFragment">

<TextView
android:id="@+id/split_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="15sp"
android:textStyle="bold"
android:padding="5dp"
android:layout_marginTop="5dp"
android:text="@string/split_info"/>

<com.dd.morphingbutton.MorphingButton
android:id="@+id/selectFile"
style="@style/MorphingButton"
Expand All @@ -38,6 +25,27 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/split_config"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
android:visibility="gone" />

<TextView
android:id="@+id/split_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:padding="5dp"
android:text="@string/split_info"
android:textSize="15sp"
android:textStyle="bold" />

<com.dd.morphingbutton.MorphingButton
android:id="@+id/splitFiles"
style="@style/MorphingButton"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@
<string name="font_family_edit">Cambiar valor de la letra fuente por defecto</string>
<string name="font_size_edit">Cambiar valor del Tamaño del Texto por defecto</string>
<string name="settings_info">Puede modificar los valores por defecto de varias opciones aquí.</string>
<string name="split_info">El PDF será dividido en múltiples PDFs de una sola página.</string>
<string name="split_info">The PDF will be split into multiple PDFs.Format example: 1&#8211;5,6&#8211;7,8,9</string>
<string name="border_width_prompt">Ingrese unidades anchura del borde</string>
<string name="default_content_description">sin descripción</string>
<string name="prompt_input">Ingrese aquí</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@
<string name="font_family_edit">Change default font family</string>
<string name="font_size_edit">Change default font size</string>
<string name="settings_info">You can modify the default value for various settings here.</string>
<string name="split_info">The PDF will be split into multiple PDFs of single page.</string>
<string name="split_info">The PDF will be split into multiple PDFs.Format example: 1&#8211;5,6&#8211;7,8,9</string>
<string name="border_width_prompt">Enter border width units</string>
<string name="default_content_description">no description</string>
<string name="prompt_input">Enter here</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-ja/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@
<string name="font_family_edit">Change default font family</string>
<string name="font_size_edit">Change default font size</string>
<string name="settings_info">You can modify the default value for various settings here.</string>
<string name="split_info">The PDF will be split into multiple PDFs of single page.</string>
<string name="split_info">The PDF will be split into multiple PDFs.Format example: 1&#8211;5,6&#8211;7,8,9</string>
<string name="border_width_prompt">Enter border width units</string>
<string name="default_content_description">no description</string>
<string name="prompt_input">Enter here</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@
<!-- Split PDF -->
<string name="split_pdf">Разделить PDF</string>
<string name="split_success">Выбранный PDF файл успешно разделен на %1$d </string>
<string name="split_info">PDF файл будел разделен на несколько одностраничных файлов.</string>
<string name="split_info">The PDF will be split into multiple PDFs.Format example: 1&#8211;5,6&#8211;7,8,9</string>

<!-- Remove Pages -->
<string name="remove_pages">Удалить страницы</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@
<!-- Split PDF -->
<string name="split_pdf">Split PDF</string>
<string name="split_success">The selected PDF was split successfully. It was split into %1$d PDF(s)</string>
<string name="split_info">The PDF will be split into multiple PDFs of single page.</string>
<string name="split_info">The PDF will be split into multiple PDFs.Format example: 1&#8211;5,6&#8211;7,8,9</string>

<!-- Remove Pages -->
<string name="remove_pages">Remove Pages</string>
Expand Down

0 comments on commit e81f807

Please sign in to comment.