【发布时间】:2018-04-15 10:35:21
【问题描述】:
你好,朋友们好吗.....我在这里是因为我想从 android listview 创建多页 pdf 我已经成功实现了将所有 listview 项目转换为 pdf,即使它们不是全部可见,使用 itextg 问题如果列表视图中的项目或捕获的列表视图图像超过了 pdf 中页面的长度,我面临的是它剪切的其余项目...... 所以我想要的是创建一个多页pdf,如果第一页已满,然后将图像的其余部分写在第二张图像中……我希望我能尽快回复…………这些是通过捕获所有列表视图项来创建 pdf 的代码 ....
//method wich generate pdf of the attendance data
private void save_as_pdf() {
//First Check if the external storage is writable
String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state)) {
// Toast.makeText(context,"")
}
//Create a directory for your PDF
final File pdfDir = new File(Environment.getExternalStorageDirectory() + "/Documents", "attendance_report");
if (!pdfDir.exists()) {
pdfDir.mkdir();
}
//take screen shoot of the entire listview of the attendance report
ListView listview = studentlist;
ListAdapter adapter = listview.getAdapter();
int itemscount = adapter.getCount();
int allitemsheight = 0;
List<Bitmap> bmps = new ArrayList<Bitmap>();
for (int i = 0; i < itemscount; i++) {
View childView = adapter.getView(i, null, listview);
childView.measure(
View.MeasureSpec.makeMeasureSpec(listview.getWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
childView.setDrawingCacheEnabled(true);
childView.buildDrawingCache();
bmps.add(childView.getDrawingCache());
allitemsheight += childView.getMeasuredHeight();
}
Bitmap bigbitmap = Bitmap.createBitmap(listview.getMeasuredWidth(), allitemsheight,
Bitmap.Config.ARGB_8888);
Canvas bigcanvas = new Canvas(bigbitmap);
bigcanvas.drawColor(getResources().getColor(R.color.white));
Paint paint = new Paint();
int iHeight = 0;
for (int i = 0; i < bmps.size(); i++) {
Bitmap bmp = bmps.get(i);
bigcanvas.drawBitmap(bmp, 0, iHeight, paint);
iHeight += bmp.getHeight();
bmp.recycle();
bmp = null;
}
//Now create the name of your PDF file that you will generate
File pdfFile = new File(pdfDir, "report_for( " + course + "," + semister + section + "," + date + ").pdf");
try {
com.itextpdf.text.Document document = new com.itextpdf.text.Document();
PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
document.addTitle("Attendance Report Generated For course:" + course + " Semester:" + semister + " Section:" + section + " Date:" + date);
document.addHeader("Header", "Department Of Information Science");
document.addCreator("Department Of Information Science");
document.addCreationDate();
document.bottomMargin();
document.setPageCount(3);
document.open();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bigbitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
addImage(document, byteArray);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void addImage(com.itextpdf.text.Document document, byte[] byteArray) {
Image image = null;
try {
image = Image.getInstance(byteArray);
} catch (BadElementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// image.scaleAbsolute(150f, 150f);
try {
document.add(image);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
【问题讨论】:
-
呸:你是说你创建一个带有光栅图像中文本的 PDF,而不是创建一个使用字体正确添加文本的 PDF?这是一个可怕的想法。如果您是学生,您的代码应该得到一个 F。它归结为“我如何在不同的页面上分发图像),但即使你得到答案,结果也总是一个糟糕的 PDF。我的建议:扔掉你的代码,重新开始一个更好的设计。为什么考勤数据存储在一张图片中?为什么要把所有这些小位图合并成一张大图?请扔掉你的代码!请重新开始!
-
你甚至没有给我任何解决方案只是说话你能给我一些代码来按照你所说的而不是仅仅说话
-
不。 Stack Overflow 不是让人们要求其他人为他们工作的。我告诉你你做错了什么。从这些错误中吸取教训。重新开始。如果您对新代码有疑问,请发布新问题。
标签: android listview pdf itextg