繁体   English   中英

在Android中使用itext将多个图像添加到PDF文档

[英]Adding multiple images to a PDF document with itext in android

我正在开发一个Android应用程序,其中我想使用itext jar将多个高分辨率图像添加到pdf文件中。 我遇到内存不足的异常。 当我为宽度和高度设置较小的值时,它可以正常工作。 这是在异步后台任务类中实现的。 源代码如下

fos = new FileOutputStream(pdfFilePath);
        //Rectangle pagesize = new Rectangle(595.44f, 841.68f);
        document = new Document(PageSize.A4, 0f, 0f, 0f, 0f);
        PdfWriter.getInstance(document,new FileOutputStream(pdfFilePath));

        fileArray  = imageDirectory.listFiles();
        Log.d("Files", "Size: "+ fileArray.length);
        for (i=0; i < fileArray.length; i++){
            document.open();
            bMap= BitmapFactory.decodeFile(fileArray[i].getPath(), null);
            bMap = Bitmap.createScaledBitmap(bMap, 2339, 1654, false);
            matrix = new Matrix();
            matrix.postRotate(90);
            bMap = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(), bMap.getHeight(), matrix, false);
            bMap = toGrayscale(bMap);
            stream = new ByteArrayOutputStream();
            bMap.compress(Bitmap.CompressFormat.JPEG, 25, stream);
            byteArray = stream.toByteArray();
            document.add(Image.getInstance(byteArray));
            Log.e("Files", "FileName:" + fileArray[i].getName());

            bMap = null;
            matrix = null;
            byteArray = null;
            stream = null;
            document.close();
        } 

        fos.close();

请参阅: 管理位图内存

您可能想要在将bMap.recycle()设置为null之前调用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM