Commit 3ad487b9 authored by wanglei's avatar wanglei

...提交部分功能...

parent 2b6ab242
......@@ -6,15 +6,17 @@ import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.graphics.Color
import android.view.View
import android.widget.Toast
import androidx.activity.addCallback
import androidx.core.animation.addListener
import androidx.lifecycle.lifecycleScope
import com.base.superpdfreader.bean.PdfParameterBean
import com.base.superpdfreader.databinding.ActivityPdfBrowserBinding
import com.base.superpdfreader.helps.BaseActivity
import com.base.superpdfreader.helps.LogEx
import com.base.superpdfreader.utils.BarUtils
import com.base.superpdfreader.view.DialogViews.showEnterDdfPassword
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle
import com.tom_roush.pdfbox.pdmodel.PDDocument
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
......@@ -40,15 +42,42 @@ class PdfBrowserActivity : BaseActivity<ActivityPdfBrowserBinding>() {
val path = intent.extras?.getString("Path") ?: ""
val file = File(path)
if (file.exists()) {
try {
PDDocument.load(file)
loadPdfView(file)
} catch (e: Exception) {
dialog = showEnterDdfPassword(cancel = {
finishToMain()
}, ok = { password ->
var pdDocument: PDDocument? = null
runCatching {
pdDocument = PDDocument.load(file, password)
}
if (pdDocument != null) {
dialog?.dismiss()
loadPdfView(file, password)
} else {
Toast.makeText(this, "Wrong password", Toast.LENGTH_SHORT).show()
}
})
}
}
}
private fun loadPdfView(file: File, password: String = "") {
binding.pdfview.fromFile(file)
.defaultPage(pageNumber)
.enableAnnotationRendering(true)
.scrollHandle(object : DefaultScrollHandle(this) {})
.spacing(10)
.onPageError { page, t -> }
.load()
}
.onPageError { page, t ->
LogEx.logDebug(TAG, "error $t")
}.password(password).load()
}
override fun initListener() {
......
......@@ -12,6 +12,8 @@ import com.tom_roush.pdfbox.pdmodel.PDDocument
import com.tom_roush.pdfbox.pdmodel.PDPage
import com.tom_roush.pdfbox.pdmodel.PDPageContentStream
import com.tom_roush.pdfbox.pdmodel.common.PDRectangle
import com.tom_roush.pdfbox.pdmodel.encryption.AccessPermission
import com.tom_roush.pdfbox.pdmodel.encryption.StandardProtectionPolicy
import com.tom_roush.pdfbox.pdmodel.graphics.image.PDImageXObject
import java.io.BufferedInputStream
import java.io.ByteArrayOutputStream
......@@ -28,11 +30,10 @@ object PdfHelp {
LogEx.logDebug(TAG, "pageSize=${arrayPageSize[pdfParameterBean.pageSize]}")
LogEx.logDebug(TAG, "margin=${arrayMargin[pdfParameterBean.margin]}")
LogEx.logDebug(TAG, "compression=${arrayCompression[pdfParameterBean.compression]}")
LogEx.logDebug(TAG, "password=${pdfParameterBean.password}")
try {
PDDocument().use { document ->
pdfParameterBean.pathArray.forEach { imagePath ->
// val pdImage = PDImageXObject.createFromFileByExtension(File(imagePath), document)
......@@ -111,7 +112,9 @@ object PdfHelp {
}
}
}
pdfParameterBean.password?.let { pwd ->
protectPdf(pwd, document)
}
document.save(pdfPath) // 保存 PDF
}
} catch (e: IOException) {
......@@ -120,6 +123,14 @@ object PdfHelp {
}
fun protectPdf(password: String, document: PDDocument) {
val accessPermission = AccessPermission()
val spp = StandardProtectionPolicy(password, password, accessPermission)
spp.setEncryptionKeyLength(128)
spp.permissions = accessPermission
document.protect(spp)
}
fun compressImage(path: String, compress: Int): ByteArray {
val bitmap = openImage2Bitmap(path)
val out = ByteArrayOutputStream()
......
......@@ -13,6 +13,8 @@ import com.base.superpdfreader.R
import com.base.superpdfreader.databinding.DialogPermissonOpenBinding
import com.base.superpdfreader.databinding.ItemDeleteTipBinding
import com.base.superpdfreader.databinding.ItemEditBinding
import com.base.superpdfreader.databinding.ItemEnterPdfPasswordBinding
import com.base.superpdfreader.view.DialogViews.showDeleteConfirm
import com.base.superpdfreader.view.DialogViews.showRenameDialog
object DialogViews {
......@@ -110,4 +112,39 @@ object DialogViews {
nButton.textSize = 16f
nButton.setTextColor(ContextCompat.getColor(this, R.color.color_pdf))
}
fun Context.showEnterDdfPassword(cancel: () -> Unit, ok: (password: String) -> Unit): AlertDialog? {
val view = LayoutInflater.from(this).inflate(R.layout.item_enter_pdf_password, null, false)
val binding = ItemEnterPdfPasswordBinding.bind(view)
val dialog = AlertDialog.Builder(this)
.setView(view)
.setPositiveButton("OK")
{ dialog, which ->
val pwd = binding.editPassword.text.toString()
if (pwd.isEmpty()) {
Toast.makeText(this, "Password cannot be empty", Toast.LENGTH_SHORT).show()
return@setPositiveButton
}
ok.invoke(pwd)
}.setNegativeButton("CANCEL")
{ dialog, which ->
dialog.dismiss()
cancel.invoke()
}.create()
dialog.setCanceledOnTouchOutside(false)
dialog.show()
val pButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE)
val nButton = dialog.getButton(AlertDialog.BUTTON_NEGATIVE)
pButton.textSize = 16f
pButton.setTextColor(ContextCompat.getColor(this, R.color.color_pdf))
nButton.textSize = 16f
nButton.setTextColor(ContextCompat.getColor(this, R.color.color_pdf))
return dialog
}
}
......@@ -18,7 +18,7 @@
android:id="@+id/pdfview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#FACCC6"
android:background="#E0E0E0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/v_top" />
......
......@@ -275,6 +275,8 @@
android:layout_marginHorizontal="15dp"
android:background="@null"
android:hint="Password"
android:inputType="textVisiblePassword"
android:singleLine="true"
tools:ignore="Autofill,HardcodedText,TextFields" />
</com.noober.background.view.BLFrameLayout>
......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="15dp"
tools:ignore="UseCompoundDrawables">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@mipmap/lock_pdf"
tools:ignore="ContentDescription" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="16dp"
android:text="Protected PDF"
android:textColor="@color/black"
android:textSize="20sp"
tools:ignore="HardcodedText" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
android:text="Enter password"
android:textSize="16sp"
tools:ignore="HardcodedText" />
<EditText
android:textSize="16sp"
android:id="@+id/edit_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="30dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="16dp"
android:inputType="textVisiblePassword"
android:theme="@style/MyMyEditText"
tools:ignore="Autofill,LabelFor,TextFields" />
</LinearLayout>
\ No newline at end of file
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