1

I'm using itext and creating PDF files. I need to add a JPG file to my document but i couldn't. I created a test folder for my app which is in assets/imageAssets includes jpg.jpg file, but i couldn't add that JPG file to my document. I'm getting "No such file and directory" error. Here is the code block

        var filePath = "assets/imageAssets/jpg.jpg"
        var image: Image? = Image.getInstance(filePath)
        document.add(image)

Actually i have another question. I'm getting user signatures from user, and i'm saving that on External Storage folder. That jpg will be the thing which i'm going to add to my PDFs. Where can i save them, and can how can i access them? Thanks!

1 Answers1

1
 @Throws(DocumentException::class)
private fun addImage(document: Document)
{
    try { // Get user Settings GeneralSettings getUserSettings =
        val rectDoc = document.pageSize
        val width = rectDoc.width
        val height = rectDoc.height
        val imageStartX = width - document.rightMargin() - 315f
        val imageStartY = height - document.topMargin() - 500f
        System.gc()

        val ims: InputStream? = activity?.assets?.open("test.jpg") //Inside a Fragment
        val bmp = BitmapFactory.decodeStream(ims)
        val stream = ByteArrayOutputStream()
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream)
        val byteArray: ByteArray = stream.toByteArray()
        // PdfImage img = new PdfImage(arg0, arg1, arg2)

        // Converting byte array into image Image img =
        val img = Image.getInstance(byteArray) // img.scalePercent(50);
        img.alignment = Image.TEXTWRAP
        img.scaleAbsolute(200f, 200f)
        img.setAbsolutePosition(imageStartX, imageStartY) // Adding Image
        document.add(img)
    } catch (e: java.lang.Exception) {
        e.printStackTrace()
    }
}