Android:文件pdf无法打开ItextPDF 5.5.10

时间:2018-09-04 06:05:36

标签: android kotlin itext

我正在使用iText 5.5.10将多个图像转换为pdf。但有时有几个无法打开的pdf文件。无法打开pdf文件的地方通常不会发生(文件图像保存在Internal Storage中)。请帮我。这是我的代码用iText转换pdf

fun createMultiFilePdfiText(list:Array<File>?,path:String,nameFile:String):Boolean{
    var isCreatePDFSuccess = true

    if(list != null){
        val document = Document()
        val dir = File(path)
        if(!dir.exists()){
            dir.mkdirs()
        }
        val fileOutput = File(dir,nameFile)
        val outputStream = FileOutputStream(fileOutput)
        PdfWriter.getInstance(document,outputStream)
        document.open()
        for(itemFile in list){
            try {
                var bitmap1 = bitmapManager?.decodeSampledBitmapFromFile(itemFile.absolutePath,1024,1754)
                if(bitmap1 != null){
                    if (bitmap1.width > bitmap1.height) {
                        bitmap1 = bitmapManager?.rotate(bitmap1,90f)
                    }
                    val bitmap = Bitmap.createScaledBitmap(bitmap1,1240,1754,true)
                    val stream = ByteArrayOutputStream()
                    bitmap?.compress(Bitmap.CompressFormat.JPEG, 80, stream)
                    val image = Image.getInstance(stream.toByteArray())
                    document.pageSize = Rectangle(image.width,image.height)
                    image.setAbsolutePosition(0f,0f)
                    document.newPage()
                    document.add(image)

                    try {
                        bitmap.recycle()
                        bitmap1?.recycle()
                        stream.flush()
                        stream.close()
                    }catch (ex:Exception){

                    }

                }else{
                    isCreatePDFSuccess = false
                    break
                }
            }catch (ex:Exception){
                Crashlytics.log(ex.message)
                isCreatePDFSuccess = false
                break
            }
        }

        try {
            document.close()
            outputStream.flush()
            outputStream.close()
        }catch (ex:Exception){
            Crashlytics.log(ex.message)
        }

    }else{
        isCreatePDFSuccess = false
    }

    return isCreatePDFSuccess
}

0 个答案:

没有答案