首页 > 解决方案 > 自动生成更多 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页面中写入。

标签: javaandroidpdfcanvas

解决方案


推荐阅读