Commit 9f95e179 authored by wanglei's avatar wanglei

...

parent 118a1bd6
...@@ -6,6 +6,7 @@ import android.graphics.drawable.BitmapDrawable ...@@ -6,6 +6,7 @@ import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.net.Uri import android.net.Uri
import com.base.pdfviewerscannerwhite.helper.MyApplication.Companion.context import com.base.pdfviewerscannerwhite.helper.MyApplication.Companion.context
import com.base.pdfviewerscannerwhite.utils.LogEx
import com.base.pdfviewerscannerwhite.utils.UriUtils.readFileToByteArray import com.base.pdfviewerscannerwhite.utils.UriUtils.readFileToByteArray
import com.tom_roush.pdfbox.pdmodel.PDDocument import com.tom_roush.pdfbox.pdmodel.PDDocument
import com.tom_roush.pdfbox.pdmodel.PDPage import com.tom_roush.pdfbox.pdmodel.PDPage
...@@ -25,7 +26,6 @@ object PdfBoxUtils { ...@@ -25,7 +26,6 @@ object PdfBoxUtils {
private val TAG = "PdfUtils" private val TAG = "PdfUtils"
fun getNumberOfPages(filePath: String, password: String? = null, uri: String? = null): Int { fun getNumberOfPages(filePath: String, password: String? = null, uri: String? = null): Int {
val document = val document =
loadPDDocument(path = filePath, password, uri) loadPDDocument(path = filePath, password, uri)
...@@ -90,7 +90,8 @@ object PdfBoxUtils { ...@@ -90,7 +90,8 @@ object PdfBoxUtils {
sourceFilePath: String, sourceFilePath: String,
userPassword: String, userPassword: String,
ownerPassword: String ownerPassword: String
) { ): Boolean {
try { try {
PDDocument.load(File(sourceFilePath)).use { document -> PDDocument.load(File(sourceFilePath)).use { document ->
val ap = AccessPermission() val ap = AccessPermission()
...@@ -102,10 +103,14 @@ object PdfBoxUtils { ...@@ -102,10 +103,14 @@ object PdfBoxUtils {
val spp = StandardProtectionPolicy(userPassword, ownerPassword, ap) val spp = StandardProtectionPolicy(userPassword, ownerPassword, ap)
document.protect(spp) document.protect(spp)
document.save(File(sourceFilePath)) document.save(File(sourceFilePath))
LogEx.logDebug(TAG, "setPassword finish")
} }
} catch (e: Exception) { } catch (e: Exception) {
LogEx.logDebug(TAG, "setPassword Exception ${e.printStackTrace()}")
e.printStackTrace()
return false
} }
return true
} }
fun clearPassword(filePath: String, password: String) { fun clearPassword(filePath: String, password: String) {
......
...@@ -13,8 +13,6 @@ import com.base.pdfviewerscannerwhite.utils.LogEx ...@@ -13,8 +13,6 @@ import com.base.pdfviewerscannerwhite.utils.LogEx
import com.base.pdfviewerscannerwhite.utils.UriUtils.readFileToByteArray import com.base.pdfviewerscannerwhite.utils.UriUtils.readFileToByteArray
import com.tom_roush.pdfbox.multipdf.PDFMergerUtility import com.tom_roush.pdfbox.multipdf.PDFMergerUtility
import com.tom_roush.pdfbox.pdmodel.PDDocument import com.tom_roush.pdfbox.pdmodel.PDDocument
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.File import java.io.File
...@@ -142,30 +140,46 @@ class PdfPresenter( ...@@ -142,30 +140,46 @@ class PdfPresenter(
val mergerUtility = PDFMergerUtility() val mergerUtility = PDFMergerUtility()
mergerUtility.destinationFileName = mergePath mergerUtility.destinationFileName = mergePath
//解密
PdfMergeActivity.mergePdfList.filter { it.state == 1 }.forEach {
PdfBoxUtils.clearPassword(it.path, it.password)
}
LogEx.logDebug(TAG, "mergePdf ${mergePdfList.size}") LogEx.logDebug(TAG, "mergePdf ${mergePdfList.size}")
PdfMergeActivity.mergePdfList.forEach { documentBean -> PdfMergeActivity.mergePdfList.forEach { documentBean ->
LogEx.logDebug(TAG, "mergePdf item= ${documentBean.path} ${documentBean.state}") LogEx.logDebug(TAG, "mergePdf item= ${documentBean.path} ${documentBean.state}")
if (documentBean.state == 0) {
mergerUtility.addSource(File(documentBean.path)) //带密码 mergerUtility.addSource(inputStream) 不行
} else { mergerUtility.addSource(File(documentBean.path))
LogEx.logDebug(TAG, "mergePdf password=${documentBean.password}")
try { // if (documentBean.state == 0) {
val pdfDocument = PDDocument.load(File(documentBean.path), documentBean.password) // mergerUtility.addSource(File(documentBean.path))
LogEx.logDebug(TAG, "mergePdf pdfDocument ${pdfDocument.numberOfPages}") // } else {
val byteArrayOutputStream = ByteArrayOutputStream(1024 * 2) // LogEx.logDebug(TAG, "mergePdf password=${documentBean.password}")
pdfDocument.save(byteArrayOutputStream) // try {
LogEx.logDebug(TAG, "mergePdf byteArrayOutputStream ${byteArrayOutputStream.size()}") // val pdfDocument = PDDocument.load(File(documentBean.path), documentBean.password)
pdfDocument.close() // LogEx.logDebug(TAG, "mergePdf pdfDocument ${pdfDocument.numberOfPages}")
val inputStream = ByteArrayInputStream(byteArrayOutputStream.toByteArray()) // val byteArrayOutputStream = ByteArrayOutputStream()
LogEx.logDebug(TAG, "mergePdf inputStream") // pdfDocument.save(byteArrayOutputStream)
mergerUtility.addSource(inputStream) // LogEx.logDebug(TAG, "mergePdf byteArrayOutputStream ${byteArrayOutputStream.size()}")
} catch (e: Exception) { // pdfDocument.close()
LogEx.logDebug(TAG, "mergePdf Exception ${e.printStackTrace()}") // val inputStream = ByteArrayInputStream(byteArrayOutputStream.toByteArray())
} // LogEx.logDebug(TAG, "mergePdf inputStream")
LogEx.logDebug(TAG, "mergePdf inputStream") // mergerUtility.addSource(inputStream)
} // } catch (e: Exception) {
// LogEx.logDebug(TAG, "mergePdf Exception ${e.printStackTrace()}")
// }
// LogEx.logDebug(TAG, "mergePdf inputStream")
// }
} }
mergerUtility.mergeDocuments(null) mergerUtility.mergeDocuments(null)
//重新加密
PdfMergeActivity.mergePdfList.filter { it.state == 1 }.forEach {
val flag = PdfBoxUtils.setPassword(it.path, it.password, it.password)
LogEx.logDebug(TAG, "重新加密 flag=$flag ${it.path} ${it.password}")
}
mergePdfList.clear() mergePdfList.clear()
LogEx.logDebug(TAG, "mergePdf finish") LogEx.logDebug(TAG, "mergePdf finish")
isSuccess = true isSuccess = true
...@@ -173,6 +187,7 @@ class PdfPresenter( ...@@ -173,6 +187,7 @@ class PdfPresenter(
} }
finishAction.invoke(isSuccess) finishAction.invoke(isSuccess)
}.start() }.start()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment