我想添加图像到PDF从相机文件夹图像显示我可以添加一个PDF格式的图像



我通过使用文档类创建pdf,在我从sd卡相机文件夹下载图像后,我想保留pdf文件中的所有图像,我正在使用文档页面是A4尺寸,我想缩放图像与A4尺寸意味着我想保持图像与A4尺寸的图像。

private void addImages(Document document) {
        // TODO Auto-generated method stub
        File targetDirector = new File(targetPath);
        File[] files = targetDirector.listFiles();
        for (File file : files) {

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            thumbnails = decodeFile(file.getAbsolutePath());
            ExifInterface exif;
            try {
                exif = new ExifInterface(file.getAbsolutePath());
                String orientString = exif
                        .getAttribute(ExifInterface.TAG_ORIENTATION);
                int orientation = orientString != null ? Integer
                        .parseInt(orientString)
                        : ExifInterface.ORIENTATION_NORMAL;
                int rotationAngle = 0;
                if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
                    System.out.println("photopotart");
                    thumbnails = getResizedBitmap(thumbnails, 150, 150);
                    thumbnails
                            .compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    Image myImg = null;
                    try {
                        myImg = Image.getInstance(stream.toByteArray());
                    } catch (BadElementException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    // add image to document
                    try {
                        document.newPage();
                        document.add(myImg);
                    } catch (DocumentException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
                    System.out.println("180 angle");
                    rotationAngle = 180;
                    System.out.println("photo180");
                    thumbnails = getResizedBitmap180(thumbnails, 200, 200);
                    thumbnails
                            .compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    Image myImg = null;
                    try {
                        myImg = Image.getInstance(stream.toByteArray());
                    } catch (BadElementException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    myImg.setAlignment(Image.LEFT);
                    // add image to document
                    try {
                        document.newPage();
                        document.add(myImg);
                    } catch (DocumentException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
                    System.out.println("other 270");
                    System.out.println("photo270");
                    thumbnails = getResizedBitmap270(thumbnails, 200, 200);
                    thumbnails
                            .compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    Image myImg = null;
                    try {
                        myImg = Image.getInstance(stream.toByteArray());
                    } catch (BadElementException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    // add image to document
                    try {
                        document.newPage();
                        document.add(myImg);
                    } catch (DocumentException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else {
                    System.out.println("photolandscape");
                    System.out.println("photo0");
                    // thumbnails =thumbnails.createScaledBitmap(thumbnails,
                    // 350, 250, false);
                    System.out.println("thumbnailsw" + thumbnails.getWidth());
                    System.out.println("thumbnailsh" + thumbnails.getHeight());

                    String imageUrl = file.getAbsolutePath();
                    Image image2 = Image.getInstance(file.getAbsolutePath());
                    image2.scaleAbsolute(150f, 150f);
                      document.newPage();
                      document.add(image2);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (BadElementException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (DocumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

试试这样

 ByteArrayOutputStream stream = new ByteArrayOutputStream();
                         Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.ic_launcher);
                         bitmap.compress(Bitmap.CompressFormat.JPEG, 100 , stream);
                         Image myImg = Image.getInstance(stream.toByteArray());
                         myImg.setAlignment(Image.MIDDLE);
                         //add image to document
                         document.add(myImg);

和缩放图像,如果不填充页面

 Bitmap.createScaledBitmap(unscaledBitmap, wantedWidth, wantedHeight, true);

也看一下这个它对缩放位图有很好的解释

最新更新