Android生成PDF文件

本文主要是操作如何将选取的图片生成一个PDF文件

/**转换的PDF的质量*/
private final int QualityHigh = 1;
private final int QualityMiddle = 2;
private final int QualitySmall = 3;
private List imageArr=new ArrayList<>;
private String fileName;
private int quality = QualityMiddle;
private String UserBean user;
@OnClick(R.id.btn_generate)
@Override
public void onClick(View v) {
    super.onClick(v);
    switch (v.getId()) {
        case R.id.btn_generate:

            fileName = etFileName.getText().toString().trim();
            File[] files = new File(user.getUserFileSubPath() + "/" + FilePath.PDF).listFiles();
            for (File f : files) {
                if (f.getName().startsWith(fileName)) {
                    showToast("文件已存在");
                    return;
                }
            }
            generate();
            break;
    }
}
/**PDF转换*/
  private void generate(){
        Observable.just(imageArr)
                .compose(this.>bindUntilEvent(ActivityEvent.DESTROY))
                .observeOn(Schedulers.computation())
                .map(new Function, PdfDocument>() {
                    @Override
                    public PdfDocument apply(List localImageBeans) throws Exception {
                        // create a new document
                        PdfDocument document = new PdfDocument();

                        for (int i = 0; i < localImageBeans.size(); i++) {
                            LocalImageBean bean = localImageBeans.get(i);
                            // crate a page description


                            BitmapFactory.Options options = new BitmapFactory.Options();
                            options.inPreferredConfig = Bitmap.Config.RGB_565;
                            float scale;
                            switch (quality) {
                                case QualityMiddle:
//                                            options.inPreferredConfig = Bitmap.Config.ARGB_4444;
                                    scale = 0.75f;
                                    break;
                                case QualitySmall:
//                                            options.inPreferredConfig = Bitmap.Config.RGB_565;
                                    scale = 0.5f;
                                    break;
                                default:
//                                            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                                    scale = 1f;
                                    break;
                            }

                            int screenW = (int) (Utils.getScreenWidth(getImplContext()) * scale);
                            int screenH = (int) (Utils.getScreenHeight(getImplContext()) * scale);

                            if (bean.getRotating() % 180 != 0) {
                                //当前是否横向
                                int temp = screenW;
                                screenW = screenH;
                                screenH = temp;
                            }

                            PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(
                                    screenW,
                                    screenH,
                                    i).create();

                            // start a page
                            PdfDocument.Page page = document.startPage(pageInfo);

                            // draw something on the page
                            WeakReference wfb = new WeakReference<>(BitmapFactory.decodeFile(bean.getRawPath(), options));
                            Bitmap bitmap = wfb.get();
                            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

                            int bitmapW = (int) (bitmap.getWidth() * scale);
                            int bitmapH = (int) (bitmap.getHeight() * scale);

                            Matrix matrix = new Matrix();
                            matrix.postRotate(bean.getRotating());

                            WeakReference nwfb = new WeakReference<>(Bitmap.createScaledBitmap(bitmap, bitmapW, bitmapH, false));

                            if (bean.getRotating() % 180 != 0) {
                                int temp = bitmapW;
                                bitmapW = bitmapH;
                                bitmapH = temp;
                            }
                            Bitmap newBM = nwfb.get();
                            Rect dst = new Rect(0, 0, bitmapW, bitmapH);
                            page.getCanvas().drawBitmap(newBM, null, dst, paint);
                            bitmap.recycle();
                            wfb.clear();
                            newBM.recycle();
                            nwfb.clear();

                            document.finishPage(page);
                        }

                        // write the document content
                        OutputStream out = new FileOutputStream(user.getUserFileSubPath() + "/" + FilePath.PDF + "/" + user.getFileName(fileName, "pdf"));
                        document.writeTo(out);
                        out.close();
                        document.close();
                        //close the document
                        return document;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        showLoading();
                    }

                    @Override
                    public void onNext(PdfDocument pdfDocument) {

                    }


                    @Override
                    public void onError(Throwable e) {
                        hideLoading();
                        LogUtils.e(e);
                    }

                    @Override
                    public void onComplete() {
                        showToast("添加成功");
                        hideLoading();
                        finish();
                    }
                });

    }
调用上述代码即可转换成PDF,imageArr是选中的要生成PDF的图片集合,UserBean是用户的实体类

你可能感兴趣的:(Android)