修复拆分需要密码的文档,错误。
This commit is contained in:
parent
0cded6a58d
commit
0bc4a4fab2
@ -38,15 +38,20 @@ class PdfResultActivity : BaseActivity() {
|
|||||||
private const val EXTRA_SELECTED_LIST = "extra_selected_list"
|
private const val EXTRA_SELECTED_LIST = "extra_selected_list"
|
||||||
private const val EXTRA_FILE_LIST = "extra_file_list"
|
private const val EXTRA_FILE_LIST = "extra_file_list"
|
||||||
private const val EXTRA_SOURCE = "extra_source"
|
private const val EXTRA_SOURCE = "extra_source"
|
||||||
private const val EXTRA_PASSWORD = "extra_password"
|
private const val EXTRA_LOCK_UNLOCK_PASSWORD = "extra_lock_unlock_password"
|
||||||
private const val EXTRA_FILE_PATH = "extra_file_path"
|
private const val EXTRA_FILE_PATH = "extra_file_path"
|
||||||
|
private const val EXTRA_SPLIT_PASSWORD = "extra_split_password"
|
||||||
|
|
||||||
fun createIntentPdfSelectedPagesItem(
|
fun createIntentPdfSelectedPagesItem(
|
||||||
context: Context, list: ArrayList<PdfSelectedPagesItem>, source: PdfPickerSource
|
context: Context,
|
||||||
|
list: ArrayList<PdfSelectedPagesItem>,
|
||||||
|
source: PdfPickerSource,
|
||||||
|
password: String? = null
|
||||||
): Intent {
|
): Intent {
|
||||||
return Intent(context, PdfResultActivity::class.java).apply {
|
return Intent(context, PdfResultActivity::class.java).apply {
|
||||||
putParcelableArrayListExtra(EXTRA_SELECTED_LIST, list)
|
putParcelableArrayListExtra(EXTRA_SELECTED_LIST, list)
|
||||||
putExtra(EXTRA_SOURCE, source)
|
putExtra(EXTRA_SOURCE, source)
|
||||||
|
putExtra(EXTRA_SPLIT_PASSWORD, password)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,7 +69,7 @@ class PdfResultActivity : BaseActivity() {
|
|||||||
): Intent {
|
): Intent {
|
||||||
return Intent(context, PdfResultActivity::class.java).apply {
|
return Intent(context, PdfResultActivity::class.java).apply {
|
||||||
putExtra(EXTRA_FILE_PATH, filepath)
|
putExtra(EXTRA_FILE_PATH, filepath)
|
||||||
putExtra(EXTRA_PASSWORD, password)
|
putExtra(EXTRA_LOCK_UNLOCK_PASSWORD, password)
|
||||||
putExtra(EXTRA_SOURCE, source)
|
putExtra(EXTRA_SOURCE, source)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -81,7 +86,7 @@ class PdfResultActivity : BaseActivity() {
|
|||||||
private val pdfRepository = getRepository()
|
private val pdfRepository = getRepository()
|
||||||
private lateinit var pdfScanner: PdfScanner
|
private lateinit var pdfScanner: PdfScanner
|
||||||
private lateinit var filepath: String
|
private lateinit var filepath: String
|
||||||
private lateinit var password: String
|
private lateinit var lockAndUnlockPassword: String
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
@ -93,7 +98,7 @@ class PdfResultActivity : BaseActivity() {
|
|||||||
selectedList = requireParcelableArrayList(EXTRA_SELECTED_LIST)
|
selectedList = requireParcelableArrayList(EXTRA_SELECTED_LIST)
|
||||||
inputFile = requireStringArrayList(EXTRA_FILE_LIST)
|
inputFile = requireStringArrayList(EXTRA_FILE_LIST)
|
||||||
filepath = intent.getStringExtra(EXTRA_FILE_PATH) ?: ""
|
filepath = intent.getStringExtra(EXTRA_FILE_PATH) ?: ""
|
||||||
password = intent.getStringExtra(EXTRA_PASSWORD) ?: ""
|
lockAndUnlockPassword = intent.getStringExtra(EXTRA_LOCK_UNLOCK_PASSWORD) ?: ""
|
||||||
source = getSerializableOrDefault(EXTRA_SOURCE, PdfPickerSource.NONE)
|
source = getSerializableOrDefault(EXTRA_SOURCE, PdfPickerSource.NONE)
|
||||||
if (source == PdfPickerSource.NONE) {
|
if (source == PdfPickerSource.NONE) {
|
||||||
showToast(getString(R.string.pdf_loading_failed))
|
showToast(getString(R.string.pdf_loading_failed))
|
||||||
@ -113,7 +118,7 @@ class PdfResultActivity : BaseActivity() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else if (source == PdfPickerSource.LOCK || source == PdfPickerSource.UNLOCK) {
|
} else if (source == PdfPickerSource.LOCK || source == PdfPickerSource.UNLOCK) {
|
||||||
if (filepath.isEmpty() || password.isEmpty()) {
|
if (filepath.isEmpty() || lockAndUnlockPassword.isEmpty()) {
|
||||||
showToast(getString(R.string.pdf_loading_failed))
|
showToast(getString(R.string.pdf_loading_failed))
|
||||||
finish()
|
finish()
|
||||||
return
|
return
|
||||||
@ -139,6 +144,7 @@ class PdfResultActivity : BaseActivity() {
|
|||||||
binding.processingLayout.visibility = View.VISIBLE
|
binding.processingLayout.visibility = View.VISIBLE
|
||||||
}
|
}
|
||||||
if (source == PdfPickerSource.SPLIT) {
|
if (source == PdfPickerSource.SPLIT) {
|
||||||
|
val splitPassword = intent.getStringExtra(EXTRA_SPLIT_PASSWORD) ?: ""
|
||||||
binding.progressBar.isIndeterminate = false
|
binding.progressBar.isIndeterminate = false
|
||||||
binding.progressBar.progress = 0
|
binding.progressBar.progress = 0
|
||||||
binding.progressBar.max = 100
|
binding.progressBar.max = 100
|
||||||
@ -166,7 +172,8 @@ class PdfResultActivity : BaseActivity() {
|
|||||||
binding.progressTv.text = "$percent"
|
binding.progressTv.text = "$percent"
|
||||||
binding.progressBar.progress = percent
|
binding.progressBar.progress = percent
|
||||||
}
|
}
|
||||||
})?.let { resultFile ->
|
}, splitPassword
|
||||||
|
)?.let { resultFile ->
|
||||||
val thumbnails = generateFastThumbnail(this@PdfResultActivity, resultFile)
|
val thumbnails = generateFastThumbnail(this@PdfResultActivity, resultFile)
|
||||||
val result = PdfSplitResultItem(resultFile.absolutePath, thumbnails, false)
|
val result = PdfSplitResultItem(resultFile.absolutePath, thumbnails, false)
|
||||||
pdfScanner.addNewPdfToDatabase(result.filePath, result.thumbnailPath) {
|
pdfScanner.addNewPdfToDatabase(result.filePath, result.thumbnailPath) {
|
||||||
@ -208,7 +215,7 @@ class PdfResultActivity : BaseActivity() {
|
|||||||
} else if (source == PdfPickerSource.LOCK) {
|
} else if (source == PdfPickerSource.LOCK) {
|
||||||
binding.congratulationsDesc.text = getString(R.string.set_password_successfully)
|
binding.congratulationsDesc.text = getString(R.string.set_password_successfully)
|
||||||
PdfSecurityUtils.setPasswordToPdfWithProgress(
|
PdfSecurityUtils.setPasswordToPdfWithProgress(
|
||||||
filepath, password, password
|
filepath, lockAndUnlockPassword, lockAndUnlockPassword
|
||||||
) { progress ->
|
) { progress ->
|
||||||
binding.progressTv.text = "$progress"
|
binding.progressTv.text = "$progress"
|
||||||
}.let { it ->
|
}.let { it ->
|
||||||
@ -220,7 +227,7 @@ class PdfResultActivity : BaseActivity() {
|
|||||||
}
|
}
|
||||||
} else if (source == PdfPickerSource.UNLOCK) {
|
} else if (source == PdfPickerSource.UNLOCK) {
|
||||||
binding.congratulationsDesc.text = getString(R.string.remove_password_successfully)
|
binding.congratulationsDesc.text = getString(R.string.remove_password_successfully)
|
||||||
PdfSecurityUtils.removePasswordFromPdfWithProgress(filepath, password) { progress ->
|
PdfSecurityUtils.removePasswordFromPdfWithProgress(filepath, lockAndUnlockPassword) { progress ->
|
||||||
binding.progressTv.text = "$progress"
|
binding.progressTv.text = "$progress"
|
||||||
}?.let { it ->
|
}?.let { it ->
|
||||||
val result = PdfSplitResultItem(
|
val result = PdfSplitResultItem(
|
||||||
@ -230,7 +237,7 @@ class PdfResultActivity : BaseActivity() {
|
|||||||
)
|
)
|
||||||
resultList.add(result)
|
resultList.add(result)
|
||||||
}
|
}
|
||||||
} else if(source == PdfPickerSource.TO_IMAGES){
|
} else if (source == PdfPickerSource.TO_IMAGES) {
|
||||||
val inputFiles: List<File> = inputFile.map { path -> File(path) }
|
val inputFiles: List<File> = inputFile.map { path -> File(path) }
|
||||||
val outputDir = File(
|
val outputDir = File(
|
||||||
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS),
|
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS),
|
||||||
|
|||||||
@ -34,6 +34,7 @@ class SplitPdfActivity : BaseActivity() {
|
|||||||
|
|
||||||
|
|
||||||
private var currentViewState: ViewState = ViewState.SPLIT_LIST
|
private var currentViewState: ViewState = ViewState.SPLIT_LIST
|
||||||
|
var currentPassword : String? = null//拆分只会选择一个文件。直接进行密码传递
|
||||||
|
|
||||||
private enum class ViewState {
|
private enum class ViewState {
|
||||||
SPLIT_LIST, // 拆分页列表
|
SPLIT_LIST, // 拆分页列表
|
||||||
@ -143,7 +144,8 @@ class SplitPdfActivity : BaseActivity() {
|
|||||||
val intent = PdfResultActivity.createIntentPdfSelectedPagesItem(
|
val intent = PdfResultActivity.createIntentPdfSelectedPagesItem(
|
||||||
this,
|
this,
|
||||||
ArrayList(selectedList),
|
ArrayList(selectedList),
|
||||||
PdfPickerSource.SPLIT
|
PdfPickerSource.SPLIT,
|
||||||
|
password = currentPassword
|
||||||
)
|
)
|
||||||
startActivity(intent)
|
startActivity(intent)
|
||||||
finish()
|
finish()
|
||||||
@ -169,6 +171,9 @@ class SplitPdfActivity : BaseActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun initSplitDataWithPassword(file: File, password: String? = null) {
|
private fun initSplitDataWithPassword(file: File, password: String? = null) {
|
||||||
|
if(!password.isNullOrEmpty()){
|
||||||
|
currentPassword = password
|
||||||
|
}
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
splitList.clear()
|
splitList.clear()
|
||||||
// 先切换到 IO 线程执行删除,等待完成
|
// 先切换到 IO 线程执行删除,等待完成
|
||||||
|
|||||||
@ -119,7 +119,8 @@ object PdfUtils {
|
|||||||
selectedPages: List<PdfPageItem>,
|
selectedPages: List<PdfPageItem>,
|
||||||
outputDir: File,
|
outputDir: File,
|
||||||
outputFileName: String,
|
outputFileName: String,
|
||||||
onProgress: ((current: Int, total: Int) -> Unit)? = null
|
onProgress: ((current: Int, total: Int) -> Unit)? = null,
|
||||||
|
splitPassword: String? = null
|
||||||
): File? = withContext(Dispatchers.IO) {
|
): File? = withContext(Dispatchers.IO) {
|
||||||
|
|
||||||
// 如果没有选中的页,直接返回 null
|
// 如果没有选中的页,直接返回 null
|
||||||
@ -127,21 +128,41 @@ object PdfUtils {
|
|||||||
if (!outputDir.exists()) outputDir.mkdirs()
|
if (!outputDir.exists()) outputDir.mkdirs()
|
||||||
val outputFile = File(outputDir, outputFileName)
|
val outputFile = File(outputDir, outputFileName)
|
||||||
try {
|
try {
|
||||||
PDDocument.load(inputFile, MemoryUsageSetting.setupTempFileOnly()).use { document ->
|
if (splitPassword.isNullOrEmpty()) {
|
||||||
PDDocument().use { newDocument ->
|
PDDocument.load(inputFile, MemoryUsageSetting.setupTempFileOnly()).use { document ->
|
||||||
// 按页索引排序,保证顺序正确
|
PDDocument().use { newDocument ->
|
||||||
val sortedPages = selectedPages.sortedBy { it.pageIndex }
|
// 按页索引排序,保证顺序正确
|
||||||
val total = sortedPages.size//总数
|
val sortedPages = selectedPages.sortedBy { it.pageIndex }
|
||||||
sortedPages.forEachIndexed { index, pageItem ->
|
val total = sortedPages.size//总数
|
||||||
// 导入原文档的页面到新文档
|
sortedPages.forEachIndexed { index, pageItem ->
|
||||||
newDocument.importPage(document.getPage(pageItem.pageIndex))
|
// 导入原文档的页面到新文档
|
||||||
// 回调进度
|
newDocument.importPage(document.getPage(pageItem.pageIndex))
|
||||||
onProgress?.invoke(index + 1, total)
|
// 回调进度
|
||||||
delay(1)
|
onProgress?.invoke(index + 1, total)
|
||||||
|
delay(1)
|
||||||
|
}
|
||||||
|
// 保存新 PDF 文件
|
||||||
|
newDocument.save(outputFile)
|
||||||
}
|
}
|
||||||
// 保存新 PDF 文件
|
|
||||||
newDocument.save(outputFile)
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
PDDocument.load(inputFile, splitPassword, MemoryUsageSetting.setupTempFileOnly())
|
||||||
|
.use { document ->
|
||||||
|
PDDocument().use { newDocument ->
|
||||||
|
// 按页索引排序,保证顺序正确
|
||||||
|
val sortedPages = selectedPages.sortedBy { it.pageIndex }
|
||||||
|
val total = sortedPages.size//总数
|
||||||
|
sortedPages.forEachIndexed { index, pageItem ->
|
||||||
|
// 导入原文档的页面到新文档
|
||||||
|
newDocument.importPage(document.getPage(pageItem.pageIndex))
|
||||||
|
// 回调进度
|
||||||
|
onProgress?.invoke(index + 1, total)
|
||||||
|
delay(1)
|
||||||
|
}
|
||||||
|
// 保存新 PDF 文件
|
||||||
|
newDocument.save(outputFile)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
outputFile
|
outputFile
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
@ -273,7 +294,13 @@ object PdfUtils {
|
|||||||
|
|
||||||
val pdImage = JPEGFactory.createFromImage(document, bitmap)
|
val pdImage = JPEGFactory.createFromImage(document, bitmap)
|
||||||
PDPageContentStream(document, page).use { contentStream ->
|
PDPageContentStream(document, page).use { contentStream ->
|
||||||
contentStream.drawImage(pdImage, offsetX, offsetY, targetWidth, targetHeight)
|
contentStream.drawImage(
|
||||||
|
pdImage,
|
||||||
|
offsetX,
|
||||||
|
offsetY,
|
||||||
|
targetWidth,
|
||||||
|
targetHeight
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
bitmap.recycle()
|
bitmap.recycle()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user