Commit 4592181e authored by wanglei's avatar wanglei

...

parent ef7a5af2
......@@ -29,7 +29,9 @@ object PdfBoxUtils {
fun getNumberOfPages(filePath: String, password: String? = null, uri: String? = null): Int {
val document =
loadPDDocument(path = filePath, password, uri)
return document.numberOfPages
val number = document.numberOfPages
document.close()
return number
}
......@@ -43,10 +45,9 @@ object PdfBoxUtils {
): Drawable {
val document = loadPDDocument(filePath, password, uri)
val renderer = PDFRenderer(document)
val bitmap: Bitmap = renderer.renderImage(pageIndex, scale, ImageType.RGB, RenderDestination.EXPORT)
val drawable = BitmapDrawable(context.resources, bitmap)
document.close()
return drawable
}
......@@ -143,8 +144,9 @@ object PdfBoxUtils {
password: String,
uri: String? = null
): Boolean {
var pdfDocument: PDDocument? = null
try {
val pdfDocument = loadPDDocument(filePath, password, uri)
pdfDocument = loadPDDocument(filePath, password, uri)
// 尝试使用提供的密码加载PDF文件
pdfDocument.use { document ->
// 如果没有抛出异常,说明密码正确
......@@ -156,10 +158,15 @@ object PdfBoxUtils {
} catch (e: IOException) {
// 其他I/O异常处理
println("An I/O error occurred: " + e.message)
} finally {
pdfDocument?.close()
}
return false
}
/**
* 注意一下PDDocument用完都要关闭一下
*/
private fun loadPDDocument(path: String, password: String?, uri: String? = null): PDDocument {
return if (uri == null) {
if (password == null) {
......
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