我正在尝试生成一些列表以发送给pdf。我能够将所有项目生成为pdf,但是每个项目只能生成一页。我想生成一个分页列表。到目前为止,我的代码:
填充recyclerView。
List<Bitmap> pegarBitmapsListaTeste(View pagina1, RecyclerView view) {
List<Bitmap> listaBitmaps = new ArrayList<>();
RecyclerView.Adapter adapter = view.getAdapter();
if (adapter != null) {
int size = adapter.getItemCount();
LinearLayoutManager layoutManagerr = ((LinearLayoutManager)view.getLayoutManager());
int findFirstVisibleItemPosition = layoutManagerr.findFirstVisibleItemPosition();
int findLastVisibleItemPosition = layoutManagerr.findLastCompletelyVisibleItemPosition();
pagina1.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
pagina1.layout(0, 0, pagina1.getMeasuredWidth(), pagina1.getMeasuredWidth());
pagina1.setDrawingCacheEnabled(true);
pagina1.buildDrawingCache();
listaBitmaps.add(pagina1.getDrawingCache());
final int maxItens = findLastVisibleItemPosition;
final int TOTAL_NUM_ITEMS=size;
final int ITEMS_PER_PAGE=maxItens;
final int ITEMS_REMAINING=TOTAL_NUM_ITEMS % ITEMS_PER_PAGE;
final int LAST_PAGE=TOTAL_NUM_ITEMS/ITEMS_PER_PAGE;
Log.i("dados quantPaginas", String.valueOf(LAST_PAGE));
for (int i = 0; i < size; i++) {
RecyclerView.ViewHolder holder = adapter.createViewHolder(view, adapter.getItemViewType(i));
adapter.onBindViewHolder(holder, i);
holder.itemView.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(), holder.itemView.getMeasuredHeight());
View childView = holder.itemView;
childView.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
childView.setDrawingCacheEnabled(true);
childView.buildDrawingCache();
listaBitmaps.add(childView.getDrawingCache());
Log.i("dados loop", String.valueOf(i));
}
}
return listaBitmaps;
}
然后我有另一种方法来使用位图列表并生成pdf文件。
private void createPdff(List<Bitmap> bitmaps, String nomePdf) {
PdfDocument document = new PdfDocument();
PdfDocument.Page page = null;
// crate a page description
for (int i = 0; i < bitmaps.size(); i++) {
Bitmap bitmap = bitmaps.get(i);
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(layout_relatorio.getWidth(), layout_relatorio.getHeight(), i).create();
// start a page
page = document.startPage(pageInfo);
if (page == null) {
return;
}
Canvas canvas = page.getCanvas();
canvas.drawBitmap(bitmap, 0, 0, null);
document.finishPage(page);
Log.i("dados", "chegou aqui no Finish Him");
}
// do final processing of the page
File folder = new File(Objects.requireNonNull(getActivity()).getApplicationContext().getFilesDir() + File.separator + "pdfs");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdirs();
}
if (success) {
// Do something on success
} else {
// Do something else on failure
}
File outputFile = new File(Objects.requireNonNull(getActivity()).getApplicationContext().getFilesDir() + "/pdfs/", nomePdf);
try {
outputFile.createNewFile();
OutputStream out = new FileOutputStream(outputFile);
document.writeTo(out);
document.close();
out.close();
localPdf = FileProvider.getUriForFile(Objects.requireNonNull(getActivity()), "vnm.com.simasdapp", outputFile);
} catch (IOException e) {
e.printStackTrace();
Log.i("dados 5", e.getMessage());
}
}
这样,我生成了很多小页面。 结果:
我需要什么:
谢谢。