Skip to content

Commit

Permalink
#727 - Fixed App error in PDF to images for password protected files (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jddeep authored and vyankatesh24 committed May 1, 2019
1 parent 5cc745f commit 98e1e4a
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 14 deletions.
Expand Up @@ -10,6 +10,7 @@
import android.support.design.widget.BottomSheetBehavior;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -41,8 +42,10 @@
import swati4star.createpdf.util.ExtractImages;
import swati4star.createpdf.util.FileUtils;
import swati4star.createpdf.util.MorphButtonUtility;
import swati4star.createpdf.util.PDFUtils;
import swati4star.createpdf.util.PdfToImages;
import swati4star.createpdf.util.RealPathUtil;
import swati4star.createpdf.util.StringUtils;

import static android.app.Activity.RESULT_OK;
import static swati4star.createpdf.util.CommonCodeUtils.checkSheetBehaviourUtil;
Expand All @@ -51,6 +54,7 @@
import static swati4star.createpdf.util.Constants.BUNDLE_DATA;
import static swati4star.createpdf.util.Constants.PDF_TO_IMAGES;
import static swati4star.createpdf.util.DialogUtils.createAnimationDialog;
import static swati4star.createpdf.util.DialogUtils.createOverwriteDialog;
import static swati4star.createpdf.util.StringUtils.showSnackbar;

public class PdfToImageFragment extends Fragment implements BottomSheetPopulate, MergeFilesAdapter.OnClickListener,
Expand All @@ -67,6 +71,9 @@ public class PdfToImageFragment extends Fragment implements BottomSheetPopulate,
private ArrayList<String> mOutputFilePaths;
private MaterialDialog mMaterialDialog;
private String mOperation;
private Context mContext;
private PDFUtils mPDFUtils;
private String[] mInputPassword;

@BindView(R.id.lottie_progress)
LottieAnimationView mLottieProgress;
Expand Down Expand Up @@ -177,13 +184,45 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) throw
}

/**
* invokes generation of images for pdf pages in the background
* invokes generation of images for pdf pages in the background by checking
* for encryption first.
*/
@OnClick(R.id.createImages)
public void parse() {
if (mOperation.equals(PDF_TO_IMAGES))
new PdfToImages(mPath, mUri, this).execute();
else
if (mPDFUtils.isPDFEncrypted(mPath)) {
mInputPassword = new String[1];
new MaterialDialog.Builder(mActivity)
.title(R.string.enter_password)
.content(R.string.decrypt_protected_file)
.inputType(InputType.TYPE_TEXT_VARIATION_PASSWORD)
.input(null, null, (dialog, input) -> {
if (StringUtils.isEmpty(input)) {
showSnackbar(mActivity, R.string.snackbar_name_not_blank);
} else {
final String inputName = input.toString();
if (inputName != null) {
mInputPassword[0] = inputName;
pdfToImage(mInputPassword);
}
}
})
.show();
} else {
pdfToImage(mInputPassword);
}
}

/**
* Thia method handles the call to the Async process of conversion
* from PDF to Image.
*
* @param mInputPassword - the password if the file is encrypted.
*/
private void pdfToImage(String[] mInputPassword) {
if (mOperation.equals(PDF_TO_IMAGES)) {
new PdfToImages(mContext, mInputPassword, mPath, mUri, this)
.execute();
} else
new ExtractImages(mPath, this).execute();
}

Expand All @@ -194,6 +233,8 @@ public void onAttach(Context context) {
mMorphButtonUtility = new MorphButtonUtility(mActivity);
mFileUtils = new FileUtils(mActivity);
mBottomSheetUtils = new BottomSheetUtils(mActivity);
mContext = context;
mPDFUtils = new PDFUtils(mActivity);
}

/**
Expand Down
Expand Up @@ -191,6 +191,36 @@ public void afterTextChanged(Editable input) {
});
}

/**
* This function removes the password for encrypted files.
* @param file - the path of the actual encrypted file.
* @param inputPassword - the password of the encrypted file.
* @return
*/
public String removeDefPasswordForImages(final String file,
final String[] inputPassword) {
String finalOutputFile;
try {
String masterpwd = mSharedPrefs.getString(MASTER_PWD_STRING, appName);
PdfReader reader = new PdfReader(file, masterpwd.getBytes());
byte[] password;
finalOutputFile = mFileUtils.getUniqueFileName
(file.replace(mContext.getResources().getString(R.string.pdf_ext),
mContext.getString(R.string.decrypted_file)));
password = reader.computeUserPassword();
byte[] input = inputPassword[0].getBytes();
if (Arrays.equals(input, password)) {
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(finalOutputFile));
stamper.close();
reader.close();
return finalOutputFile;
}
} catch (DocumentException | IOException e) {
e.printStackTrace();
}
return null;
}

