From 0bc4a4fab2393700bafa28555f72ad0289faa859 Mon Sep 17 00:00:00 2001 From: ocean <503259349@qq.com> Date: Thu, 16 Oct 2025 16:38:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8B=86=E5=88=86=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E5=AF=86=E7=A0=81=E7=9A=84=E6=96=87=E6=A1=A3=EF=BC=8C?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pro/app/ui/act/PdfResultActivity.kt | 27 +++++---- .../pro/app/ui/act/SplitPdfActivity.kt | 7 ++- .../all/pdfreader/pro/app/util/PdfUtils.kt | 57 ++++++++++++++----- 3 files changed, 65 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/com/all/pdfreader/pro/app/ui/act/PdfResultActivity.kt b/app/src/main/java/com/all/pdfreader/pro/app/ui/act/PdfResultActivity.kt index 6ddcec6..3c38f45 100644 --- a/app/src/main/java/com/all/pdfreader/pro/app/ui/act/PdfResultActivity.kt +++ b/app/src/main/java/com/all/pdfreader/pro/app/ui/act/PdfResultActivity.kt @@ -38,15 +38,20 @@ class PdfResultActivity : BaseActivity() { private const val EXTRA_SELECTED_LIST = "extra_selected_list" private const val EXTRA_FILE_LIST = "extra_file_list" 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_SPLIT_PASSWORD = "extra_split_password" fun createIntentPdfSelectedPagesItem( - context: Context, list: ArrayList, source: PdfPickerSource + context: Context, + list: ArrayList, + source: PdfPickerSource, + password: String? = null ): Intent { return Intent(context, PdfResultActivity::class.java).apply { putParcelableArrayListExtra(EXTRA_SELECTED_LIST, list) putExtra(EXTRA_SOURCE, source) + putExtra(EXTRA_SPLIT_PASSWORD, password) } } @@ -64,7 +69,7 @@ class PdfResultActivity : BaseActivity() { ): Intent { return Intent(context, PdfResultActivity::class.java).apply { putExtra(EXTRA_FILE_PATH, filepath) - putExtra(EXTRA_PASSWORD, password) + putExtra(EXTRA_LOCK_UNLOCK_PASSWORD, password) putExtra(EXTRA_SOURCE, source) } } @@ -81,7 +86,7 @@ class PdfResultActivity : BaseActivity() { private val pdfRepository = getRepository() private lateinit var pdfScanner: PdfScanner private lateinit var filepath: String - private lateinit var password: String + private lateinit var lockAndUnlockPassword: String override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -93,7 +98,7 @@ class PdfResultActivity : BaseActivity() { selectedList = requireParcelableArrayList(EXTRA_SELECTED_LIST) inputFile = requireStringArrayList(EXTRA_FILE_LIST) filepath = intent.getStringExtra(EXTRA_FILE_PATH) ?: "" - password = intent.getStringExtra(EXTRA_PASSWORD) ?: "" + lockAndUnlockPassword = intent.getStringExtra(EXTRA_LOCK_UNLOCK_PASSWORD) ?: "" source = getSerializableOrDefault(EXTRA_SOURCE, PdfPickerSource.NONE) if (source == PdfPickerSource.NONE) { showToast(getString(R.string.pdf_loading_failed)) @@ -113,7 +118,7 @@ class PdfResultActivity : BaseActivity() { return } } 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)) finish() return @@ -139,6 +144,7 @@ class PdfResultActivity : BaseActivity() { binding.processingLayout.visibility = View.VISIBLE } if (source == PdfPickerSource.SPLIT) { + val splitPassword = intent.getStringExtra(EXTRA_SPLIT_PASSWORD) ?: "" binding.progressBar.isIndeterminate = false binding.progressBar.progress = 0 binding.progressBar.max = 100 @@ -166,7 +172,8 @@ class PdfResultActivity : BaseActivity() { binding.progressTv.text = "$percent" binding.progressBar.progress = percent } - })?.let { resultFile -> + }, splitPassword + )?.let { resultFile -> val thumbnails = generateFastThumbnail(this@PdfResultActivity, resultFile) val result = PdfSplitResultItem(resultFile.absolutePath, thumbnails, false) pdfScanner.addNewPdfToDatabase(result.filePath, result.thumbnailPath) { @@ -208,7 +215,7 @@ class PdfResultActivity : BaseActivity() { } else if (source == PdfPickerSource.LOCK) { binding.congratulationsDesc.text = getString(R.string.set_password_successfully) PdfSecurityUtils.setPasswordToPdfWithProgress( - filepath, password, password + filepath, lockAndUnlockPassword, lockAndUnlockPassword ) { progress -> binding.progressTv.text = "$progress" }.let { it -> @@ -220,7 +227,7 @@ class PdfResultActivity : BaseActivity() { } } else if (source == PdfPickerSource.UNLOCK) { binding.congratulationsDesc.text = getString(R.string.remove_password_successfully) - PdfSecurityUtils.removePasswordFromPdfWithProgress(filepath, password) { progress -> + PdfSecurityUtils.removePasswordFromPdfWithProgress(filepath, lockAndUnlockPassword) { progress -> binding.progressTv.text = "$progress" }?.let { it -> val result = PdfSplitResultItem( @@ -230,7 +237,7 @@ class PdfResultActivity : BaseActivity() { ) resultList.add(result) } - } else if(source == PdfPickerSource.TO_IMAGES){ + } else if (source == PdfPickerSource.TO_IMAGES) { val inputFiles: List = inputFile.map { path -> File(path) } val outputDir = File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), diff --git a/app/src/main/java/com/all/pdfreader/pro/app/ui/act/SplitPdfActivity.kt b/app/src/main/java/com/all/pdfreader/pro/app/ui/act/SplitPdfActivity.kt index 08b0f72..4abd84d 100644 --- a/app/src/main/java/com/all/pdfreader/pro/app/ui/act/SplitPdfActivity.kt +++ b/app/src/main/java/com/all/pdfreader/pro/app/ui/act/SplitPdfActivity.kt @@ -34,6 +34,7 @@ class SplitPdfActivity : BaseActivity() { private var currentViewState: ViewState = ViewState.SPLIT_LIST + var currentPassword : String? = null//拆分只会选择一个文件。直接进行密码传递 private enum class ViewState { SPLIT_LIST, // 拆分页列表 @@ -143,7 +144,8 @@ class SplitPdfActivity : BaseActivity() { val intent = PdfResultActivity.createIntentPdfSelectedPagesItem( this, ArrayList(selectedList), - PdfPickerSource.SPLIT + PdfPickerSource.SPLIT, + password = currentPassword ) startActivity(intent) finish() @@ -169,6 +171,9 @@ class SplitPdfActivity : BaseActivity() { } private fun initSplitDataWithPassword(file: File, password: String? = null) { + if(!password.isNullOrEmpty()){ + currentPassword = password + } lifecycleScope.launch { splitList.clear() // 先切换到 IO 线程执行删除,等待完成 diff --git a/app/src/main/java/com/all/pdfreader/pro/app/util/PdfUtils.kt b/app/src/main/java/com/all/pdfreader/pro/app/util/PdfUtils.kt index b009b59..664d481 100644 --- a/app/src/main/java/com/all/pdfreader/pro/app/util/PdfUtils.kt +++ b/app/src/main/java/com/all/pdfreader/pro/app/util/PdfUtils.kt @@ -119,7 +119,8 @@ object PdfUtils { selectedPages: List, outputDir: File, outputFileName: String, - onProgress: ((current: Int, total: Int) -> Unit)? = null + onProgress: ((current: Int, total: Int) -> Unit)? = null, + splitPassword: String? = null ): File? = withContext(Dispatchers.IO) { // 如果没有选中的页,直接返回 null @@ -127,21 +128,41 @@ object PdfUtils { if (!outputDir.exists()) outputDir.mkdirs() val outputFile = File(outputDir, outputFileName) try { - PDDocument.load(inputFile, 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) + if (splitPassword.isNullOrEmpty()) { + PDDocument.load(inputFile, 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) } - // 保存新 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 } catch (e: Exception) { @@ -273,7 +294,13 @@ object PdfUtils { val pdImage = JPEGFactory.createFromImage(document, bitmap) PDPageContentStream(document, page).use { contentStream -> - contentStream.drawImage(pdImage, offsetX, offsetY, targetWidth, targetHeight) + contentStream.drawImage( + pdImage, + offsetX, + offsetY, + targetWidth, + targetHeight + ) } bitmap.recycle()