问题
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
- tempArrayList - array of file path.
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