Commit 9f95e179 authored by wanglei's avatar wanglei

...

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