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
Asked
Active
Viewed 1,400 times
-2
-
1Your question is unclear. New pages are created automatically if there is more data then there is space on a single page. You mention a `Graphics` object. I assume that you refer to `PdfGraphics2D`. However, you also mention Android. There is no `Graphics2D` on Android, so your question doesn't make any sense. – Bruno Lowagie Apr 01 '16 at 07:12
1 Answers
3
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();
}

Hardik Dungrani
- 119
- 9
-
-
1@Ravimakhija Please understand that this is a good answer to a bad question. Using `newPage()` triggers a new page. Obviously, you shouldn't copy/paste this snippet and expect it to work as it involves an array of images. Nowhere in your question do you refer to images, so you probably have to adapt this example. The most essential line in this example is `document.newPage()`. – Bruno Lowagie Apr 01 '16 at 07:14