PDF多页面不能在android中创建 [英] PDF multipage not create in android

查看:143
本文介绍了PDF多页面不能在android中创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android中创建PDF但只创建一个页面而我的其他数据都看不到。如何在android.any的想法或建议中自动增加PDF页面运行时间。我的问题是如何使用该Graphics对象创建多页的PDF

Create PDF in android but create only one page and my other data are not see. how to auto increment PDF page runtime in android.any idea or suggestion.My problem is how to create a PDF with multiple pages from that Graphics object

推荐答案

在Android中创建PDF,多个页面使用此库

Create PDF in android with Multiple pages use this library

将此gradle文件放入应用程序build.gradle文件
- compile'c​​om .itextpdf:itext-pdfa:5.5.8'

put this gradle file in your apps build.gradle file -- compile 'com.itextpdf:itext-pdfa:5.5.8'

使用此代码生成包含多个页面的pdf

Use this code for generate pdf with multiple pages


  1. tempArrayList - 文件路径数组。

  2. pdfName - pdf name as your wish。

  1. tempArrayList - array of file path.
  2. pdfName - pdf name as you wish.

generatePDF(String pdf_name) {

  String pdfName= null;
  Dialog  dialog = new MaterialDialog.Builder(activity)
        .backgroundColor(Color.WHITE).contentColor(Color.BLACK).title(getString(R.string.app_name)).titleColor(Color.BLACK)
        .content("Generating pdf...").progress(true, 0)
        .show();

 dialog.setCancelable(false);

  pdfName = new SimpleDateFormat("yyyyMMdd_HHmmss").format(System.currentTimeMillis()) + ".pdf";

File myPath = new File(AppConstant.Pdf_Directory, pdfName);
if (myPath.exists()) {
    myPath.delete();
}

Document document = new Document(PageSize.A4); // create the document

try {
    PdfWriter.getInstance(document, new FileOutputStream(myPath));
} catch (Exception e) {
}

// open document
document.open();

for (int i = 0; i < tempArrayList.size(); i++) {

    Bitmap bmp = null;
    try {
        bmp = BitmapFactory.decodeFile(tempArrayList.get(i));
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 90, stream);
        Image image = Image.getInstance(stream.toByteArray());

        if (image.getWidth() >= document.getPageSize().getWidth() || image.getHeight() >= document.getPageSize().getHeight()) {
            image.scaleToFit(document.getPageSize());
        }

        image.setAbsolutePosition((document.getPageSize().getWidth() - image.getScaledWidth()) / BaseField.BORDER_WIDTH_MEDIUM, (document.getPageSize().getHeight() - image.getScaledHeight()) / BaseField.BORDER_WIDTH_MEDIUM);
        document.add(image);
        document.newPage();

    } catch (Exception ex) {
        ex.printStackTrace();
        Toast.makeText(activity, "Fail to generate pdf.", Toast.LENGTH_SHORT).show();
    }
    if (bmp != null) {
        bmp.recycle();
    }
}
// close the document
document.close();

Toast.makeText(activity, "Pdf generate successfully.", Toast.LENGTH_SHORT).show();
System.out.println(" pdf generate ");

dialog.dismiss();

}

这篇关于PDF多页面不能在android中创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