Android 导出PDF PdfDocument

/**
 * 导出PDF
 *
 * @param view            要导出的view,如果view高度过高(超过一屏的高度),在改view外部套一层Scrollview即可
 *                        如果要导出列表类型View 比如Listview Recyclerview ,会遇见导出不全的情况,这里提前绘制view即可,方法如下
 *                        binding.mReDetail.measure(
 *                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
 *                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
 *                        );
 *                        binding.mReDetail.layout(0, 0, binding.mReDetail.getMeasuredWidth(), binding.mReDetail.getMeasuredHeight());
 * @param fileDir         导出文件夹
 * @param fileName        导出文件名称
 * @param fileNameReplace 文件名称存在时,是否需要替换
 * @return
 */
public static boolean createPdfFromView(View view, String fileDir, String fileName, boolean fileNameReplace) {
        try {
            if (view == null || fileDir == null || fileName == null) {
                Log.e(TAG, "导出PDF" + "入参为空");
                return false;
            }
            String s = Environment.getExternalStorageDirectory() + "/" + fileDir;
//            String mSDCardFolderPath = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + "/" + fileDir;
            File dir = new File(s);
            //判断文件是否存在
            if (!dir.exists()) {
                //不存在则创建
                dir.mkdirs();
            }
            File pdfFile = new File(dir, fileName + ".PDF");
            if (!pdfFile.exists()) {
                pdfFile.createNewFile();
            } else {
                if (fileNameReplace) {
                    String newFileName = getPDFNewFileName(pdfFile);
                    pdfFile = new File(newFileName);
                    pdfFile.createNewFile();
                }
            }

            PdfDocument document = new PdfDocument();
            //页对象
            PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(
                    view.getWidth(),
                    view.getHeight(),
                    1)
                    .create();

            // 开始页
            PdfDocument.Page page = document.startPage(pageInfo);
            //绘制页
            Canvas canvas = page.getCanvas();
            view.draw(canvas);
            //结束页
            document.finishPage(page);
            //TODO  需要的话,增加更多页
            //导出文档
            FileOutputStream os = null;

            Log.i(TAG, "导出PDF" + " 开始导出,导出路径:" + pdfFile);
            os = new FileOutputStream(pdfFile);
            document.writeTo(os);
            os.close();
            Log.i(TAG, "导出PDF" + " 导出成功");
            document.close();
            return true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }
private static String getPDFNewFileName(File file) {
    if (file.exists()) {
        String newPath = file.getPath().substring(0, file.getPath().length() - 4) + "(1).PDF";
        return getPDFNewFileName(new File(newPath));
    } else {
        return file.getPath();
    }
}

使用方式:

new Thread(new Runnable() {
    @Override
    public void run() {
        boolean pdfFromView = SheetHelper.createPdfFromView(mWeb, "痕迹导出文件夹", "exportPDF", true);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (pdfFromView) {
                    Toast.makeText(MainActivity2.this, "PDF 导出成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity2.this, "PDF 导出失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}).start();

注意:Android 6 以上要动态申请读写权限,Android 10 有私有文件夹限制。

excel 的导入导出:

Android 导入导出excel xls、xlsx_钟情短发姑娘的博客-CSDN博客

你可能感兴趣的:(android,java,开发语言)