在Android中创建PDF,但只创建一个页面而我的其他数据都看不到。如何在android.any的想法或建议中自动增加PDF页面运行时。我的问题是如何从该Graphics对象创建具有多个页面的PDF
答案 0 :(得分:3)
在android中创建PDF,多页使用此库
将此gradle文件放入应用build.gradle文件中 - compile' com.itextpdf:itext-pdfa:5.5.8'
使用此代码生成包含多个页面的pdf
pdfName - pdf名称,如您所愿。
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();
}