Add multiple images into a single pdf file with iText using java

前端 未结 2 884
予麋鹿
予麋鹿 2020-11-30 15:17

I have the following code but this code add only the last image into pdf.

    try {
        filePath = (filePath != null && filePath.endsWith(\".pdf\         


        
相关标签:
2条回答
  • 2020-11-30 16:03

    Android has the feature "PdfDocument" to achieve this,

    class Main2Activity : AppCompatActivity() {
    
    private var imgFiles: Array<File?>? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)
    
        imgFiles= arrayOfNulls(2)
    
        imgFiles!![0] = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + "/doc1.png")
        imgFiles!![1] = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + "/doc3.png")
    
    
    
        val file = getOutputFile(File(Environment.getExternalStorageDirectory().absolutePath)
                , "/output.pdf")
    
        val fOut = FileOutputStream(file)
        val document = PdfDocument()
    
        var i = 0
        imgFiles?.forEach {
            i++
            val bitmap = BitmapFactory.decodeFile(it?.path)
            val pageInfo = PdfDocument.PageInfo.Builder(bitmap.width, bitmap.height, i).create()
            val page = document.startPage(pageInfo)
            val canvas = page?.canvas
            val paint = Paint()
            canvas?.drawPaint(paint)
            paint.color = Color.BLUE;
            canvas?.drawBitmap(bitmap, 0f, 0f, null)
            document.finishPage(page)
            bitmap.recycle()
        }
        document.writeTo(fOut)
        document.close()        
    
    }
    
    private fun getOutputFile(path: File, fileName: String): File? {
        if (!path.exists()) {
            path.mkdirs()
        }
        val file = File(path, fileName)
        try {
            if (file.exists()) {
                file.delete()
            }
            file.createNewFile()
    
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return file
    }
    

    }

    finally enable the storage permission in manifest, this should works

    0 讨论(0)
  • 2020-11-30 16:13

    Take a look at the MultipleImages example and you'll discover that there are two errors in your code:

    1. You create a page with size 595 x 842 user units, and you add every image to that page regardless of the dimensions of the image.
    2. You claim that only one image is added, but that's not true. You are adding all the images on top of each other on the same page. The last image covers all the preceding images.

    Take a look at my code:

    public void createPdf(String dest) throws IOException, DocumentException {
        Image img = Image.getInstance(IMAGES[0]);
        Document document = new Document(img);
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        for (String image : IMAGES) {
            img = Image.getInstance(image);
            document.setPageSize(img);
            document.newPage();
            img.setAbsolutePosition(0, 0);
            document.add(img);
        }
        document.close();
    }
    

    I create a Document instance using the size of the first image. I then loop over an array of images, setting the page size of the next page to the size of each image before I trigger a newPage() [*]. Then I add the image at coordinate 0, 0 because now the size of the image will match the size of each page.

    [*] The newPage() method only has effect if something was added to the current page. The first time you go through the loop, nothing has been added yet, so nothing happens. This is why you need set the page size to the size of the first image when you create the Document instance.

    0 讨论(0)
提交回复
热议问题