Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed App error on pdf to image for protected files #739

Merged
merged 6 commits into from May 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) -> {
vyankatesh24 marked this conversation as resolved.
Show resolved Hide resolved
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) {
vyankatesh24 marked this conversation as resolved.
Show resolved Hide resolved
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);
vyankatesh24 marked this conversation as resolved.
Show resolved Hide resolved
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>
vyankatesh24 marked this conversation as resolved.
Show resolved Hide resolved
<string name="not_encrypted">PDF is not Encrypted</string>
<string name="save_current">Save</string>
<string name="save">Save</string>
Expand Down