java - 自动生成更多 PDF 页面
问题描述
我的代码只能写入一个 PDF 页面。我想根据PDF中记录列表的大小自动生成更多的PDF页面。
我设法手动创建了另一个页面但没有成功,但我希望记录自动在下一页继续。
private void createPdf(){
// create a new document
PdfDocument document = new PdfDocument();
// creating a page description
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(800, 1000, 1).create();
// Starting the page
PdfDocument.Page page = document.startPage(pageInfo);
Canvas canvas = page.getCanvas();
Paint paint = new Paint();
AlunoAdapter adaptador = new AlunoAdapter(alunos, this);
lista.setAdapter(adaptador);//lista que está em ListarALunosActivity
//int noOfPages = (int)Math.floor(canvas.getHeight()/1000)+1;
// Title
paint.setColor(Color.BLUE);
paint.setTextAlign(Paint.Align.CENTER);
paint.setTextSize(20);
canvas.drawText("Quantitativo de Refeições", canvas.getWidth()/2, 50, paint);
canvas.drawText("ETE Edson Mororó Moura", canvas.getWidth()/2, 80, paint);
//Table Title
//paint.setStyle(Paint.Style.FILL);
paint.setTextSize(15);
canvas.drawText("Qtd", 75, 130, paint);
canvas.drawText("Estudante - Turma", 213, 130, paint);
canvas.drawText("Horário", 585, 130, paint);
//printing the records
paint.setColor(Color.BLACK);
paint.setTextAlign(Paint.Align.LEFT);
paint.setTextSize(15);
int y = 180;
int y2 = 155;
for (Aluno a : alunos) {
//canvas.drawText(a.getId()+" "+a.getEstudante()+" "+a.getHora(), 30, y, paint);
canvas.drawText(a.getId()+"", 70, y, paint);
canvas.drawText(a.getEstudante()+"", 150, y, paint);
canvas.drawText(a.getHora()+"", 560, y, paint);
y+=50;
y2+=50;
canvas.drawText("----------------------------------------------------------------------------------------------------------", 70, y2, paint);
}
// final of page
document.finishPage(page);
//CREATE PAGE 2
pageInfo = new PdfDocument.PageInfo.Builder(800, 1000, 2).create();
page = document.startPage(pageInfo);
canvas = page.getCanvas();
paint = new Paint();
for (Aluno a : alunos) {
canvas.drawText(a.getId()+"", 70, y, paint);
canvas.drawText(a.getEstudante()+"", 150, y, paint);
canvas.drawText(a.getHora()+"", 560, y, paint);
y+=50;
y2+=50;
canvas.drawText("----------------------------------------------------------------------------------------------------------", 70, y2, paint);
}
document.finishPage(page);
// write the document content
String directory_path = Environment.getExternalStorageDirectory().getPath() + "/";
File file = new File(directory_path);
if (!file.exists()) {
file.mkdirs();
}
String targetPdf = directory_path+"quantitativoEEMM.pdf";
File filePath = new File(targetPdf);
try {
document.writeTo(new FileOutputStream(filePath));
//Toast.makeText(this, "Saved successfully!", Toast.LENGTH_LONG).show();
msgPDFgerado();
} catch (IOException e) {
Log.e("main", "error "+e.toString());
Toast.makeText(this, "ERROR " + e.toString(), Toast.LENGTH_LONG).show();
}
// close the document
document.close();
}
400多条记录将被保存为PDF,所以我需要在更多的PDF页面中写入。
解决方案
推荐阅读
- java - 如何从屏幕的任何位置拖动底页?
- javascript - axios.post 的 .then() 不起作用
- swift - Swift WKWebView goBack 和 goForward 不再工作
- ajax - extjs 6.53 全局增加远程请求超时
- amazon-web-services - 如何让@Scheduled 在 EC2 上运行
- java - 如何修复 java.lang.ClassNotFoundException: 一个简单的 hello world 的错误?
- excel - excel导入的SSIS问题
- c# - 在 ASP.net Core 中,如何将重定向用户发布到另一个带有数据的 url?
- roblox - Roblox如何在游戏中更改对象名称
- javascript - 如何在异步函数的上下文中对数组进行多个过滤器?