PDF multipage not create in android [closed]

戏子无情 提交于 2019-12-13 08:03:11

问题


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


回答1:


Create PDF in android with Multiple pages use this library

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

Use this code for generate pdf with multiple pages

  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();
    

    }



来源:https://stackoverflow.com/questions/36349397/pdf-multipage-not-create-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!