private boolean removePasswordUsingDefMasterPAssword(final String file,
final DataSetChanged dataSetChanged,
final String[] inputPassword) {
Expand Down
38 changes: 28 additions & 10 deletions app/src/main/java/swati4star/createpdf/util/PdfToImages.java
@@ -1,5 +1,7 @@
package swati4star.createpdf.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
Expand All @@ -26,35 +28,51 @@ public class PdfToImages extends AsyncTask<Void, Void, Void> {
private ExtractImagesListener mExtractImagesListener;
private int mImagesCount = 0;
private ArrayList<String> mOutputFilePaths;
private String[] mPassword;
private PDFEncryptionUtility mPDFEncryptionUtility;
private Context mContext;
private String mDecryptedPath;

public PdfToImages(String mPath, Uri mUri, ExtractImagesListener mExtractImagesListener) {
public PdfToImages(Context context, String[] password, String mPath, Uri mUri,
ExtractImagesListener mExtractImagesListener) {
this.mPath = mPath;
this.mUri = mUri;
this.mExtractImagesListener = mExtractImagesListener;
mOutputFilePaths = new ArrayList<>();
this.mPassword = password;
this.mContext = context;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
mPDFEncryptionUtility = new PDFEncryptionUtility((Activity) mContext);
mExtractImagesListener.extractionStarted();
}

@Override
protected Void doInBackground(Void... voids) {
if (mPassword != null) {
mDecryptedPath = mPDFEncryptionUtility.removeDefPasswordForImages(mPath, mPassword);
}
mOutputFilePaths = new ArrayList<>();
mImagesCount = 0;

// Render pdf pages as bitmap
ParcelFileDescriptor fileDescriptor = null;
try {
if (mUri != null)
// resolve pdf file path based on uri
fileDescriptor = ((PdfToImageFragment) mExtractImagesListener).getContext()
.getContentResolver().openFileDescriptor(mUri, "r");
else if (mPath != null)
// resolve pdf file path based on relative path
fileDescriptor = ParcelFileDescriptor.open(new File(mPath), MODE_READ_ONLY);
if (mDecryptedPath != null)
fileDescriptor = ParcelFileDescriptor.open(new File(mDecryptedPath), MODE_READ_ONLY);
else {
if (mUri != null) {
// resolve pdf file path based on uri
fileDescriptor = ((PdfToImageFragment) mExtractImagesListener).getContext()
.getContentResolver().openFileDescriptor(mUri, "r");
} else if (mPath != null) {
// resolve pdf file path based on relative path
fileDescriptor = ParcelFileDescriptor.open(new File(mPath), MODE_READ_ONLY);
}
}
if (fileDescriptor != null) {
PdfRenderer renderer = new PdfRenderer(fileDescriptor);
final int pageCount = renderer.getPageCount();
Expand Down Expand Up @@ -88,14 +106,14 @@ else if (mPath != null)
} catch (IOException | SecurityException e) {
e.printStackTrace();
}


return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
mExtractImagesListener.updateView(mImagesCount, mOutputFilePaths);
if (mDecryptedPath != null)
new File(mDecryptedPath).delete();
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values-es/strings.xml
Expand Up @@ -241,6 +241,7 @@
<string name="extract_text">Extract Text</string>
<string name="snackbar_txtExtracted">Text extracted!</string>
<string name="snackbar_pdfselected">PDF file selected!</string>
<string name="decrypt_protected_file">Selected pdf is password protected. Enter the password to continue...</string>
<string name="creating_txt">Creating Text File</string>
<string name="filter_brush">Pincel</string>
<string name="filter_saved">Imagen guardada</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-fr/strings.xml
Expand Up @@ -405,6 +405,7 @@
<string name="pdf_selected">PDF file selected: </string>
<string name="snackbar_txtExtracted">Text extracted!</string>
<string name="snackbar_pdfselected">PDF file selected!</string>
<string name="decrypt_protected_file">Selected pdf is password protected. Enter the password to continue...</string>
<string name="creating_txt">Creating Text File</string>

<string name="font_color">Font Color</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ja/strings.xml
Expand Up @@ -403,6 +403,7 @@
<string name="pdf_selected">PDF file selected: </string>
<string name="snackbar_txtExtracted">Text extracted!</string>
<string name="snackbar_pdfselected">PDF file selected!</string>
<string name="decrypt_protected_file">Selected pdf is password protected. Enter the password to continue...</string>
<string name="creating_txt">Creating Text File</string>

<string name="font_color">Font Color</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ru/strings.xml
Expand Up @@ -446,6 +446,7 @@
<string name="pdf_selected">PDF file selected: </string>
<string name="snackbar_txtExtracted">Text extracted!</string>
<string name="snackbar_pdfselected">PDF file selected!</string>
<string name="decrypt_protected_file">Selected pdf is password protected. Enter the password to continue...</string>
<string name="creating_txt">Creating Text File</string>

<string name="font_color">Font Color</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Expand Up @@ -200,6 +200,7 @@
<string name="set_as_default">Set as default</string>
<string name="decrypted_file">_unencrypted.pdf</string>
<string name="decrypt_message">Enter the password to continue</string>
<string name="decrypt_protected_file">Selected pdf is password protected. Enter the password to continue...</string>
<string name="not_encrypted">PDF is not Encrypted</string>
<string name="save_current">Save</string>
<string name="save">Save</string>
Expand Down

0 comments on commit 98e1e4a

Please sign in to comment.